奇怪的EOFError与python和paramiko

时间:2016-02-11 15:50:04

标签: python paramiko

我的代码:

#!/usr/bin/env python
# encoding: utf-8

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('10.X.X.X',username='user',password='password')

stdin, stdout, stderr=ssh.exec_command("get system status")
type(stdin)
stdout.readlines()

我想的很简单,但跑步引发了追溯:

Traceback (most recent call last):
  File "/Users/adieball/Dropbox/Multiverse/Programming/workspace/FortiNet/src/runCommand.py", line 8, in <module>
    ssh.connect('10.X.X.X',username='user',password='password')
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/paramiko/client.py", line 325, in connect
    t.start_client()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/paramiko/transport.py", line 492, in start_client
    raise e
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/paramiko/transport.py", line 1726, in run
    ptype, m = self.packetizer.read_message()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/paramiko/packet.py", line 386, in read_message
    header = self.read_all(self.__block_size_in, check_rekey=True)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/paramiko/packet.py", line 251, in read_all
    raise EOFError()
EOFError

我在这里有点困惑,也许我是瞎子,但我找不到问题。

感谢

显然:连接每个“普通”ssh并运行此命令完全正常: - )

我改为python 2.7(Mac上的默认设置)并完全删除了3.5安装。 我现在得到一个不同的错误(尽管仍然是EOFError):

Traceback (most recent call last):
  File "/Users/adieball/Dropbox/Multiverse/Programming/workspace/FortiNet/src/runCommand.py", line 8, in <module>
    ssh.connect('10.X.X.X,username='user',password='password')
  File "build/bdist.macosx-10.11-intel/egg/paramiko/client.py", line 325, in connect
  File "build/bdist.macosx-10.11-intel/egg/paramiko/transport.py", line 492, in start_client
EOFError

如前所述,我可以用“普通”ssh完美地连接到盒子。 我还用以下方法测试了python的连接性:

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(0.5)

try:
        s.connect(('10.2.2.254',22)) 
except Exception, e:
        print 'connection failed'
else:
    print "success"

s.close() 

并且它起作用(即印刷“成功”),但无论出于何种原因,paramiko似乎无法连接。 我在python shell中逐步尝试了我的代码,以查看错误发生的时间,并在

之后发生
ssh.connect('10.X.X.X',username='user',password='password')

2 个答案:

答案 0 :(得分:1)

您在编写脚本以远程访问fortigate时遇到了同样的错误。我可以看到你正在尝试使用forigate。

我已经在“配置系统全局”下更改了fortigate上的配置并且它有效(我不知道实际上做了哪些更改,需要一些试验和错误才能知道)

config system global
     - set admin-server-cert "Fortinet_Factory"
     - set admin-sport 2222
     - set admin-ssh-grace-time 20
     - set admin-ssh-v1 enable
     - set auth-cert "Fortinet_Factory"
     - set dh-params 1024
     - set fgd-alert-subscription advisory latest-threat
     - set gui-theme red
     - set gui-wireless-opensecurity enable
     - set hostname "FortiGate-VM64"
     - set ssh-cbc-cipher disable
     - set ssh-hmac-md5 disable
     - set strong-crypto disable
     - set timezone 04
end

如果你确实知道哪一个是必要的,请告诉我。

答案 1 :(得分:0)

感谢您的帮助。我按照你的建议做了并得到了:

DEBUG:paramiko.transport:starting thread (client mode): 0x7fe51d0L
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_1.16.0
DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-a3WzN
INFO:paramiko.transport:Connected (version 2.0, client a3WzN)
DEBUG:paramiko.transport:kex algos:[u'diffie-hellman-group-exchange-sha1', u'diffie-hellman-group1-sha1'] server key:[u'ssh-rsa', u'ssh-dss'] client encrypt:[u'aes128-cbc', u'3des-cbc', u'blowfish-cbc', u'cast128-cbc', u'arcfour', u'aes192-cbc', u'aes256-cbc', u'rijndael-cbc@lysator.liu.se', u'aes128-ctr', u'aes192-ctr', u'aes256-ctr'] server encrypt:[u'aes128-cbc', u'3des-cbc', u'blowfish-cbc', u'cast128-cbc', u'arcfour', u'aes192-cbc', u'aes256-cbc', u'rijndael-cbc@lysator.liu.se', u'aes128-ctr', u'aes192-ctr', u'aes256-ctr'] client mac:[u'hmac-md5', u'hmac-sha1', u'hmac-ripemd160', u'hmac-ripemd160@openssh.com', u'hmac-sha1-96', u'hmac-md5-96'] server mac:[u'hmac-md5', u'hmac-sha1', u'hmac-ripemd160', u'hmac-ripemd160@openssh.com', u'hmac-sha1-96', u'hmac-md5-96'] client compress:[u'none', u'zlib'] server compress:[u'none', u'zlib'] client lang:[u''] server lang:[u''] kex follows?False
DEBUG:paramiko.transport:Kex agreed: diffie-hellman-group1-sha1
DEBUG:paramiko.transport:Cipher agreed: aes128-ctr
DEBUG:paramiko.transport:MAC agreed: hmac-md5
DEBUG:paramiko.transport:Compression agreed: none
DEBUG:paramiko.transport:EOF in transport thread