Python Forum
Python Paramiko, continuing the interaction for a command in interactive shell
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

[inline]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*****[/inline]

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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python error handling Paramiko pythondiy 2 21,870 Aug-22-2017, 02:22 AM
Last Post: oldseven
  How to keep paramiko ssh session open after loggin in using python? MikeHill 3 13,443 Aug-15-2017, 06:55 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