使用Python的paramiko模块

介绍

使用paramiko模块, 在Python的环境下实现 SSH 下的一些操作.

安装

直接使用pip安装即可.

1
pip install paramiko

使用

1
2
3
4
5
6
7
8
9
import paramiko
ssh = paramiko.SSHClient()

#允许连接不在know_hosts文件中的主机。
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, port, username, password)

# 执行shell命令
ssh.exec_command('ls')

API文档:https://paramiko-docs.readthedocs.io/en/2.4/api/client.html

SFTP

通过paramiko模块可以实现SCP的功能

1
2
3
4
5
6
7
8
9
10
11
12

#在新建好ssh链接对象之后, 新建一个链接对象.
sftp = ssh.open_sftp()

#上传文件
sftp.put(local_file, remote_file)

#下载文件
sftp.get(remote_file, local_file)

#列出目录
sftp.listdir()#默认当前目录

Comments

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×