溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python怎么通過paramiko庫實現(xiàn)遠(yuǎn)程執(zhí)行l(wèi)inux命令

發(fā)布時間:2023-03-07 11:25:05 來源:億速云 閱讀:128 作者:iii 欄目:開發(fā)技術(shù)

今天小編給大家分享一下Python怎么通過paramiko庫實現(xiàn)遠(yuǎn)程執(zhí)行l(wèi)inux命令的相關(guān)知識點,內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

(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)比如準(zhǔn)備一個ip地址為192.168.1.12的linux虛擬機,然后直接按照如下方法使用上面封裝的類即可實現(xiàn)遠(yuǎn)程執(zhí)行l(wèi)inux命令

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

(4)執(zhí)行結(jié)果如下

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庫實現(xiàn)遠(yuǎn)程執(zhí)行l(wèi)inux命令”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI