Advertisement
Logos01

Untitled

Jun 27th, 2013
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import os
  4. import gnupg
  5. import paramiko
  6. import sys
  7. import time
  8.  
  9. decrypted_pass = ''
  10. chan = ''
  11. host = sys.argv[1]
  12. #userfile = sys.argv[3]
  13. user = sys.argv[2]
  14.  
  15. def decryptPass():
  16.     global decrypted_pass
  17.     home = os.getenv('HOME')
  18.     gpg = gnupg.GPG(gnupghome='%s/.gnupg/' % home)
  19.  
  20.     encrypted_pass = ''
  21.     for x in open('%s/.ssh/passtext.gpg' % home,'r').readlines():
  22.         encrypted_pass += x
  23.  
  24.     decrypted_pass = gpg.decrypt(encrypted_pass).data
  25.     decrypted_pass = decrypted_pass.strip('\n')
  26.  
  27. def establishPty():
  28.     global chan
  29.     ssh = paramiko.SSHClient()
  30.     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  31.     ssh.connect(host,username=user,password=decrypted_pass)
  32.     chan = ssh.invoke_shell()
  33.     while not chan.recv_ready():
  34.         print "Connecting..."
  35.         time.sleep(2)
  36.     print(chan.recv(1024))
  37.     chan.send('sudo su -\n')
  38.     time.sleep(1)
  39.     chan.send('%s\n' % decrypted_pass)
  40.     print(chan.recv(1024))
  41.     print "Sudo invocation command exit status was: %s" % (int(chan.exit_status_ready()))
  42.     while not chan.recv_ready():
  43.         time.sleep(2)
  44.     chan.send('pwd\n')
  45.     print(chan.recv(1024))
  46.     print(chan.recv(1024))
  47.     print "Exit status of last command was: %s" % (int(chan.exit_status_ready()))
  48.  
  49. def main():
  50.     decryptPass()
  51.     establishPty()
  52.  
  53. if __name__ == "__main__":
  54.     sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement