Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import os
- import os.path
- import getpass
- import argparse
- parser = argparse.ArgumentParser()
- #parser.add_argument('-l', '--login', action='store', help='username')
- #parser.add_argument('-p', '--port', action='store', default='22', help='port')
- parser.add_argument('-L', '--list', action='store', help='file list of IPs')
- parser.add_argument('-i', '--ip', action='store', nargs='+', metavar='host', help='ip')
- args = parser.parse_args()
- #if not args.login:
- # print("Use '-l' to specify user")
- # quit()
- if hasattr(args, "list"):
- # check if file exists and is readable
- if os.path.isfile(args.list) and os.access(args.list, os.R_OK):
- ips = [i for i in open(args.list, 'r').readlines()]
- user = input("Username: ")
- passwd = getpass.getpass('Password: ')
- for ip in ips:
- cmd = 'ssh-copy-id {0}@{1}'.format(user, ip)
- sshpass_cmd = 'sshpass -p {0} {1}'.format(passwd, cmd)
- os.system(sshpass_cmd)
- print("Key added: ", ip) # prints if successful
- else:
- print("I'm sorry but %s cannot be found or cannot be read" % (args.list))
- elif args.host:
- ip = args.host
- sshpass_cmd = 'sshpass -p {0} {1}'.format(passwd, cmd)
- os.system(sshpass_cmd)
- print("Key added: ", ip) # prints if successful
- else:
- print("No IP addresses or hostnames were given to run script.")
- quit()
Add Comment
Please, Sign In to add comment