0

I am taking backup of different network devices daily using paramiko. Each device takes different time interval to login in to the device and also take different time to complete each command. So if I keep time.sleep() with a static time value sometimes it is more time to wait and sometimes it is very less to wait. So how I can I write a program to wait until the first command execute and go for next one, irrespective time.sleep value.

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, port='22', username=usr_name, password=pwd)
connection = ssh.invoke_shell()
connection.send("\n")
time.sleep(14)
connection.send("enable\n")
time.sleep(3)
connection.send("%s\n" %(pwd))
time.sleep(1)
connection.send("terminal length 0\n")
time.sleep(1)
connection.send("sh int status | ex (trunk|routed)\n")
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
  • 1
    you can use `connection.exit_status_ready()` if I'm not mistaken to check if another command can be passed. Thanks to that you won't need to use `time.sleep()` at all. – Shan Oct 10 '18 at 06:45
  • @Shan `exit_status_ready` won't help here as it signals that the channel (shell in this case) has been closed. And it wasn't, otherwise OP would not be able to send another command to the same channel. `exit_status_ready` helps with "exec" channel only. But I'm afraid that OP needs to send all the commands in the same shell instance, so "exec" command is not usable. – Martin Prikryl Oct 10 '18 at 06:59
  • Actually the question is hardly about Python or programming at all. OP is connecting to some proprietary system. Without a good knowledge of that specific system (which OP didn't tell us anything about), the question cannot be answered. SSH "shell" channel is a black box with an input and output. No API on the client side can help. – Martin Prikryl Oct 10 '18 at 07:00
  • The question about `time.sleep` is [XY problem](https://meta.stackexchange.com/q/66377/218578). – Martin Prikryl Oct 10 '18 at 07:03
  • See [Execute multiple dependent commands individually with Paramiko and find out when each command finishes](https://stackoverflow.com/q/50962485/850848) - Not that it helps, as the question is about Linux server, while I believe that your "server" is Cisco. But it gives you the idea. – Martin Prikryl Oct 10 '18 at 07:45

0 Answers0