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

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
博客园_首页
雷峰网
雷峰网
V
Visual Studio Blog
爱范儿
爱范儿
A
About on SuperTechFans
量子位
N
Netflix TechBlog - Medium
Microsoft Security Blog
Microsoft Security Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
月光博客
月光博客
S
SegmentFault 最新的问题
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - 干炸小黄鱼

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}")