Skip to content

Instantly share code, notes, and snippets.

@jjam3774
Created January 2, 2014 16:48
Show Gist options
  • Save jjam3774/39d06f189a95faac4d43 to your computer and use it in GitHub Desktop.
Save jjam3774/39d06f189a95faac4d43 to your computer and use it in GitHub Desktop.
Another Version of the Automated Keygen Python Script..
#!/usr/bin/python
import time
import pexpect
import getpass
def create_key():
gen = pexpect.spawn('ssh-keygen -t rsa', timeout=None)
gen.expect('Enter file in')
gen.sendline('')
gen.expect('Enter passphrase')
gen.sendline('')
gen.expect('Enter')
gen.sendline('')
def copy_key():
passwd = getpass.getpass('Password..: ')
print("Checking hostfile...")
for i in open('hostfile').readlines():
print("Copying to {0}...".format(i.strip()))
id = pexpect.spawn('ssh-copy-id -i ecom@{0}'.format(i.strip()), timeout=None)
out = id.expect(['Are you sure', 'password'])
try:
if out == 0:
print id.sendline('yes')
print(out)
try:
id.sendline(passwd.strip())
id.sendline(passwd.strip())
fin = id.expect(['$','#','%'])
if fin == 0:
print("Key has been uploaded..")
except:
print("There might be an issue with the passwd....")
else:
print(out)
try:
id.sendline(passwd.strip())
id.sendline(passwd.strip())
fin = id.expect(['$','#','%'])
if fin == 0 or fin == 1 or fin == 2:
print("Key has been uploaded to all servers..")
except:
print("There might be and issue with the passwd...")
except:
print("There has been an error with copying key to the server. Please verify the passwd...")
time.sleep(1)
print("Copy is complete...")
answer = raw_input("Do you need to create a key? ")
if answer == "y":
create_key()
print("Copying key to the remote servers...")
copy_key()
else:
print("Copying key to the remote servers...")
copy_key()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment