Browse Source

上傳檔案到 'flask/test_paramiko'

fatwolf 3 years ago
parent
commit
08b2cf6e0f
2 changed files with 141 additions and 0 deletions
  1. 105 0
      flask/test_paramiko/app.py
  2. 36 0
      flask/test_paramiko/index.html

+ 105 - 0
flask/test_paramiko/app.py

@@ -0,0 +1,105 @@
+import os
+import time
+import paramiko
+from flask import Flask, render_template, request, url_for, redirect
+
+# coding=utf-8
+import paramiko
+
+
+app = Flask(__name__)
+
+host = '60.250.156.230'
+port = '22'
+username='gs1801'
+password='g53743001'
+
+
+@app.route("/",methods=['POST', 'GET'])
+def index():
+    return render_template("index.html", title='ERP WEB')
+
+@app.route("/check_ping/",methods=['POST', 'GET'])
+def check_ping():
+    if request.method == 'POST':
+        ip = '192.168.50.153'
+        command = 'ping ' + ip
+        ping = os.popen(command)
+        context = ping.read()
+        print(context)
+        #status = context.find('目的地主機無法連線')
+
+        #print("如果搜尋不到'目的地主機無法連線',會顯示-1:", status)
+        return render_template('index.html',context=context)
+    return render_template("index.html")
+
+@app.route("/check_gogs/",methods=['POST', 'GET'])
+def check_gogs():
+    if request.method == 'POST':
+        ssh = paramiko.SSHClient()
+        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
+        ssh.connect(hostname=host, port=port, username=username, password=password)
+        stdin, stdout, stderr = ssh.exec_command("ps -ef | grep -E '[g]ogs';", get_pty=True)
+        gogs = stdout.read().decode('utf-8')
+        print(gogs)
+        '''
+        check_gogs = result.find('gogs.sh')
+        test = result.find('root')
+        #print('check: ', check_gogs)
+        #print('test: ', test)
+        if check_gogs != -1 and test != -1:
+            print('gogs正常執行')
+        else:
+            print('gogs沒有執行')
+        '''
+        ssh.close()
+        return render_template('index.html',gogs=gogs)
+    return render_template("index.html")
+
+@app.route('/check_watchdog',methods=['POST', 'GET'])
+def check_watchdog():
+    if request.method == 'POST':
+        ssh = paramiko.SSHClient()
+        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
+        ssh.connect(hostname=host, port=port, username=username, password=password)
+        stdin, stdout, stderr = ssh.exec_command("ps -ef | grep -E '[p]ython3';", get_pty=True)
+        watchdog = stdout.read().decode('utf-8')
+        print(watchdog)
+        '''
+        check_watchdog = result.find('python3')
+        test = result.find('root')
+        #print('check: ', check_watchdog)
+        #print('test: ', test)
+        if check_watchdog != -1 and test != -1:
+            print('watchdog正常執行')
+        else:
+            print('watchdog沒有執行')
+        '''
+        ssh.close()
+        return render_template('index.html', watchdog=watchdog)
+    return render_template("index.html")
+
+@app.route('/check_mosquitto',methods=['POST', 'GET'])
+def check_mosquitto():
+    if request.method == 'POST':
+        ssh = paramiko.SSHClient()
+        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
+        ssh.connect(hostname=host, port=port, username=username, password=password)
+        stdin, stdout, stderr = ssh.exec_command("ps -ef | grep -E '[m]osquitto';", get_pty=True)
+        mosquitto = stdout.read().decode('utf-8')
+        print(mosquitto)
+        '''
+        check_mosquitto = result.find('/etc/mosquitto/mosquitto.conf')
+        #print('check: ', check_mosquitto)
+        #print('test: ', test)
+        if check_mosquitto != -1 :
+            print('mosquitto正常執行')
+        else:
+            print('mosquitto沒有執行')
+        '''
+        ssh.close()
+        return render_template('index.html', mosquitto=mosquitto)
+    return render_template("index.html")
+
+if __name__ == '__main__':
+    app.run(debug=True, port=5555)

+ 36 - 0
flask/test_paramiko/index.html

@@ -0,0 +1,36 @@
+<!doctype html>
+<html>
+
+<head>
+    
+    <h2>檢查伺服器狀況</h2>  
+
+    
+</head>
+
+<body> 
+
+    <form method="POST" action="{{ url_for('check_ping') }}">
+    	<p><input type="submit" name="send" value="檢查連線"></p>
+    </form>
+    
+
+    <form method="post" action="{{ url_for('check_gogs') }}">
+    <p><input type="submit" name="send" value="檢查gogs"></p>
+    </form>
+
+    <form method="post" action="{{ url_for('check_watchdog') }}">
+    <p><input type="submit" name="send" value="檢查watchdog"></p>
+    </form>
+
+    <form method="post" action="{{ url_for('check_mosquitto') }}">
+    <p><input type="submit" name="send" value="檢查mosquitto"></p>
+    </form>
+
+    <pre>{{context}}</pre>
+    <pre>{{gogs}}</pre>
+    <pre>{{watchdog}}</pre>
+    <pre>{{mosquitto}}</pre>
+</body>    
+
+</html>