Python Paramiko登录并关闭ssh会话后报告“传输线程中的EOF”

问题描述 投票:0回答:2

我正在编写一个脚本,该脚本应该登录到某些网络设备,执行命令并收集一些文件。现在,我似乎陷入了一个问题,即登录后并且不执行任何操作或在远程主机上执行任何操作后,ssh连接就会关闭。检查来自paramiko的调试消息后,我看到以下消息:“传输线程中的EOF”。我在Windows和Linux上使用密码和PSK进行了尝试,也尝试过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)

任何想法如何对此进行进一步调查或根本原因是什么?

python ssh paramiko eof
2个回答
0
投票

在break语句之后,脚本终止...因此,添加您需要做的工作。


0
投票

看来这是由于我尝试访问的设备仅允许使用交互式外壳程序,所以我不得不使用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
© www.soinside.com 2019 - 2024. All rights reserved.