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"\
r\n*************************************************************************\
r\n* IOSv is strictly limited to use for evaluation, demonstration and
IOS *\r\n* education. IOSv is provided as-isandisnot supported by
Cisco's *\r\n* Technical Advisory Center. Any use or disclosure,in whole orin part,*\r\n* of the IOSv Software or Documentation to
any third party forany*\r\n* purposes is expressly prohibited
exceptas otherwise authorized by *\r\n* Cisco in writing.*\r\n***********************************************************************\r\nlax-edg-r1#">>> new_connection.send("show version | i V\n")19>>> time.sleep(3)>>> output = new_connection.recv(5000)>>>print(output)
b'show version | i V\r\nCisco IOS Software, IOSv Software (VIOSADVENTERPRISEK9-M), Version 15.8(3)M2, RELEASE SOFTWARE (fc2)\r\nProcessor
board ID 98U40DKV403INHIULHYHB\r\nlax-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
V\n')
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 710:14 Desktopndrwxr-xr-x 2
echou echou 4096 Jan 710:14 Documentsndrwxr-xr-x 2 echou echou 4096 Jan 710:14 Downloadsn-rw-r--r--1 echou echou 8980 Jan 710:03
examples.desktopndrwxr-xr-x 2 echou echou 4096 Jan 710:14 Musicndrwxrxr-x
echou echou 4096 Jan 710:14 Picturesndrwxr-xr-x 2 echou echou 4096 Jan 710:14 Publicndrwxr-xr-x 2 echou echou 4096 Jan 710:14 Templatesndrwxrxr-x
2 echou echou 4096 Jan 710:14 Videosn'
>>> stdin, stdout, stderr = client.exec_command('pwd')>>> stdout.read()b'/home/echou'>>> client.close()
版权归原作者 老虎爱代码 所有, 如有侵权,请联系我们删除。