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连接
方法1是传统的连接服务器、执行命令、关闭的一个操作,有时候需要登录上服务器执行多个操作,比如执行命令、上传/下载文件,方法1则无法实现,可以通过如下方式来操作
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')
二、FTP操作
2.1 上传和下载
paramiko实现
import paramiko
# 实例化一个trans对象# 实例化一个transport对象
trans = paramiko.Transport(('192.168.18.11', 22))
# 建立连接
trans.connect(username='zhangdapeng', password='zhangdapeng')
# 实例化一个 sftp对象,指定连接的通道
sftp = paramiko.SFTPClient.from_transport(trans)
# 发送文件
sftp.put(localpath='README.md', remotepath='/home/zhangdapeng/README.md')
# 下载文件
# sftp.get(remotepath, localpath)
trans.close()
zdpapi_ssh实现
from zdpapi_ssh import SSH
ssh = SSH(hostname='192.168.18.11', port=22,
username='zhangdapeng', password='zhangdapeng')
# 上传
ssh.ftp_upload('192.168.18.11', 'README.md', '/home/zhangdapeng/README1.md')
# 下载
ssh.ftp_download('192.168.18.11', 'README1.md', '/home/zhangdapeng/README1.md')
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.2.tar.gz
(4.6 kB
view details)
Built Distribution
File details
Details for the file zdpapi_ssh-0.1.2.tar.gz
.
File metadata
- Download URL: zdpapi_ssh-0.1.2.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.7 CPython/3.8.8 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1058cff05d3169601f08a10875e85cf656f5877e6e9ff36d0a103a113907969c |
|
MD5 | 2164cdeebd19abbc25852e2fe521b3c2 |
|
BLAKE2b-256 | 89873c31ca930da865c89f757bc979ecf4a67c3f4894b7371c356e40f403c53c |
File details
Details for the file zdpapi_ssh-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: zdpapi_ssh-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.7 CPython/3.8.8 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8deee9227d22930f0bcc81f325f7a3518d62e548d2a85f692a04dcdc47fa69be |
|
MD5 | 42405fc656d8a30258626998b8ef9ddf |
|
BLAKE2b-256 | 3e28b0875bbeb854e1157c7d196b57523e7f011f8e0c8b676c9c21b44f675484 |