-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.py
44 lines (32 loc) · 982 Bytes
/
remote.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
import telnetlib
import socket
import logging
import config
def _execute(cmd):
#logging.debug('execute:' + cmd)
session = telnetlib.Telnet(config.host, config.port, timeout=5.0)
session.read_until('ogin: ')
session.write(config.user + '\n')
session.read_until('assword: ')
session.write(config.password + '\n')
session.read_eager() # flush login prompt
session.write(cmd + '\n')
session.write('exit\n')
response = session.read_all()
#logging.debug('response:' + response)
session.close() # be nice to remote host
return response
def execute(cmd, tries=3):
response = None
while tries > 0 and not response:
try:
response = _execute(cmd)
except socket.timeout:
#logging.debug('timeout')
pass
except EOFError as eof:
#logging.info(eof)
break
return response
if __name__ == '__main__':
print(execute('iptables -L -vx'))