pexpect 登陆ssh遇到个问题及解决

本文发布时间: 2019-Mar-21
pexpect 登陆ssh,The authenticity of host ' (' can't be established.RSA key fingerprint is d2:98:ec:00:06:61:e0:aa:14:e8:53:84:8a:cc:14:a6.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '' (RSA) to the list of known hosts.由于多了"Warning: Permanently added '' (RSA) to the list of known hosts."这里卡住,设置pexpect 超时时间没用sh = pexpect.spawn('ssh -p %s %[email protected]%s "%s"'%(port,user, ip, cmd),timeout=None)r = '' try: i = ssh.expect(['password: ', 'continue connecting (yes/no)?'],) #print "i为",i if i == 0 : ssh.sendline(passwd) ssh.sendline('\n') elif i == 1: ssh.sendline('yes \n') print ssh.before,ssh.after except pexpect.EOF,pexpect.TIMEOUT: print ssh.before,ssh.after ssh.close() else: r = ssh.expect(pexpect.EOF) print ssh.before,ssh.after ssh.close()终于在网上找到类似解决办法ssh = pexpect.spawn('ssh -oStrictHostKeyChecking=no -n -p %s %[email protected]%s "%s"'%(port,user, ip, cmd),timeout=120)加了 "ssh -oStrictHostKeyChecking=no",用来避免 输入第一次 执行ssh 要求收入yes/no 用的。


2020-Jul-13 01:11am