0
回答
执行命令后,输出结果有空行
华为云4核8G,高性能云服务器,免费试用   
#!/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()))  

这是什么原因?

举报
刘炳军
发帖于7年前 0回/386阅
顶部