-
Notifications
You must be signed in to change notification settings - Fork 10
/
coding.ch143.py
executable file
·70 lines (64 loc) · 1.75 KB
/
coding.ch143.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import paramiko, socket, select
import os, sys
import sudoku # http://norvig.com/sudopy.shtml
SSH_CLEAR_DATA = 1
SSH_BREAK_CHAT = 2
def ssh_chat(chan, data_cb):
data = ''
while True:
chan.settimeout(0.0)
rl, wl, xl = select.select([chan], [], [])
try:
b = chan.recv(1024)
if len(b) == 0:
break
data += b
action, reply = data_cb(data)
if action > 0 and action & SSH_CLEAR_DATA == SSH_CLEAR_DATA:
data = ''
if reply is not None:
chan.sendall(reply)
if action > 0 and action & SSH_BREAK_CHAT == SSH_BREAK_CHAT:
break
except socket.timeout:
pass
data_cb(data, True)
class Sudoku:
def __init__(self):
pass
def recv_data(self, data, is_last = False):
cdata = data.strip()
if is_last:
print(cdata)
if cdata.endswith('Solution:'):
gridx = ''
for line in cdata.splitlines():
if not line.startswith('|'): continue
line, reqrep = line.replace(' ',''), True
while reqrep:
l, line = len(line), line.replace('||','|.|')
reqrep = l != len(line)
line = line.replace('|','')
gridx += line
solved = sudoku.solve(gridx)
solution = ','.join(solved[s] for s in sudoku.squares)
return (SSH_CLEAR_DATA, '{0}\n'.format(solution))
return (0, None)
def ch143():
ek = 'RZ_CH143_PW'
if not ek in os.environ:
print('err: {0} environment variable not set'.format(ek))
sys.exit(1)
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
host, port, username, password = 'ringzer0team.com', 12643, 'sudoku', os.environ[ek]
client.connect(host, port, username, password)
chan = client.invoke_shell()
ob = Sudoku()
ssh_chat(chan, ob.recv_data)
chan.close()
client.close()
if __name__ == '__main__':
ch143()