Skip to content

Instantly share code, notes, and snippets.

@IAmSurajBobade
Last active August 5, 2023 20:42
Show Gist options
  • Save IAmSurajBobade/c7030fbf901b7f6f1e3818e1a54eda6b to your computer and use it in GitHub Desktop.
Save IAmSurajBobade/c7030fbf901b7f6f1e3818e1a54eda6b to your computer and use it in GitHub Desktop.
python script to Execute multiple commands over SSH
Python script to perform multiple commands sequentially over SSH in a single connection
Known issue:
Execution of commands goes on one after another, even though one/many intermediate commands fails
(NOT recommended, if your commands and dependant on previous commands execution)
Kindly share your thoughts on this script.
Link to answer: https://stackoverflow.com/a/43071039/5243762
Thank you!
#!/usr/bin/python
import os
import sys
import select
import paramiko
import time
class Commands:
def __init__(self, retry_time=0):
self.retry_time = retry_time
pass
def run_cmd(self, host_ip, cmd_list):
i = 0
while True:
# print("Trying to connect to %s (%i/%i)" % (self.host, i, self.retry_time))
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host_ip)
break
except paramiko.AuthenticationException:
print("Authentication failed when connecting to %s" % host_ip)
sys.exit(1)
except:
print("Could not SSH to %s, waiting for it to start" % host_ip)
i += 1
time.sleep(2)
# If we could not connect within time limit
if i >= self.retry_time:
print("Could not connect to %s. Giving up" % host_ip)
sys.exit(1)
# After connection is successful
# Send the command
for command in cmd_list:
# print command
print "> " + command
# execute commands
stdin, stdout, stderr = ssh.exec_command(command)
# TODO() : if an error is thrown, stop further fules
# Wait for the command to terminate
while not stdout.channel.exit_status_ready():
# Only print data if there is data to read in the channel
if stdout.channel.recv_ready():
rl, wl, xl = select.select([ stdout.channel ], [ ], [ ], 0.0)
if len(rl) > 0:
tmp = stdout.channel.recv(1024)
output = tmp.decode()
print output
# Close SSH connection
ssh.close()
return
def main(argv=None):
if argv is None:
print "arguments expected"
else:
# argv = {'<ip_address>', <list_of_commands>}
# mytest = Commands()
# mytest.run_cmd(host_ip=argv[0], cmd_list=argv[ 1 ])
# mylist.run_cmd_local(cmd_list=argv[1])
print "This method is not meant to be used directly"
return
if __name__ == "__main__":
main(sys.argv[ 1: ])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment