最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Python實(shí)現(xiàn)SSH隧道界面功能

 更新時(shí)間:2022年02月08日 10:09:37   作者:清水雅然君  
這篇文章主要介紹了使用Python實(shí)現(xiàn)一個(gè)SSH隧道界面功能,界面使用tkinter實(shí)現(xiàn),左邊是輸入隧道的信息,右邊為歷史列表,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

開(kāi)發(fā)原因

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

效果圖

界面使用tkinter實(shí)現(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

這段代碼就是整個(gè)功能的核心代碼,使用SSHTunnelForwarder模塊的sshtunnel

構(gòu)建隧道,由于我只需要一個(gè)本地端口訪問(wèn)遠(yuǎn)程遠(yuǎn)程服務(wù)器的功能,默認(rèn)本地端口固定為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, "未啟動(dòng)")
            tunnel_infos.update({tree_id: tunnel_info})

read_json是讀取歷史記錄,其中使用 cryptocode模版對(duì)明文的json進(jìn)行加密,并且對(duì)ssh_password進(jìn)行再加密

開(kāi)始服務(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, "啟動(dòng)")
                    pass
                except Exception  as e:
                    messagebox.showinfo(title='連接異常', message=e.args[0])
    else:
        messagebox.showinfo(title='選擇異常', message="未選擇列表")

tunnel_infos為報(bào)存的隧道信息字典,tunnel_infos_start為報(bào)存的已經(jīng)啟動(dòng)的隧道字典,先獲取點(diǎn)擊到的行的ID,然后查詢是否在已啟動(dòng)的字典中,如果不存在則,啟動(dòng)隧道,同時(shí)更新到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, "未啟動(dòng)")
                    pass
                except Exception  as e:
                    messagebox.showinfo(title='連接異常', message=e.args[0])
    else:
        messagebox.showinfo(title='選擇異常', message="未選擇列表")

這段代碼操作和啟動(dòng)相反,則是從停止掉服務(wù),同時(shí)從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ù)的時(shí)候,會(huì)判斷IDtunnel_infos_start是否存在,存在則表明當(dāng)前刪除的隧道還在啟動(dòng)中,則停止這個(gè)服務(wù),同時(shí)從tunnel_infos移除這配置,更新tunnel_data.json文件

不足之處

雖然這個(gè)簡(jiǎn)單的工具可以滿足超過(guò)多個(gè)隧道的使用,但是每次報(bào)存的時(shí)候,都要更新tunnel_data.json文件,如果隧道比較多,加載比較費(fèi)時(shí)間;同時(shí)由于設(shè)計(jì)界面的時(shí)候,考慮比較簡(jiǎn)單,并不支持修改的功能,只能刪除錯(cuò)誤的記錄,然后重新報(bào)存

源碼地址

https://github.com/liuhao192/ssh_tunnel

到此這篇關(guān)于使用Python實(shí)現(xiàn)SSH隧道界面功能的文章就介紹到這了,更多相關(guān)python SSH隧道內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python函數(shù)中的不定長(zhǎng)參數(shù)相關(guān)知識(shí)總結(jié)

    Python函數(shù)中的不定長(zhǎng)參數(shù)相關(guān)知識(shí)總結(jié)

    今天給大家?guī)?lái)的是關(guān)于Python函數(shù)的相關(guān)知識(shí),文章圍繞著Python不定長(zhǎng)參數(shù)展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Python虛擬機(jī)字節(jié)碼教程之裝飾器實(shí)現(xiàn)詳解

    Python虛擬機(jī)字節(jié)碼教程之裝飾器實(shí)現(xiàn)詳解

    在本篇文章當(dāng)中主要給大家介紹在?cpython?當(dāng)中一些比較常見(jiàn)的字節(jié)碼,從根本上理解?python?程序的執(zhí)行。在本文當(dāng)中主要介紹一些?python?基本操作的字節(jié)碼,并且將從字節(jié)碼的角度分析函數(shù)裝飾器的原理
    2023-04-04
  • python使用response.read()接收json數(shù)據(jù)的實(shí)例

    python使用response.read()接收json數(shù)據(jù)的實(shí)例

    今天小編就為大家分享一篇python使用response.read()接收json數(shù)據(jù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • python selenium瀏覽器復(fù)用技術(shù)的使用

    python selenium瀏覽器復(fù)用技術(shù)的使用

    本文主要介紹了python selenium瀏覽器復(fù)用技術(shù)的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • python numpy格式化打印的實(shí)例

    python numpy格式化打印的實(shí)例

    今天小編就為大家分享一篇python numpy格式化打印的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • python基礎(chǔ)之多態(tài)

    python基礎(chǔ)之多態(tài)

    這篇文章主要介紹了python多態(tài),實(shí)例分析了Python中返回一個(gè)返回值與多個(gè)返回值的方法,需要的朋友可以參考下
    2021-10-10
  • 使用Matplotlib創(chuàng)建基本圖表的詳細(xì)指南

    使用Matplotlib創(chuàng)建基本圖表的詳細(xì)指南

    Matplotlib 是一個(gè)功能強(qiáng)大的 Python 庫(kù),用于創(chuàng)建各種類型的圖表和可視化,在本文中,我們將提供一個(gè)完整的指南,介紹如何使用 Matplotlib 創(chuàng)建基本的圖表,包括折線圖、散點(diǎn)圖、柱狀圖和餅圖,感興趣的小伙伴跟著小編一起來(lái)看看吧
    2024-05-05
  • Python基于checksum計(jì)算文件是否相同的方法

    Python基于checksum計(jì)算文件是否相同的方法

    這篇文章主要介紹了Python基于checksum計(jì)算文件是否相同的方法,涉及Python針對(duì)二進(jìn)制文件的讀取與判定技巧,需要的朋友可以參考下
    2015-07-07
  • Python腳本支持OC代碼重構(gòu)模塊調(diào)用關(guān)系分析實(shí)踐

    Python腳本支持OC代碼重構(gòu)模塊調(diào)用關(guān)系分析實(shí)踐

    在軟件開(kāi)發(fā)中,經(jīng)常會(huì)遇到一些代碼問(wèn)題,例如邏輯結(jié)構(gòu)復(fù)雜、依賴關(guān)系混亂、代碼冗余、不易讀懂的命名等,這些問(wèn)題可能導(dǎo)致代碼的可維護(hù)性下降,增加維護(hù)成本,同時(shí)也會(huì)影響到開(kāi)發(fā)效率,本文以Python實(shí)現(xiàn)自動(dòng)化的工具,支持代碼重構(gòu)過(guò)程的實(shí)踐
    2023-10-10
  • Python腳本實(shí)現(xiàn)網(wǎng)卡流量監(jiān)控

    Python腳本實(shí)現(xiàn)網(wǎng)卡流量監(jiān)控

    這篇文章主要介紹了Python腳本實(shí)現(xiàn)網(wǎng)卡流量監(jiān)控,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2015-02-02

最新評(píng)論

酒泉市| 廊坊市| 威海市| 泸州市| 田林县| 玛沁县| 灵台县| 石泉县| 镇坪县| 观塘区| 读书| 藁城市| 江源县| 广西| 广河县| 承德市| 长寿区| 松潘县| 山东省| 北京市| 伊吾县| 武穴市| 渑池县| 石楼县| 江津市| 彩票| 繁昌县| 香河县| 鄄城县| 女性| 天台县| 兴仁县| 桐柏县| 遂宁市| 大港区| 淄博市| 梓潼县| 易门县| 酒泉市| 山阳县| 龙陵县|