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
| class ModTelnet(MXComm):
def __init__(self):
MXComm.__init__(self)
self._tn = None
def _connect(self):
#connect to telnet session
try:
self._tn = pexpect.spawn('telnet')
i = self._tn.expect('telnet>', 3)
if i == 0:
print 'connected'
elif i !=0:
print 'connection error'
except:
print "Connection refused"
def _receive(self):
#receive data (= msg) from telnet stdout
try:
saveout = sys.stdout
outfile = open('output.txt', 'w')
sys.stdout = outfile
outfile.flush()
data = outfile.readline()
outfile.close
sys.stdout = saveout
return data
except:
print 'Connection error:'
raise Disconnected()
def _send(self, command):
#send command to telnet session
try:
self._tn.send('help')
except :
print 'Connection error:'
raise Disconnected() |
Partager