使用web ssh 登陸設備---gateone開發之旅

前言:


gateone是一個非常強大的web 開源終端(Gate One is an HTML5 web-based terminal emulator and SSH client.)。 


目前這個項目在github有3614個stars, 439個fork, 26個貢獻者, 可見其受歡迎程度。


項目的github地址: https://github.com/liftoff/GateOne 


gateone文檔: http://liftoff.github.io/GateOne/ 


我們的項目基於gateone做了一些應用和開發,寫這個文章的目的是想和大家一起分享和交流下經驗。


需求:


開發一個界面, 可以通過這個界面來登陸設備,即web ssh。


關閉頁面後(但是沒有logout),下次打開還能回到上次登陸的狀態。


環境:

ubuntu 12.04

python 2.7.6

django 1.7.3

Gateone 1.2




實現:


HTML 代碼

<div id="gateone_container" style="position: relative; width: 100%; height: 50em;margin: 0 auto">

    <div id="gateone"></div>

</div>

<script type="text/javascript" src="/static/js/gateone/gateone.js"></script>

JS代碼


    <script type="text/javascript">

        $(document).ready(function(){         

            var ip = “xxxxxxx”;   // 想辦法拿到要登陸的設備的ip地址, 有多種方法, 比如把ip地址放置一個隱藏的input標籤內, 或者通過url的參數行獲取

            var ssh_url = 'ssh://' + ip + ':' + 22;           

            var request = $.ajax({

                url:'/get_auth_obj',   // api認證方式, 參考gateone文檔

                type:"GET",

                dataType:"json",

            });

            request.done(function(auth_info){

                GateOne.init({

                    auth:auth_info.auth,

                    url: auth_info.url,

                    theme:'black',

                    goDiv:'#gateone',
                    disableTermTransitions:'true',

                    autoConnectURL:ssh_url,

                });

            });

 

            GateOne.Base.superSandbox("GateOne.SomePlugin", ["GateOne", "GateOne.Net",  "GateOne.Terminal.Input", "GateOne.Terminal"], function(window, undefined) {

                // this will ensure that modules in superSandbox will load completely first, then execute your code

                // Put your code here

                var location =  ip;              

                GateOne.prefs.autoConnectURL=ssh_url;

                GateOne.prefs.fontSize="100%";

                GateOne.prefs.scrollback = 10000;  // scrollback buffer up to 10,000 lines

                GateOne.Terminal.loadFont("Source Code Pro", "150%");
                GateOne.locations; // Holds the state of all current known/open locations

                GateOne.Net.setLocation(location); // Change locations in the current tab on-the-fly!這裏設置的作用在於記錄和保持ssh登陸的狀態,只要不logout或者斷開session,關閉頁面後打開還會回到上次的狀態

            });

 

        }); // end of document ready

 

 

    </script>


Python代碼


#  django urls.py

("/get_auto_obj, views.generate_gate_one_auth_obj")

#  python 代碼  ,api認證方式, django views

def generate_gate_one_auth_obj(request):

    import time, hmac, hashlib, json

    user = request.user.username

    gateone_server = GATEONE_SERVER  // 替換成對應的部署gateone的server的ip地址

    secret = GATEONE_API_SECRET  // 替換成對應的api secret ,該信息 存放在gateone的配置文件30api.conf中

    api_key = GATEONE_API_KEY  // 替換成對應的api key ,該信息 存放在gateone的配置文件30api.conf中

    authobj = {

        'api_key': api_key,

        'upn': user,

        'timestamp': str(int(time.time() * 1000)),

        'signature_method': 'HMAC-SHA1',

        'api_version': '1.0'

    }

    my_hash = hmac.new(secret, digestmod=hashlib.sha1)

    my_hash.update(authobj['api_key'] + authobj['upn'] + authobj['timestamp'])

    authobj['signature'] = my_hash.hexdigest()

    auth_info_and_server = {"url": gateone_server, "auth": authobj}

    valid_json_auth_info = json.dumps(auth_info_and_server)

    return HttpResponse(valid_json_auth_info)


其實使用gateone還有許多新的玩法, 比如結合sso登陸,pam驗證登陸, google驗證登陸, 還有 登陸自動執行腳本, log 噴到es等等,以後再慢慢講。 gateone還提供了很詳細的文檔,包括使用文檔和開發文檔。gateone支持插件開發, 它提供了豐富的api, 只要你的開發能力夠強, 你能基於gateone開發出非常強大的工具。


hackstoic 2015-12-26 整理




如果有任何問題,歡迎交流~~~~博客下留言或者email給我 hackstoic#163.com(將#換成@)







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章