Python通過(guò)paramiko庫(kù)實(shí)現(xiàn)遠(yuǎn)程執(zhí)行l(wèi)inux命令的方法

(1)首先安裝paramiko庫(kù)
pip install paramiko
(2)封裝了以下類,可以直接拿來(lái)使用
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)備一個(gè)ip地址為192.168.1.12的linux虛擬機(jī),然后直接按照如下方法使用上面封裝的類即可實(shí)現(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 0lo: 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 0virbr0: 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
到此這篇關(guān)于Python通過(guò)paramiko庫(kù)實(shí)現(xiàn)遠(yuǎn)程執(zhí)行l(wèi)inux命令的文章就介紹到這了,更多相關(guān)Python遠(yuǎn)程執(zhí)行l(wèi)inux命令內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python面向?qū)ο缶幊袒A(chǔ)實(shí)例分析
這篇文章主要介紹了Python面向?qū)ο缶幊袒A(chǔ),結(jié)合實(shí)例形式分析了Python面向?qū)ο缶幊填惖亩x、繼承、特殊方法及模塊相關(guān)原理與操作技巧,需要的朋友可以參考下2020-01-01
Python數(shù)據(jù)合并的concat函數(shù)與merge函數(shù)詳解
大家都知道concat()函數(shù)可以沿著一條軸將多個(gè)對(duì)象進(jìn)行堆疊,其使用方式類似數(shù)據(jù)庫(kù)中的數(shù)據(jù)表合并,在使用merge()函數(shù)進(jìn)行合并時(shí),默認(rèn)會(huì)使用重疊的列索引做為合并鍵,即取行索引重疊的部分,本文給大家介紹python?數(shù)據(jù)合并concat函數(shù)與merge函數(shù),感興趣的朋友一起看看吧2022-05-05
Python實(shí)現(xiàn)向服務(wù)器請(qǐng)求壓縮數(shù)據(jù)及解壓縮數(shù)據(jù)的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)向服務(wù)器請(qǐng)求壓縮數(shù)據(jù)及解壓縮數(shù)據(jù)的方法,涉及Python文件傳輸及zip文件相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
Python實(shí)現(xiàn)批量修改Word文檔中圖片大小并居中對(duì)齊
這篇文章主要介紹了如何利用Python實(shí)現(xiàn)批量修改Word文檔中圖片大小并居中對(duì)齊,文中通過(guò)代碼示例給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-08-08
Python結(jié)合Window計(jì)劃任務(wù)監(jiān)測(cè)郵件的示例代碼
這篇文章主要介紹了Python結(jié)合Window計(jì)劃任務(wù)監(jiān)測(cè)郵件的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Python 實(shí)現(xiàn)日志同時(shí)輸出到屏幕和文件
這篇文章主要介紹了Python 實(shí)現(xiàn)日志同時(shí)輸出到屏幕和文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
python獲取系統(tǒng)內(nèi)存占用信息的實(shí)例方法
在本篇文章里小編給大家整理的是關(guān)于python獲取系統(tǒng)內(nèi)存占用信息的實(shí)例方法,有需要的朋友們可以參考學(xué)習(xí)下。2020-07-07

