# -*- coding: utf-8 -*-
import requests
import json
import os
from pathlib import Path

from handle_ssh_multi import SShMulti

base_dir = str(Path(__file__).parent)
with open(base_dir + '/yunlifang.json') as f:
    ecs_list  = json.loads(f.read())

def after_call(request, ppp_ip):
    req = request.args[0]
    if isinstance(ppp_ip, str):
        ppp_ip = ppp_ip.strip()
    else:
        print('hostname: %s - result: %s - resp: %s' % (req['hostname'], str(ppp_ip)))
        return

    cmd = 'curl --max-time 5 -o /dev/null -s -w "%{http_code}\n" -x http://ylf2018pxy:2w6XeXA*@' + ppp_ip + ':8016 --proxy-anyauth https://aks.jdpay.com/robots.txt'
    #cmd = 'curl --max-time 5 -x http://ylf2018pxy:2w6XeXA*@' + ppp_ip + ':8016 --proxy-anyauth https://aks.jdpay.com/robots.txt'
    #cmd = 'curl -x http://erpinnernative:Mo2.mN*34kjk@' + ppp_ip + ':9098 --proxy-anyauth  https://seller.id.shopee.cn'
    http_code = os.popen(cmd).read()
    print('hostname: %s - result: %s - httpCode: %s - cmd - %s' % (req['hostname'], str(ppp_ip), http_code, cmd))
    print("\n\n")


def pathHostTxt(filePath):
    vpsMap = {}
    with open(filePath, 'r', encoding='utf-8', newline='') as f:
        for line in f:
            if len(line.strip()) <= 0:
                continue
            if line.startswith('#'):
                continue
            host_arr = line.split("\t")
            if len(host_arr) < 3:
                continue

            host_arr = list(filter(None,host_arr))    
            host_arr = [one.strip() for one in host_arr]
        
            hostname, os, desc, ipPort, user, password, adslUser, adslPasswrd, gmtExpire = host_arr
            ip, port = ipPort.split(':')
            ecs = {'ip': ip, 'port': int(port), 'hostname': hostname, 'password': password, 'desc': desc, 'expires': gmtExpire}
            vpsMap[hostname] = ecs

    return vpsMap

if __name__ == "__main__":
    cmd = 'ip -4  addr show ppp0 |  grep -oP \'(?<=inet\s)\d+(\.\d+){3}\''
    #cmd = 'vim  /etc/yum/pluginconf.d/fastestmirror.conf'
    #cmd = 'sed -i "s/apc.shm_size=64M/apc.shm_size=256M/g" /alidata/server/php/etc/php.ini'
    #cmd = 'systemctl status squid'

    ssh2 = SShMulti()
    dir_path = os.path.dirname(os.path.realpath(__file__))
    filePath = dir_path + '/zndata.txt'

    vpsMap = pathHostTxt(filePath)

    ecs_list = []
    for key in vpsMap:
        ecs = vpsMap[key]
        
        if ecs['hostname'] not in ['46093W6SZOQ', '46093J7L0U4']:
           continue

        ecs['user'] = 'root'
        hostname = 'MM_' + ecs['hostname']
        #ecs['cmd'] = 'wget -qO- https://ae-rt.oss-cn-beijing.aliyuncs.com/proxy/install_ms.sh | /bin/bash -s ' + hostname + ' >> install.txt'
        #ecs['cmd'] = 'wget -qO- https://ae-rt.oss-cn-beijing.aliyuncs.com/proxy/install_yum.sh | /bin/bash'
        ecs['cmd'] = 'wget -qO- https://ae-rt.oss-cn-beijing.aliyuncs.com/proxy/zndata/install_pc.sh | /bin/bash'
        ecs['cmd'] = 'systemctl status squid'
        #ecs['cmd'] = 'mv epel.repo epel.repo.bak && curl "https://oss.91vps.com/ssh/epel-7.repo" > /etc/yum.repos.d/epel.repo'
        ecs['cmd'] = 'ifconfig'    
        #ecs['cmd'] = 'ps aux | grep sbin/pppoe | grep -v grep | awk \'{print $2}\' | xargs kill'    
        ecs_list.append(ecs)

    #print(ecs_list)
    ssh2.mutil_exec(ecs_list)