Guest User

Untitled

a guest
Mar 15th, 2018
107
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/python3
  2.  
  3. import os
  4. import os.path
  5. import getpass
  6. import argparse
  7.  
  8. parser = argparse.ArgumentParser()
  9. #parser.add_argument('-l', '--login', action='store', help='username')
  10. #parser.add_argument('-p', '--port', action='store', default='22', help='port')
  11. parser.add_argument('-L', '--list', action='store', help='file list of IPs')
  12. parser.add_argument('-i', '--ip', action='store', nargs='+', metavar='host', help='ip')
  13.  
  14. args = parser.parse_args()
  15. #if not args.login:
  16. # print("Use '-l' to specify user")
  17. # quit()
  18.  
  19. if hasattr(args, "list"):
  20. # check if file exists and is readable
  21. if os.path.isfile(args.list) and os.access(args.list, os.R_OK):
  22. ips = [i for i in open(args.list, 'r').readlines()]
  23. user = input("Username: ")
  24. passwd = getpass.getpass('Password: ')
  25.  
  26. for ip in ips:
  27. cmd = 'ssh-copy-id {0}@{1}'.format(user, ip)
  28. sshpass_cmd = 'sshpass -p {0} {1}'.format(passwd, cmd)
  29. os.system(sshpass_cmd)
  30. print("Key added: ", ip) # prints if successful
  31. else:
  32. print("I'm sorry but %s cannot be found or cannot be read" % (args.list))
  33.  
  34. elif args.host:
  35. ip = args.host
  36. sshpass_cmd = 'sshpass -p {0} {1}'.format(passwd, cmd)
  37. os.system(sshpass_cmd)
  38. print("Key added: ", ip) # prints if successful
  39. else:
  40. print("No IP addresses or hostnames were given to run script.")
  41. quit()
Add Comment
Please, Sign In to add comment