【笔记】Python3连接SSH

前言

Python3连接SSH

下载依赖

1
pip3 install paramiko

正文

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

# 创建SSH对象
ssh = paramiko.SSHClient()

# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# 通过用户名和密码登录,建立连接
ssh.connect("<ip>", "22", "<username>", "<password>")

# 执行命令
result = ssh.exec_command("<shell>")

完成