Last active
January 16, 2017 10:00
-
-
Save Renkai/f23e915ac21ca4684049a8ee7a31718f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import getpass | |
import os | |
import pexpect | |
hosts = [''] | |
user = raw_input("type in the username:") | |
password = getpass.getpass('type in the password:') | |
expect_list = ['(yes/no)', 'password:'] | |
rsa_pub_file = open(os.path.expanduser('~/.ssh/id_rsa.pub')) | |
rsa_pub = '' | |
ppt = '\$ ' | |
for line in rsa_pub_file: | |
rsa_pub = rsa_pub + line | |
def store_key(s): | |
print 'store key' | |
s.sendline('mkdir ~/.ssh') | |
s.expect(ppt) | |
s.sendline('touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys') | |
s.expect(ppt) | |
s.sendline("echo '{0}' >> ~/.ssh/authorized_keys".format(rsa_pub)) | |
s.expect(ppt) | |
for host in hosts: | |
print "host " + host + " begin" | |
s = pexpect.spawn("ssh -p10022 {0}@{1}".format(user,host)) | |
#s.logfile = sys.stdout | |
idx = s.expect(['(yes/no)','password',ppt]) | |
if idx == 0: | |
s.sendline('yes') | |
s.sendline(password) | |
s.expect(user) | |
s.expect(ppt) | |
elif idx == 1: | |
s.sendline(password) | |
s.expect(user) | |
s.expect(ppt) | |
elif idx == 2: | |
print "no pass" | |
pass | |
print "loged in" | |
store_key(s) | |
s.sendline('sudo su data') | |
idx = s.expect(['password',ppt]) | |
if idx == 0: | |
s.sendline(password) | |
print 'send password' | |
s.expect('data') | |
s.expect(ppt) | |
print 'prompted' | |
elif idx == 1: | |
print 'no need to send password' | |
pass | |
print 'should send who am i' | |
s.sendline('whoami') | |
s.expect('data') | |
s.expect(ppt) | |
print 'switched to data' | |
store_key(s) | |
s.sendline('exit') | |
s.sendline('exit') | |
s.expect(pexpect.EOF) | |
print "host " + host + " end" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import pexpect | |
import getpass | |
hosts = [''] | |
password = getpass.getpass('type in the password:') | |
expect_list = ['(yes/no)', 'password:'] | |
for host in hosts: | |
print "for host " + host + " host" | |
p = pexpect.spawn("ssh-copy-id '{0} -p10022'".format(host)) | |
try: | |
while True: | |
idx = p.expect(expect_list) | |
print p.before + "matches" + expect_list[idx] | |
if idx == 0: | |
print "send yes" | |
p.sendline("yes") | |
elif idx == 1: | |
print "send password" | |
p.sendline(password) | |
except pexpect.TIMEOUT: | |
print "timeout\n" | |
except pexpect.EOF: | |
print "EOF\n" + p.before | |
print "for host " + host + " end" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment