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)
