app.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import os
  2. import time
  3. import paramiko
  4. from flask import Flask, render_template, request, url_for, redirect
  5. # coding=utf-8
  6. import paramiko
  7. app = Flask(__name__)
  8. host = '60.250.156.230'
  9. port = '22'
  10. username='gs1801'
  11. password='g53743001'
  12. @app.route("/",methods=['POST', 'GET'])
  13. def index():
  14. return render_template("index.html", title='ERP WEB')
  15. @app.route("/check_ping/",methods=['POST', 'GET'])
  16. def check_ping():
  17. if request.method == 'POST':
  18. ip = '192.168.50.153'
  19. command = 'ping ' + ip
  20. ping = os.popen(command)
  21. context = ping.read()
  22. print(context)
  23. #status = context.find('目的地主機無法連線')
  24. #print("如果搜尋不到'目的地主機無法連線',會顯示-1:", status)
  25. return render_template('index.html',context=context)
  26. return render_template("index.html")
  27. @app.route("/check_gogs/",methods=['POST', 'GET'])
  28. def check_gogs():
  29. if request.method == 'POST':
  30. ssh = paramiko.SSHClient()
  31. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  32. ssh.connect(hostname=host, port=port, username=username, password=password)
  33. stdin, stdout, stderr = ssh.exec_command("ps -ef | grep -E '[g]ogs';", get_pty=True)
  34. gogs = stdout.read().decode('utf-8')
  35. print(gogs)
  36. '''
  37. check_gogs = result.find('gogs.sh')
  38. test = result.find('root')
  39. #print('check: ', check_gogs)
  40. #print('test: ', test)
  41. if check_gogs != -1 and test != -1:
  42. print('gogs正常執行')
  43. else:
  44. print('gogs沒有執行')
  45. '''
  46. ssh.close()
  47. return render_template('index.html',gogs=gogs)
  48. return render_template("index.html")
  49. @app.route('/check_watchdog',methods=['POST', 'GET'])
  50. def check_watchdog():
  51. if request.method == 'POST':
  52. ssh = paramiko.SSHClient()
  53. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  54. ssh.connect(hostname=host, port=port, username=username, password=password)
  55. stdin, stdout, stderr = ssh.exec_command("ps -ef | grep -E '[p]ython3';", get_pty=True)
  56. watchdog = stdout.read().decode('utf-8')
  57. print(watchdog)
  58. '''
  59. check_watchdog = result.find('python3')
  60. test = result.find('root')
  61. #print('check: ', check_watchdog)
  62. #print('test: ', test)
  63. if check_watchdog != -1 and test != -1:
  64. print('watchdog正常執行')
  65. else:
  66. print('watchdog沒有執行')
  67. '''
  68. ssh.close()
  69. return render_template('index.html', watchdog=watchdog)
  70. return render_template("index.html")
  71. @app.route('/check_mosquitto',methods=['POST', 'GET'])
  72. def check_mosquitto():
  73. if request.method == 'POST':
  74. ssh = paramiko.SSHClient()
  75. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  76. ssh.connect(hostname=host, port=port, username=username, password=password)
  77. stdin, stdout, stderr = ssh.exec_command("ps -ef | grep -E '[m]osquitto';", get_pty=True)
  78. mosquitto = stdout.read().decode('utf-8')
  79. print(mosquitto)
  80. '''
  81. check_mosquitto = result.find('/etc/mosquitto/mosquitto.conf')
  82. #print('check: ', check_mosquitto)
  83. #print('test: ', test)
  84. if check_mosquitto != -1 :
  85. print('mosquitto正常執行')
  86. else:
  87. print('mosquitto沒有執行')
  88. '''
  89. ssh.close()
  90. return render_template('index.html', mosquitto=mosquitto)
  91. return render_template("index.html")
  92. if __name__ == '__main__':
  93. app.run(debug=True, port=5555)