python paramiko module use

1. Install the paramiko module

$ pip install paramiko
  • 1

2. Connect to the server and execute the command

import threading,paramiko
url_list = ['192.168.88.130',
            '192.168.88.131',
            '192.168.88.132',
            '192.168.88.133',
            ]
def private_key_connect(ssh_ip):
    '''Connect based on public key
    Connect based on private key string
    from io import StringIO
    key_file = open('/root/.ssh/id_rsa',r).read()
    private_key = paramiko.RSAKey(file_obj=StringIO(key_file))
    transport = paramiko.Transport((ssh_ip, 22))
    transport.connect(username='root', pkey=private_key)
    ssh = paramiko.SSHClient()
    ssh._transport = transport
    stdin,stdout,stderr = ssh.exec_command('uptime')
    '''
    private_key = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa')
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=ssh_ip,port=22,username='root',password=private_key)   #连接
    stdin, stdout, stderr = ssh.exec_command('uptime') #Execute command
    res,err = stdout.read(),stderr.read()
    result = res if res else err
    print(ssh_ip,result.decode())
    ssh.close()

def user_passwd_connect(ssh_ip):
    '''Connect based on username and password
    SSHClient encapsulates transport
    transport = paramiko.Transport((ssh_ip, 22))
    transport.connect(username='root', password='123456')
    ssh = paramiko.SSHClient()
    ssh._transport = transport
    stdin,stdout,stderr = ssh.exec_command('uptime')
    '''
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=ssh_ip,port=22,username='root',password='123456')
    stdin,stdout,stderr = ssh.exec_command('uptime')
    res,err = stdout.read(),stderr.read()
    result = res if res else err
    print(ssh_ip,result.decode())
    ssh.close()

if __name__ == '__main__':
    for ip in url_list:
        t = threading.Thread(target=private_key_connect,args=(ip,))
        # t = threading.Thread(target=user_passwd_connect,args=(ip,))
        t.start()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

3. Upload and download files

# ! / usr / bin / env python
#-*- coding:utf-8 -*-

import paramiko
url_list = ['192.168.88.130',
            '192.168.88.131',
            '192.168.88.132',
            '192.168.88.133',
            ]

def user_passwd_upload_wget(ssh_ip):
    '''Upload and download files based on username and password'''
    transport = paramiko.Transport((ssh_ip, 22))
    transport.connect(username='root', password='123456')
    sftp = paramiko.SFTPClient.from_transport(transport)
    # Upload location.py to server /tmp/test.py
    sftp.put('/tmp/location.py', '/tmp/test.py')
    # Download remove_path to local local_path
    sftp.get('remove_path', 'local_path')
    transport.close()

def private_key_upload_wget(ssh_ip):
    '''Upload and download files based on key'''
    private_key = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa')
    transport = paramiko.Transport((ssh_ip, 22))
    transport.connect(username='root', pkey=private_key)
    sftp = paramiko.SFTPClient.from_transport(transport)
    # Upload location.py to server /tmp/test.py
    sftp.put('/tmp/location.py', '/tmp/test.py')
    # Download remove_path to local local_path
    sftp.get('remove_path', 'local_path')
    transport.close()

if __name__ == '__main__':
    for ip in url_list:
        user_passwd_upload_wget(ip)
        # private_key_upload_wget(ip)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

Related: python paramiko module use