Guest User

Untitled

a guest
Jan 17th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import paramiko
  4. import cmd
  5.  
  6. class RunCommand(cmd.Cmd):
  7. """ Simple shell to run a command on the host """
  8.  
  9. prompt = 'ssh > '
  10.  
  11. def __init__(self):
  12. cmd.Cmd.__init__(self)
  13. self.hosts = []
  14. self.connections = []
  15.  
  16. def do_add_host(self, args):
  17. """add_host
  18. Add the host to the host list"""
  19. if args:
  20. self.hosts.append(args.split(','))
  21. else:
  22. print "usage: host "
  23.  
  24. def do_connect(self, args):
  25. """Connect to all hosts in the hosts list"""
  26. for host in self.hosts:
  27. client = paramiko.SSHClient()
  28. client.set_missing_host_key_policy(
  29. paramiko.AutoAddPolicy())
  30. client.connect(host[0],
  31. username=host[1],
  32. password=host[2])
  33. self.connections.append(client)
  34.  
  35. def do_run(self, command):
  36. """run
  37. Execute this command on all hosts in the list"""
  38. if command:
  39. for host, conn in zip(self.hosts, self.connections):
  40. stdin, stdout, stderr = conn.exec_command(command)
  41. stdin.close()
  42. for line in stdout.read().splitlines():
  43. print 'host: %s: %s' % (host[0], line)
  44. else:
  45. print "usage: run "
  46.  
  47. def do_close(self, args):
  48. for conn in self.connections:
  49. conn.close()
  50.  
  51. if __name__ == '__main__':
  52. RunCommand().cmdloop()
Add Comment
Please, Sign In to add comment