python实现ssh操作
Project description
zdpapi_shh
python实现ssh操作,基于paramiko二次封装
安装方式:
pip install zdpapi_ssh
一、快速入门
1.1 建立连接
paramiko方式
import paramiko
# 建立一个sshclient对象
ssh = paramiko.SSHClient()
# 允许将信任的主机自动加入到host_allow 列表,此方法必须放在connect方法的前面
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 调用connect方法连接服务器
ssh.connect(hostname='192.168.18.11', port=22,
username='zhangdapeng', password='zhangdapeng')
# 执行命令
stdin, stdout, stderr = ssh.exec_command('df -hl')
# 结果放到stdout中,如果有错误将放到stderr中
print(stdout.read().decode())
# 关闭连接
ssh.close()
zdpapi_shh方式
from zdpapi_ssh import SSH
ssh = SSH(hostname='192.168.18.11', port=22,
username='zhangdapeng', password='zhangdapeng')
ssh.execute('df -hl')
1.2 建立多个ssh连接
paramiko的方式
import paramiko
# 实例化一个transport对象
trans = paramiko.Transport(('192.168.18.11', 22))
# 建立连接
trans.connect(username='zhangdapeng', password='zhangdapeng')
# 将sshclient的对象的transport指定为以上的trans
ssh = paramiko.SSHClient()
ssh._transport = trans
# 执行命令,和传统方法一样
stdin, stdout, stderr = ssh.exec_command('df -hl')
print(stdout.read().decode())
# 关闭连接
trans.close()
zdpapi_ssh的方式
from zdpapi_ssh import SSH
ssh = SSH(hostname='192.168.18.11', port=22,
username='zhangdapeng', password='zhangdapeng')
ssh.execute_trans('192.168.18.11', 'df -hl')
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
zdpapi_ssh-0.1.1.tar.gz
(3.8 kB
view hashes)
Built Distribution
Close
Hashes for zdpapi_ssh-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7e995361156b54e55948a93404f791b0300175624ec530e99f237e43bba773ce |
|
MD5 | e4237f8a878510753a76fa843d53bb34 |
|
BLAKE2b-256 | cab1bbb3092b7e20afc475550ca7462be1262e075a92c7b48cc14a3b7057d15a |