使用Python實現(xiàn)SSH隧道界面功能
開發(fā)原因
MobaXterm作為一個全能型終端神器,功能十分強大,我經(jīng)常使用其中隧道功能,使用內(nèi)部無法直接服務(wù)器,查詢數(shù)據(jù),一般來說,一個本地端口對于一個隧道,但是MobaXterm,免費版本最多只能建立三個隧道,比如,我需要一次查詢統(tǒng)計,就會用到四個隧道的操作,就非常不方便,需要調(diào)整一個隧道,于是,便用python寫了多隧道的客戶端

效果圖

界面使用tkinter實現(xiàn),左邊是輸入隧道的信息,右邊為歷史列表,
源碼分析
構(gòu)建隧道
def operate_sshtunnel(tunnel_info):
try:
tunnel = SSHTunnelForwarder(
(tunnel_info.ssh_ip, int(tunnel_info.ssh_port)),
ssh_username=tunnel_info.ssh_username,
ssh_password=tunnel_info.ssh_password,
remote_bind_address=(tunnel_info.remote_ip, int(tunnel_info.remote_port)),
local_bind_address=('127.0.0.1', int(tunnel_info.localhost_port))
)
return tunnel
except Exception as e:
print(e.args[0])
messagebox.showinfo(title='連接異常', message=e.args[0])
return這段代碼就是整個功能的核心代碼,使用SSHTunnelForwarder模塊的sshtunnel
構(gòu)建隧道,由于我只需要一個本地端口訪問遠程遠程服務(wù)器的功能,默認本地端口固定為127.0.0.1
初始化加載
def read_json():
if os.path.exists('tunnel_data.json'):
with open('tunnel_data.json', 'r', encoding='utf-8') as load_f:
data = load_f.read()
if len(data) > 0:
json_str = cryptocode.decrypt(data, "EjdeB55cvQMN2WHf")
return json.loads(json_str)
else:
return
def load_config():
load_arr = read_json()
if load_arr is not None:
for tunnel_info_json in load_arr:
tunnel_info = tunnel_info_class()
tunnel_info.localhost_port = tunnel_info_json['localhost_port']
tunnel_info.ssh_ip = tunnel_info_json['ssh_ip']
tunnel_info.ssh_port = tunnel_info_json['ssh_port']
tunnel_info.ssh_username = tunnel_info_json['ssh_username']
tunnel_info.ssh_password = cryptocode.decrypt(tunnel_info_json['ssh_password'], "F1jgEg1arVyxmUqC")
tunnel_info.remote_ip = tunnel_info_json['remote_ip']
tunnel_info.remote_port = tunnel_info_json['remote_port']
tunnel_info.tunnel_name = tunnel_info_json['tunnel_name']
tree_id = insert_tree_view(tunnel_info, "未啟動")
tunnel_infos.update({tree_id: tunnel_info})read_json是讀取歷史記錄,其中使用 cryptocode模版對明文的json進行加密,并且對ssh_password進行再加密
開始服務(wù)
def start_tunnel():
iid = treeview.selection()
if len(iid) > 0:
if iid not in tunnel_infos_start.keys():
tunnel_info = tunnel_infos[iid[0]]
tunnel = ssl_tunnel.operate_sshtunnel(tunnel_info)
if tunnel is not None:
try:
tunnel.start()
tunnel_infos_start.update({iid[0]: tunnel})
update_tree_view(iid[0], tunnel_info, "啟動")
pass
except Exception as e:
messagebox.showinfo(title='連接異常', message=e.args[0])
else:
messagebox.showinfo(title='選擇異常', message="未選擇列表")tunnel_infos為報存的隧道信息字典,tunnel_infos_start為報存的已經(jīng)啟動的隧道字典,先獲取點擊到的行的ID,然后查詢是否在已啟動的字典中,如果不存在則,啟動隧道,同時更新到tunnel_infos_start中
停止服務(wù)
def stop_tunnel():
iid = treeview.selection()
if len(iid) > 0:
if iid[0] in tunnel_infos_start.keys():
tunnel_info = tunnel_infos[iid[0]]
tunnel = tunnel_infos_start[iid[0]]
if tunnel is not None:
try:
tunnel.stop()
tunnel_infos_start.pop(iid[0])
update_tree_view(iid[0], tunnel_info, "未啟動")
pass
except Exception as e:
messagebox.showinfo(title='連接異常', message=e.args[0])
else:
messagebox.showinfo(title='選擇異常', message="未選擇列表")這段代碼操作和啟動相反,則是從停止掉服務(wù),同時從tunnel_infos_start中移除掉該隧道
移除服務(wù)
def remove_tunnel():
iid = treeview.selection()
if len(iid) > 0:
if iid[0] in tunnel_infos_start.keys():
stop_tunnel()
## 從列表刪除
treeview.delete(iid)
tunnel_infos.pop(iid[0])
write_json()
else:
messagebox.showinfo(title='選擇異常', message="未選擇列表")移除服務(wù)的時候,會判斷ID在tunnel_infos_start是否存在,存在則表明當(dāng)前刪除的隧道還在啟動中,則停止這個服務(wù),同時從tunnel_infos移除這配置,更新tunnel_data.json文件
不足之處
雖然這個簡單的工具可以滿足超過多個隧道的使用,但是每次報存的時候,都要更新tunnel_data.json文件,如果隧道比較多,加載比較費時間;同時由于設(shè)計界面的時候,考慮比較簡單,并不支持修改的功能,只能刪除錯誤的記錄,然后重新報存
源碼地址
https://github.com/liuhao192/ssh_tunnel
到此這篇關(guān)于使用Python實現(xiàn)SSH隧道界面功能的文章就介紹到這了,更多相關(guān)python SSH隧道內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python函數(shù)中的不定長參數(shù)相關(guān)知識總結(jié)
今天給大家?guī)淼氖顷P(guān)于Python函數(shù)的相關(guān)知識,文章圍繞著Python不定長參數(shù)展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下2021-06-06
Python虛擬機字節(jié)碼教程之裝飾器實現(xiàn)詳解
在本篇文章當(dāng)中主要給大家介紹在?cpython?當(dāng)中一些比較常見的字節(jié)碼,從根本上理解?python?程序的執(zhí)行。在本文當(dāng)中主要介紹一些?python?基本操作的字節(jié)碼,并且將從字節(jié)碼的角度分析函數(shù)裝飾器的原理2023-04-04
python使用response.read()接收json數(shù)據(jù)的實例
今天小編就為大家分享一篇python使用response.read()接收json數(shù)據(jù)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
python selenium瀏覽器復(fù)用技術(shù)的使用
本文主要介紹了python selenium瀏覽器復(fù)用技術(shù)的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
使用Matplotlib創(chuàng)建基本圖表的詳細指南
Matplotlib 是一個功能強大的 Python 庫,用于創(chuàng)建各種類型的圖表和可視化,在本文中,我們將提供一個完整的指南,介紹如何使用 Matplotlib 創(chuàng)建基本的圖表,包括折線圖、散點圖、柱狀圖和餅圖,感興趣的小伙伴跟著小編一起來看看吧2024-05-05
Python腳本支持OC代碼重構(gòu)模塊調(diào)用關(guān)系分析實踐
在軟件開發(fā)中,經(jīng)常會遇到一些代碼問題,例如邏輯結(jié)構(gòu)復(fù)雜、依賴關(guān)系混亂、代碼冗余、不易讀懂的命名等,這些問題可能導(dǎo)致代碼的可維護性下降,增加維護成本,同時也會影響到開發(fā)效率,本文以Python實現(xiàn)自動化的工具,支持代碼重構(gòu)過程的實踐2023-10-10
Python腳本實現(xiàn)網(wǎng)卡流量監(jiān)控
這篇文章主要介紹了Python腳本實現(xiàn)網(wǎng)卡流量監(jiān)控,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-02-02

