Python Forum
Output from Paramiko exec_command from channel
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Output from Paramiko exec_command from channel
#1
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 :


#!/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)
 
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
 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
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'[email protected]', u'[email protected]', u'aes256-ctr', u'aes192-ctr', u'aes128-ctr'] server encrypt:[u'[email protected]', u'[email protected]', 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'[email protected]'] server compress:[u'none', u'[email protected]'] 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 "[email protected]"
DEB [20211203-12:36:11.035] thr=1 paramiko.transport: Rejecting "[email protected]" 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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to check if someone send a message in a discord channel? Zerolysimin 1 785 Nov-06-2022, 11:10 AM
Last Post: Larz60+
  Not able to add multiple channel IDs to python bot flaykez 3 86,595 Oct-18-2021, 01:26 AM
Last Post: CyKlop
  Auto detect threshold for skin detection w/ HSV channel davlovsky 1 2,225 Oct-13-2019, 01:45 AM
Last Post: Larz60+
  pika channel.start_consuming() is not running sundar 0 2,422 Mar-20-2019, 06:28 AM
Last Post: sundar
  How to change TV channel with Python arsenal58 7 6,591 Nov-19-2018, 01:11 AM
Last Post: Larz60+
  Paramiko output printing issue anna 3 16,067 Feb-06-2018, 08:34 AM
Last Post: anna
  AttributeError: type object 'MyClass' has no attribute 'channel' chris0147 2 8,754 Sep-29-2017, 06:16 PM
Last Post: chris0147
  ORA-03113: end-of-file on communication channel Python Cx_Oracle akansha 3 7,575 Nov-17-2016, 08:50 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020