Python script for interactive command execution on remote node

1k views Asked by At

I am trying to write a python script (Python 2.7.12) for interactive command execution on remote node. I'm using "paramiko" to create session and non-interactive commands are running fine using that session. Below is the class I have for creating session and executing command :

from paramiko import client

class ssh:
    client = None

    def __init__(self, hostname, username, password,verbose=1):
    if verbose == 1 :
            print("connecting to server " + hostname)
        try :
            self.client = client.SSHClient()
            self.client.set_missing_host_key_policy(client.AutoAddPolicy())
            self.client.connect(hostname, username=username, password=password)
        except :
            print "Error in connecting to server " + hostname
            print "Terminating Further execution"
            exit()

    def sendCommand(self, command):   
        res =  {'rc': -1,
            'stdout': '',
            'stderr': ''
            }
        nbytes = 204800000000000000000

        if(self.client):
            try :
               out = self.client.exec_command(command)
            except:
               print "Can't execute Command"

            stdout = out[1]
            stderr = out[2]

            stdout_data = []
            stderr_data = []

            while True:
                data = stdout.channel.recv(nbytes)
                if len(data) == 0:
                    break
                stdout_data.append(data)
            stdout_data = "".join(stdout_data)

            while True:
                err_str = stderr.channel.recv_stderr(nbytes)
                if len(err_str) == 0:
                    break
                stderr_data.append(err_str)
            stderr_data = "".join(stderr_data)

            res['stdout'] = stdout_data.strip()
            res['stderr'] = stderr_data.strip()
            res['rc'] = stdout.channel.recv_exit_status()
            return res
        else:
            print("Connection not open")
        self.client.close()

I'm using the this class in script as follows :

ssh_obj = ssh(node, user, password, 0)
out = ssh_obj.sendCommand("some_command")
stdout = out['stdout']

Now, I want to send an interactive command using this class. The command asks for the user password which is already used to create the session. Is it possible using paramiko ?

If not, is there any other way to do it ? I'm open to any options/suggestions that can provide me the interactive command execution on remote node using python.

Answer :
Thanks to pynexj for the suggestion. I've used 'stdin' of paramiko and got issue resolved. Just added the following in the script :

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy() )
ssh.connect(hostname=node, username=user, password=password)

cmd = 'some_command'

stdin, stdout, stderr = ssh.exec_command(cmd)
stdin.write(password)
stdin.write('\n')
stdin.flush()

My command required the password only once. So, it solved my problem.

0

There are 0 answers