Paramiko session times out, but i need to execute a lot of commands

Miri Adj

I'm working on a script (python 2.7) that is wotking with a remote device running Cisco IOS, so I need to execute a lot of commands through ssh. Few commands have no output and some of them have, and I want to recieve the output. It goes something like this:

import paramiko
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(self._ip, port=22, username=username, password=password
stdin, stdout, stderr = ssh.exec_command('command with no output')
stdin, stdout, stderr = ssh.exec_command('command with no output')
stdin, stdout, stderr = ssh.exec_command('command with output')
sh_ver = stdout.readlines()

The thing is exec_command is causes the channel to close and it can’t be reused, but it's not possible for me to open a new channel in order to execute another command, because this is a session of commands that in the end I need to get the output.

I've tried to execute the commands this way as well:

stdin, stdout, stderr = ssh.exec_command('''
command
command
command
''')
output = stdout.readlines()

but this way, output is empty. And even if it would'nt, I need to perform a few checks on the output and then continue the session where I stopped.

So what do I need? A way to manage this ssh connection without closing it or starting a new one, and to easily recieve the output from the command.

Thanks in advance, Miri. :)

pynexj

I think what you need is invoke_shell(). For example:

ssh = paramiko.SSHClient()
... ...
chan = ssh.invoke_shell() # starts an interactive session

chan.send('command 1\r')
output = chan.recv()

chan.send('command 2\r')
output = chan.recv()
... ...

The Channel has many other methods. You can refer to the document for more details.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is it true that some times, batch files need a break to execute commands properly?

From Dev

Execute remote commands as root using Python and paramiko

From Dev

I need to execute a AsynckTask object 5 times with an interval of 2 minutes

From Dev

PDO Execute Statement Times Out

From Dev

PDO Execute Statement Times Out

From Dev

Execute several commands on Cisco switch via Paramiko - improve performance

From Dev

Execute two commands in a row using one SSH object via paramiko

From Dev

Uploading large files to proftpd through paramiko times out

From Dev

Better way to execute a lot of SQL commands in Visual Basic?

From Dev

Azure powershell session instantly times out

From Dev

Azure powershell session instantly times out

From Dev

Quickly execute commands without running out of memory

From Dev

stdout output is not saved to list when I execute command with paramiko

From Dev

Will it cause performance issues if I call tableView.reloadData a lot of times?

From Dev

paramiko multiple commands as argument

From Dev

How to retain the session with paramiko

From Dev

How can I execute commands on startup in awesome?

From Dev

Can I execute multiple lines sudo commands?

From Dev

Can I execute commands written in a textdocument in the terminal

From Dev

How can I execute commands in a text file?

From Dev

I need an automatic way of making a lot of numbered folders

From Dev

How I can execute asynctask several times with selectable times?

From Dev

How to save $_SESSION variable when it times out on the server

From Dev

Get users current page when session times out

From Dev

500 error when asp.net session variable times out

From Dev

How to save $_SESSION variable when it times out on the server

From Dev

Session times out although the user has been typing in the form

From Dev

Why I need times in closing files in flow

From Dev

Telnet BASH script - Some commands are running out of telnet session

Related Related

  1. 1

    Is it true that some times, batch files need a break to execute commands properly?

  2. 2

    Execute remote commands as root using Python and paramiko

  3. 3

    I need to execute a AsynckTask object 5 times with an interval of 2 minutes

  4. 4

    PDO Execute Statement Times Out

  5. 5

    PDO Execute Statement Times Out

  6. 6

    Execute several commands on Cisco switch via Paramiko - improve performance

  7. 7

    Execute two commands in a row using one SSH object via paramiko

  8. 8

    Uploading large files to proftpd through paramiko times out

  9. 9

    Better way to execute a lot of SQL commands in Visual Basic?

  10. 10

    Azure powershell session instantly times out

  11. 11

    Azure powershell session instantly times out

  12. 12

    Quickly execute commands without running out of memory

  13. 13

    stdout output is not saved to list when I execute command with paramiko

  14. 14

    Will it cause performance issues if I call tableView.reloadData a lot of times?

  15. 15

    paramiko multiple commands as argument

  16. 16

    How to retain the session with paramiko

  17. 17

    How can I execute commands on startup in awesome?

  18. 18

    Can I execute multiple lines sudo commands?

  19. 19

    Can I execute commands written in a textdocument in the terminal

  20. 20

    How can I execute commands in a text file?

  21. 21

    I need an automatic way of making a lot of numbered folders

  22. 22

    How I can execute asynctask several times with selectable times?

  23. 23

    How to save $_SESSION variable when it times out on the server

  24. 24

    Get users current page when session times out

  25. 25

    500 error when asp.net session variable times out

  26. 26

    How to save $_SESSION variable when it times out on the server

  27. 27

    Session times out although the user has been typing in the form

  28. 28

    Why I need times in closing files in flow

  29. 29

    Telnet BASH script - Some commands are running out of telnet session

HotTag

Archive