|
| 1 | +#! /usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | + |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import xlwt |
| 8 | +import time |
| 9 | +import json |
| 10 | +import urllib |
| 11 | +import urllib2 |
| 12 | +import datetime |
| 13 | +import paramiko |
| 14 | +import commands |
| 15 | + |
| 16 | + |
| 17 | +reload(sys) |
| 18 | +sys.setdefaultencoding("utf-8") |
| 19 | + |
| 20 | +username = 'root' |
| 21 | +password = '123456' |
| 22 | + |
| 23 | +url_idc = 'http://www.baidu.com/assetInfo' |
| 24 | +url_clound = 'http://www.tengxun.com/cloudAsset' |
| 25 | + |
| 26 | +def GetAllSeal(url): |
| 27 | + user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' |
| 28 | + if url == url_clound: |
| 29 | + values = {'test': 'demo'} |
| 30 | + elif url == url_idc: |
| 31 | + values = {'product': u'运维'} |
| 32 | + else: |
| 33 | + return 'URL is Error !!!' |
| 34 | + headers = {'User-Agent': user_agent} |
| 35 | + data = urllib.urlencode(values) |
| 36 | + req = urllib2.Request(url, data, headers) |
| 37 | + response = urllib2.urlopen(req) |
| 38 | + res = response.read() |
| 39 | + a = json.loads(res) |
| 40 | + return a |
| 41 | + |
| 42 | + |
| 43 | +def set_style(name, height, bold=False): |
| 44 | + style = xlwt.XFStyle() |
| 45 | + |
| 46 | + font = xlwt.Font() |
| 47 | + font.name = name |
| 48 | + font.bold = bold |
| 49 | + font.color_index = 4 |
| 50 | + font.height = height |
| 51 | + |
| 52 | + style.font = font |
| 53 | + return style |
| 54 | + |
| 55 | +def WriteDateExcel(): |
| 56 | + Tm = datetime.datetime.now().strftime('%Y-%m-%d') |
| 57 | + urls = [url_clound, url_idc] |
| 58 | + wbk = xlwt.Workbook(encoding='utf-8') |
| 59 | + for url in urls: |
| 60 | + result = GetAllSeal(url) |
| 61 | + if url == url_clound: |
| 62 | + sheet1 = wbk.add_sheet(u'云资产扫描结果', cell_overwrite_ok=True) |
| 63 | + row0 = [u'业务', u'负责人', u'外网IP', u'内网IP', u'操作系统', u'扫描结果'] |
| 64 | + for x in xrange(len(row0)): |
| 65 | + sheet1.write(0, x, row0[x], set_style('Times New Roman',220,True)) |
| 66 | + for i in xrange(len(result)): |
| 67 | + conn = paramiko.SSHClient() |
| 68 | + conn.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 69 | + try: |
| 70 | + conn.connect(hostname = result[i]['WIp'], username = 'root', timeout = 1) |
| 71 | + stdin, stdout, stderr = conn.exec_command("iptables -nvL|grep -E '10.0.0.0/8|10.12.0.0/16'") |
| 72 | + cdt = stdout.readlines() |
| 73 | + ret = ''.join(cdt) |
| 74 | + except: |
| 75 | + ret = u'无法连接' |
| 76 | + contl = [result[i]['Name'], result[i]['Responser'], result[i]['WIp'], result[i]['LIp'], result[i]['os'], ret] |
| 77 | + for j in xrange(len(contl)): |
| 78 | + sheet1.write(i+1, j, contl[j]) |
| 79 | + elif url == url_idc: |
| 80 | + sheet2 = wbk.add_sheet(u'物理机扫描结果', cell_overwrite_ok=True) |
| 81 | + row0 = [u'项目', u'负责人', u'外网IP', u'内网IP', u'操作系统', u'扫描结果'] |
| 82 | + for x in xrange(len(row0)): |
| 83 | + sheet2.write(0, x, row0[x], set_style('Times New Roman',220,True)) |
| 84 | + for i in xrange(len(result)): |
| 85 | + conn = paramiko.SSHClient() |
| 86 | + conn.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 87 | + try: |
| 88 | + conn.connect(hostname = result[i]['wIp'], username = 'root', timeout = 1) |
| 89 | + stdin, stdout, stderr = conn.exec_command("iptables -nvL|grep -E '10.0.0.0/8|10.12.0.0/16'") |
| 90 | + cdt = stdout.readlines() |
| 91 | + ret = ''.join(cdt) |
| 92 | + except: |
| 93 | + ret = u'无法连接' |
| 94 | + contl = [result[i]['Name'], result[i]['Owner'], result[i]['wIp'], result[i]['lIp'], result[i]['os'], ret] |
| 95 | + for j in xrange(len(contl)): |
| 96 | + sheet2.write(i+1, j, contl[j]) |
| 97 | + else: |
| 98 | + return 'URL is Error !!!' |
| 99 | + wbk.save('checkseal_{0}.xls'.format(Tm)) |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + WriteDateExcel() |
0 commit comments