Ошибка EOF с методами exec_command и invoke_shell в paramiko - PullRequest
0 голосов
/ 08 мая 2020

Я пытаюсь выполнить команду на сервере linux с моей windows машины, используя python paramiko, я использовал оба метода

1.exec_command

2. invoke_shell

Оба они дают ошибку EOF.

import paramiko
import time
def ConnectSSH1(host, port, username, password):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(host, int(port), username, password)
    chan = ssh.invoke_shell() # Throws EOF error here 
    #stdin, stdout, stderr = ssh.exec_command("ls")   #Throws EOF error here with exec_command
    chan.send("ls -a")
    time.sleep(5)
    output = chan.recv(9999)
    print (output)
    print (command)
    return (output)

Ошибка в exec_command

C:\RobotFramework>python paramiko_solution1.py
Traceback (most recent call last):
  File "paramiko_solution1.py", line 15, in <module>
    chan.exec_command(command)
  File "C:\Python37\lib\site-packages\paramiko\channel.py", line 72, in _check
    return func(self, *args, **kwds)
  File "C:\Python37\lib\site-packages\paramiko\channel.py", line 257, in exec_command
    self._wait_for_event()
  File "C:\Python37\lib\site-packages\paramiko\channel.py", line 1226, in _wait_for_event
    raise e
  File "C:\Python37\lib\site-packages\paramiko\transport.py", line 2055, in run
    ptype, m = self.packetizer.read_message()
  File "C:\Python37\lib\site-packages\paramiko\packet.py", line 459, in read_message
    header = self.read_all(self.__block_size_in, check_rekey=True)
  File "C:\Python37\lib\site-packages\paramiko\packet.py", line 303, in read_all
    raise EOFError()
EOFError

Ошибка в команде invoke_shell

>>> channel.get_pty()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python37\lib\site-packages\paramiko\channel.py", line 72, in _check
    return func(self, *args, **kwds)
  File "C:\Python37\lib\site-packages\paramiko\channel.py", line 203, in get_pty
    self._wait_for_event()
  File "C:\Python37\lib\site-packages\paramiko\channel.py", line 1226, in _wait_for_event
    raise e
  File "C:\Python37\lib\site-packages\paramiko\transport.py", line 2055, in run
    ptype, m = self.packetizer.read_message()
  File "C:\Python37\lib\site-packages\paramiko\packet.py", line 459, in read_message
    header = self.read_all(self.__block_size_in, check_rekey=True)
  File "C:\Python37\lib\site-packages\paramiko\packet.py", line 303, in read_all
    raise EOFError()
EOFError
>>>

Журналы Parmakio

CRITICAL:root:None
DEBUG:paramiko.transport:starting thread (client mode): 0x8133c488
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.7.1
DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-SSH
INFO:paramiko.transport:Connected (version 2.0, client SSH)
DEBUG:paramiko.transport:kex algos:['diffie-hellman-group-exchange-sha256', 'diffie-hellman-group1-sha1', 'diffie-hellman-group14-sha1'] server key:['ssh-rsa'] client encrypt:['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'aes128-cbc', 'blowfish-cbc', 'cast128-cbc', 'aes192-cbc', 'aes256-cbc', '3des-cbc', 'arcfour'] server encrypt:['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'aes128-cbc', 'blowfish-cbc', 'cast128-cbc', 'aes192-cbc', 'aes256-cbc', '3des-cbc', 'arcfour'] client mac:['hmac-md5', 'hmac-md5-96', 'hmac-sha1', 'hmac-sha1-96', 'hmac-sha2-256', 'hmac-sha2-512'] server mac:['hmac-md5', 'hmac-md5-96', 'hmac-sha1', 'hmac-sha1-96', 'hmac-sha2-256', 'hmac-sha2-512'] client compress:['none'] server compress:['none'] client lang:[''] server lang:[''] kex follows?False
DEBUG:paramiko.transport:Kex agreed: diffie-hellman-group-exchange-sha256
DEBUG:paramiko.transport:HostKey agreed: ssh-rsa
DEBUG:paramiko.transport:Cipher agreed: aes128-ctr
DEBUG:paramiko.transport:MAC agreed: hmac-sha2-256
DEBUG:paramiko.transport:Compression agreed: none
DEBUG:paramiko.transport:Got server p (2048 bits)
DEBUG:paramiko.transport:kex engine KexGexSHA256 specified hash_algo <built-in function openssl_sha256>
DEBUG:paramiko.transport:Switch to new keys ...
DEBUG:paramiko.transport:Adding ssh-rsa host key for 12.22.124.66: b'16d9d878229cssdcc837d33a64'
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (password) successful!
CRITICAL:root:None
DEBUG:paramiko.transport:[chan 0] Max packet in: 32768 bytes
DEBUG:paramiko.transport:[chan 0] Max packet out: 32768 bytes
DEBUG:paramiko.transport:Secsh channel 0 opened.
DEBUG:paramiko.transport:EOF in transport thread

Журналы от S SH windows

ssh -vv eijm@12.22.124.66 /usr/bin/who
OpenSSH_for_Windows_8.1p1, LibreSSL 2.9.2
debug2: resolve_canonicalize: hostname 12.22.124.66 is address
debug2: ssh_connect_direct
debug1: Connecting to 12.22.124.66 [12.22.124.66] port 22.
debug1: Connection established.
debug1: identity file C:\\Users\\eijm/.ssh/id_rsa type -1
debug1: identity file C:\\Users\\eijm/.ssh/id_rsa-cert type -1
debug1: identity file C:\\Users\\eijm/.ssh/id_dsa type -1
debug1: identity file C:\\Users\\eijm/.ssh/id_dsa-cert type -1
debug1: identity file C:\\Users\\eijm/.ssh/id_ecdsa type -1
debug1: identity file C:\\Users\\eijm/.ssh/id_ecdsa-cert type -1
debug1: identity file C:\\Users\\eijm/.ssh/id_ed25519 type -1
debug1: identity file C:\\Users\\eijm/.ssh/id_ed25519-cert type -1
debug1: identity file C:\\Users\\eijm/.ssh/id_xmss type -1
debug1: identity file C:\\Users\\eijm/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_for_Windows_8.1
debug1: Remote protocol version 2.0, remote software version SSH
debug1: no match: SSH
debug2: fd 3 setting O_NONBLOCK
debug1: Authenticating to 12.22.124.66:22 as 'eijmto'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug2: local client KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,ext-info-c
debug2: host key algorithms: rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,rsa-sha2-512,rsa-sha2-256,ssh-rsa,ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ssh-ed25519-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519
debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: compression ctos: none,zlib@openssh.com,zlib
debug2: compression stoc: none,zlib@openssh.com,zlib
debug2: languages ctos:
debug2: languages stoc:
debug2: first_kex_follows 0
debug2: reserved 0
debug2: peer server KEXINIT proposal
debug2: KEX algorithms: diffie-hellman-group-exchange-sha256,diffie-hellman-group1-sha1,diffie-hellman-group14-sha1
debug2: host key algorithms: ssh-rsa
debug2: ciphers ctos: aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,3des-cbc,arcfour
debug2: ciphers stoc: aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,3des-cbc,arcfour
debug2: MACs ctos: hmac-md5,hmac-md5-96,hmac-sha1,hmac-sha1-96,hmac-sha2-256,hmac-sha2-512
debug2: MACs stoc: hmac-md5,hmac-md5-96,hmac-sha1,hmac-sha1-96,hmac-sha2-256,hmac-sha2-512
debug2: compression ctos: none
debug2: compression stoc: none
debug2: languages ctos:
debug2: languages stoc:
debug2: first_kex_follows 0
debug2: reserved 0
debug1: kex: algorithm: diffie-hellman-group-exchange-sha256
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: aes128-ctr MAC: hmac-sha2-256 compression: none
debug1: kex: client->server cipher: aes128-ctr MAC: hmac-sha2-256 compression: none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(2048<8192<8192) sent
debug1: got SSH2_MSG_KEX_DH_GEX_GROUP
debug2: bits set: 4078/8192
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: got SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: ssh-rsa SHA256:gj6tlHPgeFidgXKMwymWz55UiuGnFwFw38GwSToCQMk
debug1: Host '12.22.124.66' is known and matches the RSA host key.
debug1: Found key in C:\\Users\\eijm/.ssh/known_hosts:1
debug2: bits set: 4015/8192
debug2: set_newkeys: mode 1
debug1: rekey out after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug2: set_newkeys: mode 0
debug1: rekey in after 4294967296 blocks
debug1: pubkey_prepare: ssh_get_authentication_socket: No such file or directory
debug1: Will attempt key: C:\\Users\\eijm/.ssh/id_rsa
debug1: Will attempt key: C:\\Users\\eijm/.ssh/id_dsa
debug1: Will attempt key: C:\\Users\\eijm/.ssh/id_ecdsa
debug1: Will attempt key: C:\\Users\\eijm/.ssh/id_ed25519
debug1: Will attempt key: C:\\Users\\eijm/.ssh/id_xmss
debug2: pubkey_prepare: done
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Trying private key: C:\\Users\\eijm/.ssh/id_rsa
debug1: Trying private key: C:\\Users\\eijm/.ssh/id_dsa
debug1: Trying private key: C:\\Users\\eijm/.ssh/id_ecdsa
debug1: Trying private key: C:\\Users\\eijm/.ssh/id_ed25519
debug1: Trying private key: C:\\Users\\eijm/.ssh/id_xmss
debug2: we did not send a packet, disable method
debug1: Next authentication method: password
debug1: read_passphrase: can't open /dev/tty: No such file or directory
eijmto@12.22.124.66's password:
debug2: we sent a password packet, wait for reply
debug1: Authentication succeeded (password).
Authenticated to 12.22.124.66 ([12.22.124.66]:22).
debug1: channel 0: new [client-session]
debug2: channel 0: send open
debug1: Entering interactive session.
debug1: pledge: network
debug2: channel_input_open_confirmation: channel 0: callback start
debug2: fd 3 setting TCP_NODELAY
debug2: client_session2_setup: id 0
debug1: Sending command: /usr/bin/who
debug2: channel 0: request exec confirm 1
debug2: channel_input_open_confirmation: channel 0: callback done
debug2: channel 0: open confirm rwindow 0 rmax 32768
debug2: channel 0: rcvd adjust 2097152
debug2: channel_input_status_confirm: type 99 id 0
debug2: exec request accepted on channel 0
debug2: channel 0: rcvd eof
debug2: channel 0: output open -> drain
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug2: channel 0: rcvd close
debug2: channel 0: chan_shutdown_read (i0 o1 sock -1 wfd 4 efd 6 [write])
debug2: channel 0: input open -> closed
ansda pts/9        2020-05-13 09:28 (185.39.130.179)
moroka pts/13       2019-07-29 17:04 (185.39.130.181)
eijmto pts/15       2020-05-13 09:35 (172.30.170.1)
debug2: channel 0: obuf empty
debug2: channel 0: chan_shutdown_write (i3 o1 sock -1 wfd 5 efd 6 [write])
debug2: channel 0: output drain -> closed
debug2: channel 0: almost dead
debug2: channel 0: gc: notify user
debug2: channel 0: gc: user detached
debug2: channel 0: send close
debug2: channel 0: is dead
debug2: channel 0: garbage collecting
ion, nchannels 1
conds

Пожалуйста, дайте мне знать, что мне не хватает.

Версия Paramiko - 2.7.1

1 Ответ

0 голосов
/ 12 мая 2020

Использовать тайм-аут в exec_command ()

SSHClient.exec_command (command, bufsize = -1, timeout = None, get_pty = False) Выполнить команду на сервере S SH. Открывается новый .Channel, и запрошенная команда выполняется. Потоки ввода и вывода команды возвращаются как Python файловые объекты, представляющие stdin, stdout и stderr.

Параметры: command (str) - команда для выполнения bufsize (int) - интерпретируется аналогичным образом как встроенной функцией file () в Python timeout (int) - установить таймаут канала команды. См. Channel.settimeout.settimeout Возвращает:
stdin, stdout и stderr выполняющейся команды в виде трех кортежей

Вызывает исключение SSHException:

, если серверу не удается выполнить команда

...