Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Paramiko, continuing the interaction for a command in interactive shell
#1
I have been trying to automate a task using python, in which I ssh to a remote server using paramiko, bring up the interactive shell and run the command.
The problem I'm facing is that the command I execute requires further more inputs to be given to it . Consider the below example, which happens on the cli, if the command is manually executed on the server

bash@:restore-config
Select the config to restore
a.conf b.conf.c.conf
>a.conf
Are you sure you want to proceed(Y/N)?
>Y
******the execution begins and completes by itself*****


The problem with my code is after the restore-config is executed, the paramiko channel gets closed without asking the input(in case of above example,"select the config to restore"),it displays restore-config aborted, how can I carry on the interaction?
My code is as follows:

import paramiko

ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
ssh.connect( '127.0.0.1',username='abcd',pass='1234' )

ishell=ssh.invoke_shell()

def run_cmd(cmd):
     stdin,stdout.stderr=ssh.exec_command(cmd)

     for l in stdout:
        print "stdout:%s" %l.strip()

     for l in stderr:
         print "Error:%s" %l.strip()

     ssh_stdin.write('a.conf')

      for l in stdout:
        print "stdout:%s" %l.strip()

      for l in stderr:
         print "Error:%s" %l.strip()

    ssh_stdin.write('Y')

        for l in stdout:
            print "stdout:%s" %l.strip()

        for l in stderr:
            print "Error:%s" %l.strip()


run_cmd(restore-config)
The output is similar as follows

Output:
stdout:Select the configuration file stdout:a.conf stdout:b.conf stdout:c.conf stdout:restore aborted
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Is the paramiko's API threading safe? bsxfun 0 192 May-18-2018, 02:56 PM
Last Post: bsxfun
  paramiko/ssh-matic not working from Windows Server 2012 R2 machine having Python34. pythonuser14 1 567 Oct-15-2017, 06:53 AM
Last Post: pythonuser14
  Python error handling Paramiko pythondiy 2 2,179 Aug-22-2017, 02:22 AM
Last Post: oldseven
  How to keep paramiko ssh session open after loggin in using python? MikeHill 3 1,836 Aug-15-2017, 06:55 PM
Last Post: nilamo
  SSH to a device using paramiko fails MikeHill 0 668 Aug-07-2017, 08:12 PM
Last Post: MikeHill
  Is this fast for Paramiko? gohanzdad 3 1,121 May-06-2017, 05:59 AM
Last Post: gohanzdad

Forum Jump:


Users browsing this thread: 1 Guest(s)