Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import paramiko
- cmd_sudo = """sudo -s su"""
- def get_ssh_client():
- """
- creer un client SSH
- Exemple:
- --------
- >>> from ssh_client import get_ssh_client
- >>> ssh, chan = get_client()
- >>> chan.send('ls \n')
- >>> resp = chan.recv(9999).decode()
- >>> sftp = ssh.open_sftp()
- >>> sftp.get('file.txt', 'file.txt')
- """
- ssh = paramiko.SSHClient()
- time.sleep(1)
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- time.sleep(1)
- ssh.connect(hostname='127.0.0.1', username='username', password='password')
- print('connexion reussie : ', 'url')
- time.sleep(1)
- chan = ssh.invoke_shell()
- time.sleep(1)
- chan.send(cmd_sudo + '\n')
- time.sleep(1)
- resp = chan.recv(9999).decode()
- time.sleep(1)
- if resp.endswith('# '):
- # on est en Sudo / pas la peine d'envoyer le mot de passe une 2 e fois
- pass
- else:
- # renvoyer le mot de passe
- #chan.send(cmd_sudo + '\n')
- time.sleep(3)
- chan.send('password' + '\n')
- resp = chan.recv(9999).decode()
- return ssh, chan
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement