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

推荐订阅源

AWS News Blog
AWS News Blog
Jina AI
Jina AI
量子位
V
V2EX
The GitHub Blog
The GitHub Blog
阮一峰的网络日志
阮一峰的网络日志
The Cloudflare Blog
博客园 - 【当耐特】
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
L
LangChain Blog
博客园_首页
aimingoo的专栏
aimingoo的专栏
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Check Point Blog
T
Tailwind CSS Blog
M
MIT News - Artificial intelligence
Engineering at Meta
Engineering at Meta
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
宝玉的分享
宝玉的分享
博客园 - 聂微东
G
Google Developers Blog
C
CERT Recently Published Vulnerability Notes
K
Kaspersky official blog
NISL@THU
NISL@THU
Hacker News: Ask HN
Hacker News: Ask HN
腾讯CDC
Security Archives - TechRepublic
Security Archives - TechRepublic
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
H
Hacker News: Front Page
Webroot Blog
Webroot Blog
有赞技术团队
有赞技术团队
W
WeLiveSecurity
Martin Fowler
Martin Fowler
S
Security @ Cisco Blogs
L
LINUX DO - 最新话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
WordPress大学
WordPress大学
雷峰网
雷峰网
PCI Perspectives
PCI Perspectives
月光博客
月光博客
SecWiki News
SecWiki News
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News | PayPal Newsroom
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 干炸小黄鱼

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脚本合集 分布式id生成器 springboot通用CURD Python PB级检索系统架构设计 rancher 在三台机器搭建k8s集群 数据库排序Null值字段靠后/靠前 常规web项目 docker-compose 例子 手搓一个验证码 使用itertools 中的groupby 对字典数组进行分组后排序 使用开源库 geoip2 获取某ip的经纬度地理信息 python中 apscheduler.schedulers.blocking.BlockingScheduler 定时执行任务 简单的python web项目的docker-compose.yml 示例 python和sliver交互 golang sliver二次开发自定义命令(格式乱后面再调) pydantic做参数校验 基于rancher部署k8s 地理位置相关基础数据 flask migrate时报错 Can't locate revision identified by '3d80e4c025df'
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}")