Python通过paramiko库实现远程执行linux命令的方法

 更新时间:2023年03月07日 10:57:36   作者:redrose2100  
这篇文章主要介绍了Python通过paramiko库实现远程执行linux命令,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

(1)首先安装paramiko库

pip install paramiko

(2)封装了以下类,可以直接拿来使用

import paramiko

class SSHClient(object):
    def __init__(self,host,username,password,port=22):
        self.__host=host
        self.__username=username
        self.__password=password
        self.__port=port
        self.__ssh=None
        self.connect()

    def __del__(self):
        self.close()

    def connect(self):
        self.__ssh = paramiko.SSHClient()
        self.__ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.__ssh.connect(hostname=self.__host,port=self.__port,username=self.__username,password=self.__password)

    def exec(self,cmd):
        print(f"begin to run remote cmd: {cmd}")
        stdin, stdout, stderr = self.__ssh.exec_command(cmd,timeout=1800)
        returncode = stdout.channel.recv_exit_status()
        output=stdout.read().decode('utf-8')
        return output

    def close(self):
        self.__ssh.close()

(3)比如准备一个ip地址为192.168.1.12的linux虚拟机,然后直接按照如下方法使用上面封装的类即可实现远程执行linux命令

ssh=SSHClient(host="192.168.1.12",username="root",password="xxxxxx")
output=ssh.exec("ifconfig")
print(output)

(4)执行结果如下

begin to run remote cmd: ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.12  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 240e:3a1:da7:6590:b39f:e15:6b3d:7e7  prefixlen 64  scopeid 0x0<global>
        inet6 fe80::4a67:131d:9133:acdf  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:58:d8:4c  txqueuelen 1000  (Ethernet)
        RX packets 195340  bytes 148862388 (141.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 163425  bytes 20837281 (19.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 32  bytes 2592 (2.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32  bytes 2592 (2.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:e8:3f:5c  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

到此这篇关于Python通过paramiko库实现远程执行linux命令的文章就介绍到这了,更多相关Python远程执行linux命令内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • python实现在pickling的时候压缩的方法

    python实现在pickling的时候压缩的方法

    这篇文章主要介绍了python实现在pickling的时候压缩的方法,比较具有实用价值,需要的朋友可以参考下
    2014-09-09
  • Python爬虫基本库request的基本使用

    Python爬虫基本库request的基本使用

    这篇文章主要介绍了Python爬虫基本库request的基本使用,urllib库使用繁琐,比如处理网页验证和Cookies时,需要编写Opener和Handler来处理。为了更加方便的实现这些操作,就有了更为强大的requests库,需要的朋友可以参考下
    2023-07-07
  • Python只用40行代码编写的计算器实例

    Python只用40行代码编写的计算器实例

    这篇文章主要介绍了Python只用40行代码编写的计算器,结合完整实例形式分析了Python计算器的具体实现技巧,需要的朋友可以参考下
    2017-05-05
  • Jupyter notebook 不自动弹出网页的解决方案

    Jupyter notebook 不自动弹出网页的解决方案

    这篇文章主要介绍了Jupyter notebook 不自动弹出网页的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-05-05
  • Python类和对象基础入门介绍

    Python类和对象基础入门介绍

    Python 是一种面向对象的编程语言。Python 中的几乎所有东西都是对象,拥有属性和方法。类(Class)类似对象构造函数,或者是用于创建对象的蓝图
    2022-08-08
  • python访问mysql数据库的实现方法(2则示例)

    python访问mysql数据库的实现方法(2则示例)

    这篇文章主要介绍了python访问mysql数据库的实现方法,结合实例形式分析了两种Python操作MySQL数据库的相关技巧,需要的朋友可以参考下
    2016-01-01
  • Python实现矩阵转置的几种方法详解

    Python实现矩阵转置的几种方法详解

    这篇文章主要介绍了Python实现矩阵转置的几种方法详解,zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象,这样做的好处是节约了不少的内存,需要的朋友可以参考下
    2023-08-08
  • 研究Python的ORM框架中的SQLAlchemy库的映射关系

    研究Python的ORM框架中的SQLAlchemy库的映射关系

    这篇文章主要介绍了研究Python的ORM框架中的SQLAlchemy库的映射关系,SQLAlchemy库是一个常见的Python中操作数据库的工具,需要的朋友可以参考下
    2015-04-04
  • python 实现dict转json并保存文件

    python 实现dict转json并保存文件

    今天小编就为大家分享一篇python 实现dict转json并保存文件,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • python+matplotlib实现鼠标移动三角形高亮及索引显示

    python+matplotlib实现鼠标移动三角形高亮及索引显示

    这篇文章主要介绍了Python+matplotlib实现鼠标移动三角形高亮及索引显示,具有一定借鉴价值,需要的朋友可以参考下
    2018-01-01

最新评论