Python Paraimko在传输线程中报告" EOF"登录并关闭ssh会话后

时间:2016-12-28 15:20:14

标签: python ssh paramiko eof

我正在编写一个脚本,该脚本应该登录到某些网络设备,执行命令并收集一些文件。现在我似乎陷入了一个问题,即在登录后没有执行任何操作或在远程主机上执行任何操作时,ssh连接将被关闭。检查来自paramiko的调试消息后,我看到以下消息:"传输线程中的EOF"。我已经尝试过使用密码和PSK,从Windows和Linux,也试过Spur,总是得到相同的EOF。到目前为止,我只有会话边界控制器具有不同的软件版本,但无法对其进行任何调试,而运行调试甚至无法看到SSH连接发生,可能它发生在某处我无法访问的背景。

这里是调试消息:

Trying to connect to 10.xxx.xxx.xxx (1/3)
starting thread (client mode): 0x57f09d0L
Local version/idstring: SSH-2.0-paramiko_2.1.1
Remote version/idstring: SSH-2.0-Mocana SSH
Connected (version 2.0, client Mocana)
kex algos:[u'diffie-hellman-group14-sha1', u'diffie-hellman-group1-sha1'] server key:[u'ssh-dss', u'ssh-rsa'] client encrypt:[u'aes256-cbc', u'rijndael256-cbc', u'aes192-cbc', u'rijndael192-cbc', u'aes128-cbc', u'rijndael128-cbc', u'3des-cbc', u'arcfour'] server encrypt:[u'aes256-cbc', u'rijndael256-cbc', u'aes192-cbc', u'rijndael192-cbc', u'aes128-cbc', u'rijndael128-cbc', u'3des-cbc', u'arcfour'] client mac:[u'hmac-sha1', u'hmac-sha1-96', u'hmac-md5', u'hmac-md5-96'] server mac:[u'hmac-sha1', u'hmac-sha1-96', u'hmac-md5', u'hmac-md5-96'] client compress:[u'none'] server compress:[u'none'] client lang:[u''] server lang:[u''] kex follows?False
Kex agreed: diffie-hellman-group1-sha1
Cipher agreed: aes128-cbc
MAC agreed: hmac-md5
Compression agreed: none
kex engine KexGroup1 specified hash_algo <built-in function openssl_sha1>
Switch to new keys ...
Adding ssh-rsa host key for 10.xxx.xxx.xxx: e3afa50cb2380e75cbbd535fb6ceb3fc
userauth is OK
Authentication (password) successful!
Connected to 10.xxx.xxx.xxx
EOF in transport thread

这是脚本,没有,只有登录:

import paramiko
import time
import sys
import logging

logging.getLogger("paramiko").setLevel(logging.DEBUG) 


host = '10.xxx.xxx.xxx'
username = 'username'
password = 'password'
i = 1

while True:
    print ("Trying to connect to %s (%i/3)" % (host, i))

    try:
        paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG)
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(host,username=username,password=password)
        print "Connected to %s" % host
        break
    except paramiko.AuthenticationException:
        print "Authentication failed when connecting to %s" % host
        sys.exit(1)
    except:
        print "Could not SSH to %s, waiting for it to start" % host
        i += 1
        time.sleep(2)

    # If we could not connect within time limit
    if i == 3:
        print "Could not connect to %s. Giving up" % host
        sys.exit(1)

任何想法如何进一步调查或可能是根本原因?

2 个答案:

答案 0 :(得分:0)

在break语句之后,脚本终止......所以添加你需要做的工作。

答案 1 :(得分:0)

看起来这是因为我尝试访问的设备只允许交互式shell,所以我不得不使用paramiko的invoke_shell,对我来说它基本上是这样的:

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host,username=username,password=password)
channel = ssh.invoke_shell()
channel.send('uname -a\n' )

buff=''
while not buff.endswith('# '): # Checking for returned prompt
    resp = channel.recv(4096)
    buff += resp
print resp