Mar-13-2018, 04:51 AM
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:
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