Python编程:封装paramiko模块实现便捷的远程登录

简介: Python编程:封装paramiko模块实现便捷的远程登录

基于 paramiko 的使用,将其做简单封装,便于使用

# -*- coding: utf-8 -*-
import paramiko
class SSHClient(object):
    def __init__(self, hostname, username, password):
        self.hostname = hostname
        self.username = username
        self.password = password
        self.ssh = None
        self._connect()
    def __del__(self):
        self.ssh.close()
    def _connect(self):
        self.ssh = paramiko.SSHClient()
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.ssh.connect(hostname=self.hostname, username=self.username, password=self.password)
    def execute_command(self, command):
        stdin, stdout, stderr = self.ssh.exec_command(command)
        stdout = stdout.read().decode("utf-8")
        stderr = stderr.read().decode("utf-8")
        return stdout, stderr
# 使用示例
def main():
    cmd = "pwd"
    client = SSHClient(hostname, username, password)
    stdout, stderr = client.execute_command(cmd)
    print(stdout, stderr)
if __name__ == '__main__':
    main()

参考:

Python编程:paramiko模块远程登录

相关文章
|
1月前
|
缓存 分布式计算 自然语言处理
Python语言的函数编程模块
Python语言的函数编程模块
|
2月前
|
安全 调度 Python
什么是Python中的事件驱动编程?如何使用`asyncio`模块实现异步事件处理?
【2月更文挑战第4天】【2月更文挑战第9篇】什么是Python中的事件驱动编程?如何使用`asyncio`模块实现异步事件处理?
|
2月前
|
并行计算 程序员 API
Python多进程编程:利用multiprocessing模块实现并行计算
Python多进程编程:利用multiprocessing模块实现并行计算
|
3月前
|
前端开发 安全 Unix
Python编程手册系列 - 日历、日期、时间相关内建模块详解
Python编程手册系列 - 日历、日期、时间相关内建模块详解
70 0
|
4月前
|
安全 Linux 网络安全
Python使用Paramiko实现SSH管理
paramiko 是一个用于在Python中实现SSHv2协议的库,它支持对远程服务器进行加密的通信。目前该模块支持所有平台架构且自身遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接,你可以在Python中实现SSH客户端和服务器,并进行安全的文件传输和远程命令执行。
53 0
|
7月前
|
开发者 Python
< Python全景系列-7 > 提升Python编程效率:模块与包全面解读
< Python全景系列-7 > 提升Python编程效率:模块与包全面解读
92 0
|
10月前
|
运维 Linux Python
【python】paramiko远程操作Linux
【python】paramiko远程操作Linux
|
SQL 关系型数据库 MySQL
Python编程:MySQLdb模块对数据库的基本增删改查操作
Python编程:MySQLdb模块对数据库的基本增删改查操作
101 1
|
Python
Python编程:entry_points将Python模块转变为命令行工具
Python编程:entry_points将Python模块转变为命令行工具
97 0
|
数据采集 IDE 关系型数据库
Python编程:PyThink数据库交互模块提高爬虫编写速度
Python编程:PyThink数据库交互模块提高爬虫编写速度
97 0
Python编程:PyThink数据库交互模块提高爬虫编写速度