Advertisement
Guest User

Untitled

a guest
Mar 21st, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. import secure
  4. import pexpect
  5.  
  6. # the file containing the list of servers to log into
  7. input_file = "script_list"
  8.  
  9. # The login creds
  10. user = secure.USER
  11. password= secure.PASSWORD
  12.  
  13. def ssh_command (user, host, password, command):
  14.  
  15. """This runs a command on the remote host."""
  16. print " I am logging into", host
  17.  
  18. ssh_newkey = 'Are you sure you want to continue connecting'
  19. child = pexpect.spawn('ssh -l %s %s %s'%(user, host, command))
  20. i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: '])
  21. if i == 0: # Timeout
  22. print('ERROR!')
  23. print('SSH could not login. Here is what SSH said:')
  24. print(child.before, child.after)
  25. return None
  26. if i == 1: # SSH does not have the public key. Just accept it.
  27. child.sendline ('yes')
  28. child.expect ('password: ')
  29. i = child.expect([pexpect.TIMEOUT, 'password: '])
  30. if i == 0: # Timeout
  31. print('9ERROR!')
  32. print('SSH could not login. Here is what SSH said:')
  33. print(child.before, child.after)
  34. return None
  35. child.sendline(password)
  36. return child
  37.  
  38. def main():
  39.  
  40. f = open(input_file, "r")
  41. server_list = f.readlines()
  42.  
  43. for server in server_list:
  44. child = ssh_command (user, server, password, 'script.sh')
  45. child.expect(pexpect.EOF)
  46. output = child.before
  47.  
  48. if __name__ == "__main__":
  49. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement