惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
博客园 - 司徒正美
大猫的无限游戏
大猫的无限游戏
Last Week in AI
Last Week in AI
V
Visual Studio Blog
阮一峰的网络日志
阮一峰的网络日志
小众软件
小众软件
宝玉的分享
宝玉的分享
Apple Machine Learning Research
Apple Machine Learning Research
美团技术团队
WordPress大学
WordPress大学
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
罗磊的独立博客
The Cloudflare Blog
V
V2EX
月光博客
月光博客
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
GbyAI
GbyAI
博客园 - 【当耐特】
T
Tailwind CSS Blog

博客园 - 干炸小黄鱼

timex 处理时间戳 gorm-gen go 雪花算法 golang每日一库--协程池库ants golang每日一库--json解析库gjson python高级编程-asyncio python高级编程-condition python高级编程-event python装饰器-自动重试 EAP系统 go实现实现 SECS/GEM 协议 设备通信协议 SECS go项目使用Jenkins进行CICD go操作ES mongo db聚合查询 go如何使用mongodb Apache ShardingSphere paxos and raft (分布式一致性算法) go使用zookeeper分布式锁以及和redis差异 go使用 seata 示例 Alibaba 分布式事务 Seata go中使用saga go中使用TCC示例 分布式事务TCC 熔断器 Hystrix OR Sentinel k8s下部署consul and etcd Consul OR Etcd 【力扣hot100】双指针-盛水最多的容器 【力扣hot100】滑动窗口-最小覆盖子串 shell脚本合集
python ssh clinet
干炸小黄鱼 · 2024-11-19 · via 博客园 - 干炸小黄鱼

业务需要, 封装了一个ssh 客户端;

import paramiko

host = "192.168.11.1"
port = 22
user = "1"
pw = "1"


class SshClient:
    def __init__(self) -> None:
        self.ssh_client = paramiko.SSHClient()
        self.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.ssh_client.connect(hostname=host, port=port, username=user, password=pw)
        self.ftp_client = self.ssh_client.open_sftp()

    def ssh_exec(self, cmd):
        _, stdout, stderr = self.ssh_client.exec_command(cmd)
        print(stdout.read())
        print(stderr.read())

    def get_ssh_exec(self, cmd):
        _, stdout, _ = self.ssh_client.exec_command(cmd)
        out_data = stdout.read().strip().decode("utf8")
        return out_data

    def ftp_get(self, remote_file, local_file):
        self.ftp_client.get(remote_file, local_file)

    def ftp_put(self, local_file, remote_file):
        self.ftp_client.put(local_file, remote_file)

    def __del__(self):
        self.ssh_client.close()
        self.ftp_client.close()
# 调用展示某目录下的文件
cam_files = SshClient().get_ssh_exec(f"ls /root/")
# 下载某文件
vf = "/root/a.txt"
filename = "a.new.txt"
SshClient().ftp_get(vf, f"/tmp/{filename}")