广告
返回顶部
首页 > 资讯 > 操作系统 >Python通过paramiko库实现远程执行linux命令的方法
  • 502
分享到

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

摘要

(1)首先安装paramiko库 pip install paramiko (2)封装了以下类,可以直接拿来使用 import paramiko class sshClient(object): def __

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命令内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

--结束END--

本文标题: Python通过paramiko库实现远程执行linux命令的方法

本文链接: https://www.lsjlt.com/news/198777.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作