python3模擬實(shí)現(xiàn)xshell遠(yuǎn)程執(zhí)行l(wèi)inux命令的方法
依賴包:pip install paramiko
源碼demo:
from time import *
import paramiko
# 定義一個(gè)類,表示一臺(tái)遠(yuǎn)端linux主機(jī)
class Linux(object):
# 通過(guò)IP, 用戶名,密碼,超時(shí)時(shí)間初始化一個(gè)遠(yuǎn)程Linux主機(jī)
def __init__(self, ip, username, password, timeout=30):
self.ip = ip
self.username = username
self.password = password
self.timeout = timeout
# transport和chanel
self.t = ''
self.chan = ''
# 鏈接失敗的重試次數(shù)
self.try_times = 3
# 調(diào)用該方法連接遠(yuǎn)程主機(jī)
def connect(self):
while True:
# 連接過(guò)程中可能會(huì)拋出異常,比如網(wǎng)絡(luò)不通、鏈接超時(shí)
try:
self.t = paramiko.Transport(sock=(self.ip, 22))
self.t.connect(username=self.username, password=self.password)
self.chan = self.t.open_session()
self.chan.settimeout(self.timeout)
self.chan.get_pty()
self.chan.invoke_shell()
# 如果沒(méi)有拋出異常說(shuō)明連接成功,直接返回
print('連接%s成功' % self.ip)
# 接收到的網(wǎng)絡(luò)數(shù)據(jù)解碼為str
print(self.chan.recv(65535).decode('utf-8'))
return
# 這里不對(duì)可能的異常如socket.error, socket.timeout細(xì)化,直接一網(wǎng)打盡
except Exception as e1:
if self.try_times != 0:
print('連接%s失敗,進(jìn)行重試' % self.ip)
self.try_times -= 1
else:
print('重試3次失敗,結(jié)束程序')
exit(1)
# 斷開(kāi)連接
def close(self):
self.chan.close()
self.t.close()
# 發(fā)送要執(zhí)行的命令
def send(self, cmd):
cmd += '\r'
result = ''
# 發(fā)送要執(zhí)行的命令
self.chan.send(cmd)
# 回顯很長(zhǎng)的命令可能執(zhí)行較久,通過(guò)循環(huán)分批次取回回顯,執(zhí)行成功返回true,失敗返回false
while True:
sleep(0.5)
ret = self.chan.recv(65535)
ret = ret.decode('utf-8')
result += ret
return result
'''
發(fā)送文件
@:param upload_files上傳文件路徑 例如:/tmp/test.py
@:param upload_path 上傳到目標(biāo)路徑 例如:/tmp/test_new.py
'''
def upload_file(self,upload_files,upload_path):
try:
tran=paramiko.Transport(sock=(self.ip, self.port))
tran.connect(username=self.username, password=self.password)
sftp = paramiko.SFTPClient.from_transport(tran)
result=sftp.put(upload_files, upload_path)
return True if result else False
except Exception as ex:
print(ex)
tran.close()
finally:
tran.close()
# 連接正常的情況
if __name__ == '__main__':
host = Linux('192.168.16.57', 'root', '+B*A15*EFpKG') # 傳入Ip,用戶名,密碼
host.connect()
# result = host.send('ls') # 發(fā)送一個(gè)查看ip的命令
def input_cmd(str):
return input(str)
tishi_msg="輸入命令:"
while True:
msg=input(tishi_msg)
if msg=="exit":
host.close()
break
else:
res=host.send(msg)
data=res.replace(res.split("\n")[-1],"")
tishi_msg=res.split("\n")[-1]
print(res.split("\n")[-1] + data.strip("\n"))
運(yùn)行代碼測(cè)試效果圖:

以上這篇python3模擬實(shí)現(xiàn)xshell遠(yuǎn)程執(zhí)行l(wèi)inux命令的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python Sql數(shù)據(jù)庫(kù)增刪改查操作簡(jiǎn)單封裝
這篇文章主要為大家介紹了Python Sql數(shù)據(jù)庫(kù)增刪改查操作簡(jiǎn)單封裝,感興趣的小伙伴們可以參考一下2016-04-04
python切片中內(nèi)存的注意事項(xiàng)總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于python切片中內(nèi)存的注意事項(xiàng)總結(jié)內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。2021-08-08
python中@property的作用和getter setter的解釋
這篇文章主要介紹了python中@property的作用和getter setter的解釋,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
Python3視頻轉(zhuǎn)字符動(dòng)畫(huà)的實(shí)例代碼
這篇文章主要介紹了Python3視頻轉(zhuǎn)字符動(dòng)畫(huà)的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
Python中標(biāo)準(zhǔn)庫(kù)OS的常用方法總結(jié)大全
python中的OS庫(kù)提供了使用各種操作系統(tǒng)功能的接口。是經(jīng)常用到的一個(gè)庫(kù),所以這篇文章主要給大家總結(jié)介紹了關(guān)于Python中標(biāo)準(zhǔn)庫(kù)OS的常用方法,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來(lái)一起看看吧。2017-07-07

