Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. import time
  2. import paramiko
  3.  
  4. cmd_sudo = """sudo -s su"""
  5.  
  6. def get_ssh_client():
  7. """
  8. creer un client SSH
  9.  
  10. Exemple:
  11. --------
  12. >>> from ssh_client import get_ssh_client
  13. >>> ssh, chan = get_client()
  14. >>> chan.send('ls \n')
  15. >>> resp = chan.recv(9999).decode()
  16. >>> sftp = ssh.open_sftp()
  17. >>> sftp.get('file.txt', 'file.txt')
  18. """
  19. ssh = paramiko.SSHClient()
  20. time.sleep(1)
  21. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  22. time.sleep(1)
  23. ssh.connect(hostname='127.0.0.1', username='username', password='password')
  24. print('connexion reussie : ', 'url')
  25. time.sleep(1)
  26. chan = ssh.invoke_shell()
  27. time.sleep(1)
  28. chan.send(cmd_sudo + '\n')
  29. time.sleep(1)
  30. resp = chan.recv(9999).decode()
  31. time.sleep(1)
  32. if resp.endswith('# '):
  33. # on est en Sudo / pas la peine d'envoyer le mot de passe une 2 e fois
  34. pass
  35. else:
  36. # renvoyer le mot de passe
  37. #chan.send(cmd_sudo + '\n')
  38. time.sleep(3)
  39. chan.send('password' + '\n')
  40. resp = chan.recv(9999).decode()
  41.  
  42. return ssh, chan
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement