基于ssh用于连接远程服务器做操作: 远程执行命令上传/下载文件
1.调用paramiko,没有安装的话得提前安装
import paramiko
2.创建一个ssh对象
client = paramiko.SSHClient()
3.第一用ssh连接的话,会出现下面的认证
The authenticity of host ‘172.25.254.254 (172.25.254.254)’ can’t be
established. ECDSA key fingerprint is
4c:98:51:43:65:46:66:fd:af:5b:ea:cc:d9:97:c0:74. Are you sure you want
to continue connecting (yes/no)?
自动选择yes
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
4.连接服务器
client.connect(
hostname='172.25.254.26',
username='root',
password='redhat'
)
5.执行操作
#标准输入 标准输出 标准错误
stdin,stdout,stderr = client.exec_command('route -n')
6.获取命令的执行结果,相当于解码
print(stdout.read().decode('utf-8'))
7.关闭连接
client.close()
具体操作如下:
需求:
查看我ip为172.25.254.26的网关