Hi,
I'm new to python and Paramiko.
I want do do an SSH login to a Oracle SBC, which only supports keyboard interactive login.
I have found som code and modiified it, and i am able to log in according to the log.
But i cant find out how to send a command, and get the result back.
Please review my code :
How do i get the result from chan.exec_command(cmd)
stdin,stdout,stderr = chan.exec_command(cmd) is not working with channel
Here is my output
Here is my log file
/paramiko.log
DEB [20211203-12:36:06.932] thr=1 paramiko.transport: starting thread (client mode): 0xc50135d0L
DEB [20211203-12:36:06.932] thr=1 paramiko.transport: Local version/idstring: SSH-2.0-paramiko_2.1.1
DEB [20211203-12:36:06.943] thr=1 paramiko.transport: Remote version/idstring: SSH-2.0-OpenSSH_7.4
INF [20211203-12:36:06.943] thr=1 paramiko.transport: Connected (version 2.0, client OpenSSH_7.4)
DEB [20211203-12:36:06.950] thr=1 paramiko.transport: kex algos:[u'diffie-hellman-group-exchange-sha256'] server key:[u'ssh-rsa', u'rsa-sha2-512', u'rsa-sha2-256'] client encrypt:[u'aes256-gcm@openssh.com', u'aes128-gcm@openssh.com', u'aes256-ctr', u'aes192-ctr', u'aes128-ctr'] server encrypt:[u'aes256-gcm@openssh.com', u'aes128-gcm@openssh.com', u'aes256-ctr', u'aes192-ctr', u'aes128-ctr'] client mac:[u'hmac-sha2-256'] server mac:[u'hmac-sha2-256'] client compress:[u'none', u'zlib@openssh.com'] server compress:[u'none', u'zlib@openssh.com'] client lang:[u''] server lang:[u''] kex follows?False
DEB [20211203-12:36:06.950] thr=1 paramiko.transport: Kex agreed: diffie-hellman-group-exchange-sha256
DEB [20211203-12:36:06.951] thr=1 paramiko.transport: Cipher agreed: aes128-ctr
DEB [20211203-12:36:06.951] thr=1 paramiko.transport: MAC agreed: hmac-sha2-256
DEB [20211203-12:36:06.951] thr=1 paramiko.transport: Compression agreed: none
DEB [20211203-12:36:07.022] thr=1 paramiko.transport: Got server p (8192 bits)
DEB [20211203-12:36:10.614] thr=1 paramiko.transport: kex engine KexGexSHA256 specified hash_algo <built-in function openssl_sha256>
DEB [20211203-12:36:10.616] thr=1 paramiko.transport: Switch to new keys ...
DEB [20211203-12:36:10.670] thr=1 paramiko.transport: userauth is OK
INF [20211203-12:36:10.996] thr=1 paramiko.transport: Authentication (keyboard-interactive) successful!
DEB [20211203-12:36:11.026] thr=2 paramiko.transport: [chan 0] Max packet in: 32768 bytes
DEB [20211203-12:36:11.035] thr=1 paramiko.transport: Received global request "hostkeys-00@openssh.com"
DEB [20211203-12:36:11.035] thr=1 paramiko.transport: Rejecting "hostkeys-00@openssh.com" global request from server.
DEB [20211203-12:36:11.082] thr=1 paramiko.transport: [chan 0] Max packet out: 32768 bytes
DEB [20211203-12:36:11.082] thr=1 paramiko.transport: Secsh channel 0 opened.
DEB [20211203-12:36:11.099] thr=1 paramiko.transport: [chan 0] Sesch channel 0 request ok
DEB [20211203-12:36:11.101] thr=1 paramiko.transport: [chan 0] EOF received (0)
DEB [20211203-12:36:11.102] thr=1 paramiko.transport: [chan 0] EOF sent (0)
DEB [20211203-12:36:13.206] thr=1 paramiko.transport: EOF in transport thread
Thanks in advance,
Peter
I'm new to python and Paramiko.
I want do do an SSH login to a Oracle SBC, which only supports keyboard interactive login.
I have found som code and modiified it, and i am able to log in according to the log.
But i cant find out how to send a command, and get the result back.
Please review my code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
#!/usr/bin/env python import paramiko #Provides SSH functionality import getpass #Allows for secure prompting and collection of the user password import os #Used to setup the Paramiko log file import logging #Used to setup the Paramiko log file import socket #This method requires that we create our own socket import time #Time #Global variables are used to store these data because they're sent to the server by a callback user = "user" pw = "abc123" host = "sbcdkxxx.yyy.net" cmd = "sh interfaces" def inter_handler(title, instructions, prompt_list): """ inter_handler: the callback for paramiko.transport.auth_interactive The prototype for this function is defined by Paramiko, so all of the arguments need to be there, even though we don't use 'title' or 'instructions'. The function is expected to return a tuple of data containing the responses to the provided prompts. Experimental results suggests that there will be one call of this function per prompt, but the mechanism allows for multiple prompts to be sent at once, so it's best to assume that that can happen. Since tuples can't really be built on the fly, the responses are collected in a list which is then converted to a tuple when it's time to return a value. Experiments suggest that the username prompt never happens. This makes sense, but the Username prompt is included here just in case. """ resp = [] #Initialize the response container #Walk the list of prompts that the server sent that we need to answer for pr in prompt_list: #str() used to to make sure that we're dealing with a string rather than a unicode string #strip() used to get rid of any padding spaces sent by the server if str (pr[ 0 ]).strip() = = "Username:" : resp.append(user) elif str (pr[ 0 ]).strip() = = "Password:" : resp.append(pw) return tuple (resp) #Convert the response list to a tuple and return it #Main Entry Point if __name__ = = "__main__" : #Setup Paramiko logging; this is useful for troubleshooting paramiko.util.log_to_file(os.path.expanduser( '~/paramiko.log' ), logging.DEBUG) #Create a socket and connect it to port 22 on the host sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((host, 22 )) #Make a Paramiko Transport object using the socket ts = paramiko.Transport(sock) #Tell Paramiko that the Transport is going to be used as a client ts.start_client(timeout = 10 ) #Begin authentication; note that the username and callback are passed ts.auth_interactive(user, inter_handler) #Opening a session creates a channel along the socket to the server chan = ts.open_session(timeout = 10 ) # Check print ( 'Connection authenticated = ' + str (ts.is_authenticated())) print ( 'Send_ready = ' + str (chan.send_ready())) print ( 'Transport = ' + str (chan.get_transport())) #Now the channel can be used to execute commands out = chan.exec_command(cmd) |
stdin,stdout,stderr = chan.exec_command(cmd) is not working with channel
Here is my output
1 2 3 4 5 6 7 |
python sbc_login.py Connection authenticated = True Send_ready = False Transport = <paramiko.Transport at 0xc66945d0L (cipher aes128 - ctr, 128 bits) (active; 1 open channel(s))> before command after command out = None |
/paramiko.log
DEB [20211203-12:36:06.932] thr=1 paramiko.transport: starting thread (client mode): 0xc50135d0L
DEB [20211203-12:36:06.932] thr=1 paramiko.transport: Local version/idstring: SSH-2.0-paramiko_2.1.1
DEB [20211203-12:36:06.943] thr=1 paramiko.transport: Remote version/idstring: SSH-2.0-OpenSSH_7.4
INF [20211203-12:36:06.943] thr=1 paramiko.transport: Connected (version 2.0, client OpenSSH_7.4)
DEB [20211203-12:36:06.950] thr=1 paramiko.transport: kex algos:[u'diffie-hellman-group-exchange-sha256'] server key:[u'ssh-rsa', u'rsa-sha2-512', u'rsa-sha2-256'] client encrypt:[u'aes256-gcm@openssh.com', u'aes128-gcm@openssh.com', u'aes256-ctr', u'aes192-ctr', u'aes128-ctr'] server encrypt:[u'aes256-gcm@openssh.com', u'aes128-gcm@openssh.com', u'aes256-ctr', u'aes192-ctr', u'aes128-ctr'] client mac:[u'hmac-sha2-256'] server mac:[u'hmac-sha2-256'] client compress:[u'none', u'zlib@openssh.com'] server compress:[u'none', u'zlib@openssh.com'] client lang:[u''] server lang:[u''] kex follows?False
DEB [20211203-12:36:06.950] thr=1 paramiko.transport: Kex agreed: diffie-hellman-group-exchange-sha256
DEB [20211203-12:36:06.951] thr=1 paramiko.transport: Cipher agreed: aes128-ctr
DEB [20211203-12:36:06.951] thr=1 paramiko.transport: MAC agreed: hmac-sha2-256
DEB [20211203-12:36:06.951] thr=1 paramiko.transport: Compression agreed: none
DEB [20211203-12:36:07.022] thr=1 paramiko.transport: Got server p (8192 bits)
DEB [20211203-12:36:10.614] thr=1 paramiko.transport: kex engine KexGexSHA256 specified hash_algo <built-in function openssl_sha256>
DEB [20211203-12:36:10.616] thr=1 paramiko.transport: Switch to new keys ...
DEB [20211203-12:36:10.670] thr=1 paramiko.transport: userauth is OK
INF [20211203-12:36:10.996] thr=1 paramiko.transport: Authentication (keyboard-interactive) successful!
DEB [20211203-12:36:11.026] thr=2 paramiko.transport: [chan 0] Max packet in: 32768 bytes
DEB [20211203-12:36:11.035] thr=1 paramiko.transport: Received global request "hostkeys-00@openssh.com"
DEB [20211203-12:36:11.035] thr=1 paramiko.transport: Rejecting "hostkeys-00@openssh.com" global request from server.
DEB [20211203-12:36:11.082] thr=1 paramiko.transport: [chan 0] Max packet out: 32768 bytes
DEB [20211203-12:36:11.082] thr=1 paramiko.transport: Secsh channel 0 opened.
DEB [20211203-12:36:11.099] thr=1 paramiko.transport: [chan 0] Sesch channel 0 request ok
DEB [20211203-12:36:11.101] thr=1 paramiko.transport: [chan 0] EOF received (0)
DEB [20211203-12:36:11.102] thr=1 paramiko.transport: [chan 0] EOF sent (0)
DEB [20211203-12:36:13.206] thr=1 paramiko.transport: EOF in transport thread
Thanks in advance,
Peter