paramiko

Time:2019-12-10

1.paramiko

It is used to help developers connect to the server remotely through code and operate the server.

pip3 install paramiko

Remote execution command [user name and password]

import paramiko

#Create SSH object
ssh = paramiko.SSHClient()

#Allow connections to hosts that are not in the know? Hosts file
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

#Connect to server
ssh.connect(hostname='192.168.16.85', port=22, username='root', password='123456')

#Execute command
stdin, stdout, stderr = ssh.exec_command('df')
#Get command results
result = stdout.read()
#Close connection
ssh.close()

print(result.decode('utf-8'))

Execute the command [public key and private key] remotely (the public key must be uploaded to the server in advance)

import paramiko

private_key = paramiko.RSAKey.from_private_key_file(r'C:/Users/Administrator/.ssh/id_rsa')

#Create SSH object
ssh = paramiko.SSHClient()
#Allow connections to hosts that are not in the know? Hosts file
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#Connect to server
ssh.connect(hostname='192.168.16.85', port=22, username='root', pkey=private_key)

#Execute command
stdin, stdout, stderr = ssh.exec_command('df')
#Get command results
result = stdout.read()

#Close connection
ssh.close()

print(result)

Upload and download files remotely [user name and password]

import paramiko

transport = paramiko.Transport(('192.168.16.85', 22))
transport.connect(username='root', password='123456')
sftp = paramiko.SFTPClient.from_transport(transport)


#Upload location.py to server / TMP / test.py
# sftp.put('wy.txt', '/data/wy.txt')
sftp.get('/data/wy.txt', 'xx.txt')

transport.close()

Upload and download files remotely [public key and private key]

import paramiko

private_key = paramiko.RSAKey.from_private_key_file(r'C:/Users/Administrator/.ssh/id_rsa')

transport = paramiko.Transport(('192.168.16.85', 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 path
# sftp.get('remove_path', 'local_path')

transport.close()

Add: you can also connect to the remote server through the private key string.

key = """-----BEGIN RSA PRIVATE KEY-----
MIIG5AIBAAKCAYEAu0fkMInsVRnIBSiZcVYhKuccWCh6hapYgB1eSOWZLz3+xFGy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f43dVm7nv4PM2e24CRCWXMXYl7G3lFsQF/g7JNUoyr6bZQBf3pQcBw4
IJ38IcKV+L475tP4rfDrqyJz7mcJ+90a+ai5cSr9XoZqviAqNdhvBq5LjGOLkcdN
bS0NAVVoGqjqIY/tOd2NMTEF6kVoYfJ7ZJtjxk/R3sdbdtajV3YsAg==
-----END RSA PRIVATE KEY-----"""


import paramiko
from io import StringIO

private_key = paramiko.RSAKey(file_obj=StringIO(key))

#Create SSH object
ssh = paramiko.SSHClient()
#Allow connections to hosts that are not in the know? Hosts file
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#Connect to server
ssh.connect(hostname='192.168.16.85', port=22, username='root', pkey=private_key)

#Execute command
stdin, stdout, stderr = ssh.exec_command('df')
#Get command results
result = stdout.read()

#Close connection
ssh.close()

print(result)

2. Employees of the company connect to the server based on xshell

Generate public and private keys

ssh-keygen.exe -m pem

Generated in the current user's home directory:. SSH / id_rsa.pub. SSH / id_rsa

Put the public key on the server

ssh-copy-id -i ~.ssh/id_rsa.pub [email protected]

When you connect to the server later, you do not need to enter a password

ssh [email protected]