Python模拟ssh远程登录,并执行命令

288 次查看 0 条评论

直接上代码:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import warnings
warnings.filterwarnings("ignore")
## pip install paramiko
import paramiko
ssh = paramiko.SSHClient()
# Skip the connection link and confirm with YES
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
## use password login
ssh.connect('10.1.2.30',22,'root','123456')
## use private key login
# private_key_path = '/root/.ssh/id_rsa'
# key = paramiko.RSAKey.from_private_key_file(private_key_path)
# ssh.connect('10.1.2.30',22,'appliance',key)
stdin, stdout, stderr = ssh.exec_command('df -h')
print stdout.read()
quit()

返回结果:

88600-w0xgm4qzkfs.png

Other Python

暂无评论,快来抢沙发。