# -*- coding: UTF-8 -*-
#author:ailann

import sys,getopt
import subprocess
import time
import os
import IP
import nmap_scan
#读取ip地址大全

def main(argv):
    global provice 
    provice=""
    global opertor 
    opertor=""

    try:
        # 这里的 h 就表示该选项无参数，p:表示 p 选项后需要有参数
        opts, args = getopt.getopt(argv, "hp:o:",["provice=", "opertor="])
    except getopt.GetoptError:
        print 'Error: test.py -p <provice> -o <opertor>'
        
        sys.exit(2)

    for opt, arg in opts:
        if opt == "-h":
            print 'test.py -p <provice> -o <opertor>'
            

            sys.exit()
        elif opt == "-p":
            provice = arg
        elif opt == "-o":
            opertor = arg

    print 'provice : ', provice
    print 'opertor: ', opertor



if __name__ == '__main__':
	

	#判断如果al_result.txt文件存在则删除该文件
	if os.path.exists('al_result.txt'):
		os.system('rm -f al_result.txt')
	#接收参数 em: 安徽 联通
	main(sys.argv[1:])

	#执行系统命令筛选ip
	cmd = 'cat ip_all.txt |grep %s | grep %s  > ip_pro.txt' %(provice,opertor)
	print cmd
	p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
	time.sleep(3)
	IP.ip_segment('ip_pro.txt')
	time.sleep(3)
	nmap_scan.scan_alive('ipf_segment.txt')
