Advertisement
jvjfranca

SSH Python

May 14th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | None | 0 0
  1. from pexpect import pxssh
  2. import getpass
  3.  
  4.  
  5. def connect(hostname, username, password, port):
  6.     s = pxssh.pxssh()
  7.     if port:
  8.         s.login(hostname, username, password, port)
  9.     else:
  10.         s.login(hostname, username, password)
  11.     return s
  12.  
  13.  
  14. def enviar_comando(s, comando):
  15.     s.sendline(comando)  # run a command
  16.     s.prompt()  # match the prompt
  17.     print(s.before)  # print everything before the prompt.
  18.  
  19.  
  20. def disconnect(s):
  21.     print("[+] Saindo do console SSH")
  22.     s.logout()
  23.  
  24.  
  25. def main():
  26.     try:
  27.         hostname = raw_input('hostname: ')
  28.         username = raw_input('username: ')
  29.         password = getpass.getpass('password: ')
  30.         port = ('port='+raw_input('port: '))
  31.         s = connect(hostname, username, password, port)
  32.         print "[!] Digite q ou Q para sair do Shell Remoto"
  33.         shell = raw_input(">")
  34.         while True:
  35.             enviar_comando(s, shell)
  36.             print "[!] Digite q ou Q para sair do Shell Remoto"
  37.             shell = raw_input(">")
  38.             if shell == 'q' or shell == 'Q':
  39.                 break
  40.         disconnect(s)
  41.     except Exception as e:
  42.         print("pxssh failed on login.")
  43.         print(e)
  44.  
  45. if __name__ == '__main__':
  46.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement