Python通過paramiko庫(kù)實(shí)現(xiàn)遠(yuǎn)程執(zhí)行l(wèi)inux命令的方法
(1)首先安裝paramiko庫(kù)
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)備一個(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通過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實(shí)現(xiàn)帶下標(biāo)索引的遍歷操作示例
這篇文章主要介紹了Python實(shí)現(xiàn)帶下標(biāo)索引的遍歷操作,結(jié)合具體實(shí)例形式分析了2種帶索引的遍歷操作實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-05-05Python通過paramiko遠(yuǎn)程下載Linux服務(wù)器上的文件實(shí)例
今天小編就為大家分享一篇Python通過paramiko遠(yuǎn)程下載Linux服務(wù)器上的文件實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12Python抓取網(wǎng)頁(yè)圖片難點(diǎn)分析
沒想到python是如此強(qiáng)大,令人著迷,以前看見圖片總是一張一張復(fù)制粘貼,現(xiàn)在好了,學(xué)會(huì)python就可以用程序?qū)⒁粡垙垐D片,保存下來。今天網(wǎng)上沖浪看到很多美圖,可是圖片有點(diǎn)多,不想一張一張地復(fù)制粘貼,怎么辦呢?辦法總是有的,即便沒有我們也可以創(chuàng)造一個(gè)辦法2023-01-01python?遠(yuǎn)程執(zhí)行命令的詳細(xì)代碼
有時(shí)會(huì)需要在遠(yuǎn)程的機(jī)器上執(zhí)行一個(gè)命令,并獲得其返回結(jié)果。對(duì)于這種情況,python 可以很容易的實(shí)現(xiàn)。今天通過實(shí)例代碼介紹下python?遠(yuǎn)程執(zhí)行命令的相關(guān)知識(shí),感興趣的朋友一起看看吧2022-02-02python數(shù)據(jù)可視化Seaborn繪制山脊圖
這篇文章主要介紹了利用python數(shù)據(jù)可視化Seaborn繪制山脊圖,山脊圖一般由垂直堆疊的折線圖組成,這些折線圖中的折線區(qū)域間彼此重疊,此外它們還共享相同的x軸.下面來看看具體的繪制過程吧,需要的小伙伴可以參考一下2022-01-01Java中重定向輸出流實(shí)現(xiàn)用文件記錄程序日志
這篇文章主要介紹了Java中重定向輸出流實(shí)現(xiàn)用文件記錄程序日志,本文直接給出代碼實(shí)例,并對(duì)代碼做了詳細(xì)注解,需要的朋友可以參考下2015-06-06pip安裝py_zipkin時(shí)提示的SSL問題對(duì)應(yīng)
今天小編就為大家分享一篇關(guān)于pip安裝py_zipkin時(shí)提示的SSL問題對(duì)應(yīng),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12淺談python中對(duì)于json寫入txt文件的編碼問題
今天小編就為大家分享一篇淺談python中對(duì)于json寫入txt文件的編碼問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06基于Python中request請(qǐng)求得到的response的屬性問題
這篇文章主要介紹了基于Python中request請(qǐng)求得到的response的屬性問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05