Paramiko是一个Python实现的SSH2远程安全连接模块,它支持认证及密钥方式。
具体来说,Paramiko是一个用于实现SSHv2协议的模块,它可以用于创建SSH客户端或服务器端。通过使用Paramiko,你可以连接到远程服务器、执行命令、传输文件等操作。Paramiko还支持多种认证方式,包括口令认证和公钥认证,以及多种密钥交换算法和压缩方法。
使用Paramiko模块可以编写Python代码,实现SSH相关功能,例如文件传输、远程执行命令等。
安装方式
sudo apt-get install build-essential libssl-dev libffi-dev python3-dev
pip3 install cryptography
pip3 install paramiko
示例:
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 自动添加主机密钥
ssh.connect('remote_host', username='your_username', password='your_password')
#hostname:连接的目标主机。
#port=SSH_PORT:指定端口,一般默认为22。
#username=None:验证的用户名。
#password=None:验证的用户密码。
#pkey=None:私钥方式用于身份验证。
#key_filename=None:一个文件名或文件列表,指定私钥文件。
#timeout=None:可选的tcp连接超时时间。
#allow_agent=True:是否允许连接到ssh代理,默认为True。
#look_for_keys=True:是否在~/.ssh中搜索私钥文件,默认为True。
#compress=False:是否打开压缩。
#常用方法:
#connect():主要是用于实现和远程服务器的连接与认证。
stdin, stdout, stderr = ssh.exec_command('ls')
output = stdout.read().decode()
print(output)
sftp = ssh.open_sftp()
sftp.put('local_file', 'remote_file') # 上传文件
sftp.close()
终端示例
>>> import paramiko, time
>>> connection = paramiko.SSHClient()
>>> connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> connection.connect('192.168.2.51', username='cisco', password='cisco',
look_for_keys=False, allow_agent=False)
>>> new_connection = connection.invoke_shell()
>>> output = new_connection.recv(5000)
>>> print(output) b"
rn*************************************************************************
rn* IOSv is strictly limited to use for evaluation, demonstration and
IOS *rn* education. IOSv is provided as-is and is not supported by
Cisco's *rn* Technical Advisory Center. Any use or disclosure,
in whole or in part, *rn* of the IOSv Software or Documentation to
any third party for any *rn* purposes is expressly prohibited
except as otherwise authorized by *rn* Cisco in writing.
*rn*********************************************************************
**rnlax-edg-r1#"
>>> new_connection.send("show version | i Vn")
19
>>> time.sleep(3)
>>> output = new_connection.recv(5000)
>>> print(output)
b'show version | i VrnCisco IOS Software, IOSv Software (VIOSADVENTERPRISEK9-M), Version 15.8(3)M2, RELEASE SOFTWARE (fc2)rnProcessor
board ID 98U40DKV403INHIULHYHBrnlax-edg-r1#'
>>> new_connection.close()
>>>
>>> import paramiko
>>> connection = paramiko.SSHClient()
>>> connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> connection.connect('192.168.2.51', username='cisco', password='cisco',
look_for_keys=False, allow_agent=False)
>>> stdin, stdout, stderr = connection.exec_command('show version | i
Vn')
Traceback (most recent call last):
skip>
raise SSHException('SSH session not active') paramiko.ssh_exception.
SSHException: SSH session not active
ssh-keygen -t rsa
cat ~/.ssh/id_rsa.pub
>>> import paramiko
>>> key = paramiko.RSAKey.from_private_key_file('/home/xxx/.ssh/id_rsa')
>>> client = paramiko.SSHClient()
>>> client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> client.connect('192.168.199.182', username='echou', pkey=key)
>>> stdin, stdout, stderr = client.exec_command('ls -l')
>>> stdout.read()
b'total 44ndrwxr-xr-x 2 echou echou 4096 Jan 7 10:14 Desktopndrwxr-xr-x 2
echou echou 4096 Jan 7 10:14 Documentsndrwxr-xr-x 2 echou echou 4096 Jan 7
10:14 Downloadsn-rw-r--r-- 1 echou echou 8980 Jan 7 10:03
examples.desktopndrwxr-xr-x 2 echou echou 4096 Jan 7 10:14 Musicndrwxrxr-x
echou echou 4096 Jan 7 10:14 Picturesndrwxr-xr-x 2 echou echou 4096 Jan 7
10:14 Publicndrwxr-xr-x 2 echou echou 4096 Jan 7 10:14 Templatesndrwxrxr-x
2 echou echou 4096 Jan 7 10:14 Videosn'
>>> stdin, s服务器托管网tdout, stderr = client.exec_服务器托管网command('pwd')
>>> stdout.read()
b'/home/echou'
>>> client.close()
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
老周一般很少玩游戏,在某宝上买了一堆散件,计划在过年期间自己做个机械臂耍耍。头脑中划过一道紫蓝色的闪电,想起用游戏手柄来控制机械臂。机械臂是由树莓派(大草莓)负责控制,然后客户端通过 Socket UDP 来发送信号。优先考虑在 PC 和手机上测试,就顺便折腾…