0
回答

#!/usr/bin/env python import pexpect import getpass, os import traceback import time def ssh_cisco (host): ssh_newkey = 'Are you sure you want to continue connecting' child = pexpect.spawn('ssh -o StrictHostKeyChecking=no -l %s %s %s' % (host.userName, host.hostIp, host.commandList)) child.maxread=50000 i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: ']) if i == 0: # Timeout print 'ERROR!' print 'SSH could not login. Here is what SSH said:' print child.before, child.after return None if i == 1: # SSH does not have the public key. Just accept it. child.sendline ('yes') child.expect ('password: ') i = child.expect([pexpect.TIMEOUT, 'password: ']) if i == 0: # Timeout print 'ERROR!' print 'SSH could not login. Here is what SSH said:' print child.before, child.after return None child.sendline(host.passWord) child.expect(pexpect.EOF) result = child.before print result filename="host/"+getfilename()+".txt" filehandler = open(filename, 'w') filehandler.write(result) filehandler.close() return filename def getfilename(): return time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
这是什么原因?