Advertisement
Guest User

Untitled

a guest
May 9th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.91 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. from __future__ import with_statement
  4.  
  5. import sys, os, hashlib, base64
  6.  
  7. from getpass import getpass
  8. from contextlib import contextmanager
  9.  
  10. import paramiko
  11.  
  12. lampda = base64.decodestring
  13.    
  14. @contextmanager
  15. def create_ssh(host):
  16.     ssh = paramiko.SSHClient()
  17.     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  18.     try :
  19.         print("attempt to create a connection using a private key...")    
  20.         ssh.connect("147.210.18.1", username="boubekki", look_for_keys=True)
  21.         print("connected")
  22.         yield ssh
  23.     except :
  24.         print("no private key available")
  25.         username = raw_input("username: ")
  26.         password = getpass()    
  27.         ssh = paramiko.SSHClient()
  28.         ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  29.         print("a private key will be used for futur connexion...")
  30.        
  31.         try:
  32.             print("creating connection")
  33.             ssh.connect(host, username=username, password=password)
  34.             print("connected")
  35.             yield ssh
  36.         except :
  37.             print("fatal error")
  38.         finally:
  39.             print("closing connection")
  40.             ssh.close()
  41.             print("closed")
  42.  
  43. def slpr(path, file_name):
  44.     with create_ssh("147.210.18.1") as ssh:
  45.         sftp = ssh.open_sftp()
  46.         sftp.put(os.path.join(path, file_name),"/tmp/"+file_name)
  47.         print("file uploaded to %s" % "/tmp/"+file_name)
  48.        
  49.         stdin, stdout, stderr = ssh.exec_command("imp -pz -2 -rv /tmp/"+file_name)
  50.         print(stdout.read())
  51.         print(stderr.read())
  52.        
  53. if __name__ == "__main__":
  54.     if (len(sys.argv)<1)
  55.         raise Exception(lampda("SWwgbWFucXVlIHVuIGFyZ3VtZW50"))
  56.  
  57.     file_path = os.path.abspath(sys.argv[1])
  58.  
  59.     if not (os.path.isfile(file_path)):
  60.         raise Exception("file does not exist")
  61.  
  62.     path, file_name = os.path.split(file_path)
  63.     slpr(path, file_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement