Advertisement
Guest User

Untitled

a guest
Oct 8th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3.  
  4. import sys
  5. import argparse
  6. import paramiko
  7.  
  8.  
  9. def parse_args():
  10. parser = argparse.ArgumentParser(description="This script will ")
  11. parser.add_argument("-i", "--ip", action="store", dest="remote_ip", help="ip address of remote host", required=True)
  12. parser.add_argument("-u", "--username", action="store", dest="username", help="username of remote host",
  13. required=True)
  14. parser.add_argument("-p", "--password", action="store", dest="password", help="password of remote host",
  15. required=True)
  16. parser.add_argument("-c", "--command", action="store", dest="command", help="command to be executed on remote host",
  17. required=True)
  18. args = parser.parse_args()
  19.  
  20. return args.remote_ip, args.username, args.password, args.command
  21.  
  22.  
  23. def execute_remote_command(remote_ip, username, password, cmd):
  24. client = paramiko.SSHClient()
  25. client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  26. client.connect(hostname=remote_ip, username=username, password=password)
  27.  
  28. print "Executing command: \"" + cmd + "\" on " + username + "@" + remote_ip
  29. try:
  30. stdin, stdout, stderr = client.exec_command(cmd)
  31. except paramiko.SSHException as err:
  32. print err
  33. return 1
  34. print stdout.read()
  35. client.close()
  36.  
  37.  
  38. def main():
  39. remote_ips, username, password, cmd = parse_args()
  40. remote_ip_list = remote_ips.split(',')
  41. for ip in remote_ip_list:
  42. execute_remote_command(ip, username, password, cmd)
  43.  
  44.  
  45. if __name__ == '__main__':
  46. sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement