Created
February 1, 2019 06:35
-
-
Save yv84/aea400b097fd6f079c0375e771d25c33 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#python3\n", | |
"from subprocess import check_output\n", | |
"from xml.etree.ElementTree import fromstring\n", | |
"from ipaddress import IPv4Interface, IPv6Interface\n", | |
"import time\n", | |
"\n", | |
"def getNics() :\n", | |
"\n", | |
" cmd = 'wmic.exe nicconfig where \"IPEnabled = True\" get ipaddress,MACAddress,IPSubnet,DNSHostName,Caption,DefaultIPGateway /format:rawxml'\n", | |
" xml_text = check_output(cmd, creationflags=8)\n", | |
" xml_root = fromstring(xml_text)\n", | |
"\n", | |
" nics = []\n", | |
" keyslookup = {\n", | |
" 'DNSHostName' : 'hostname',\n", | |
" 'IPAddress' : 'ip',\n", | |
" 'IPSubnet' : '_mask',\n", | |
" 'Caption' : 'hardware',\n", | |
" 'MACAddress' : 'mac',\n", | |
" 'DefaultIPGateway' : 'gateway',\n", | |
" }\n", | |
"\n", | |
" for nic in xml_root.findall(\"./RESULTS/CIM/INSTANCE\") :\n", | |
" # parse and store nic info\n", | |
" n = {\n", | |
" 'hostname':'',\n", | |
" 'ip':[],\n", | |
" '_mask':[],\n", | |
" 'hardware':'',\n", | |
" 'mac':'',\n", | |
" 'gateway':[],\n", | |
" }\n", | |
" for prop in nic :\n", | |
" name = keyslookup[prop.attrib['NAME']]\n", | |
" if prop.tag == 'PROPERTY':\n", | |
" if len(prop):\n", | |
" for v in prop:\n", | |
" n[name] = v.text\n", | |
" elif prop.tag == 'PROPERTY.ARRAY':\n", | |
" for v in prop.findall(\"./VALUE.ARRAY/VALUE\") :\n", | |
" n[name].append(v.text)\n", | |
" nics.append(n)\n", | |
"\n", | |
" # creates python ipaddress objects from ips and masks\n", | |
" for i in range(len(n['ip'])) :\n", | |
" arg = '%s/%s'%(n['ip'][i],n['_mask'][i])\n", | |
" if ':' in n['ip'][i] : n['ip'][i] = IPv6Interface(arg)\n", | |
" else : n['ip'][i] = IPv4Interface(arg)\n", | |
" del n['_mask']\n", | |
"\n", | |
" return nics\n", | |
"\n", | |
"# Import smtplib for the actual sending function\n", | |
"import smtplib\n", | |
"import io\n", | |
"import smtplib\n", | |
"import email.message\n", | |
"\n", | |
"# Import the email modules we'll need\n", | |
"from email.mime.text import MIMEText\n", | |
"\n", | |
"def send_mail(text):\n", | |
" # me == the sender's email address\n", | |
" # you == the recipient's email address\n", | |
" me = '[email protected]'\n", | |
" you = '[email protected]'\n", | |
" msg = email.message.Message()\n", | |
" msg['subject'] = 'ip'\n", | |
" msg['subject'] = 'ip'\n", | |
" msg['from'] = me\n", | |
" msg['to'] = you\n", | |
" msg.set_payload(text, 'utf-8')\n", | |
"\n", | |
" # Send the message via our own SMTP server, but don't include the\n", | |
" # envelope header.\n", | |
" #s = smtplib.SMTP('localhost')\n", | |
" #s.sendmail(me, [you], msg.as_string())\n", | |
" #s.quit()\n", | |
"\n", | |
" con = smtplib.SMTP_SSL('smtp.yandex.ru')\n", | |
" con.login('[email protected]', 'password')\n", | |
" con.send_message(msg)\n", | |
" con.quit()\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import time\n", | |
"nics = None\n", | |
"while (True):\n", | |
" if nics != getNics():\n", | |
" try:\n", | |
" nics = getNics()\n", | |
" send_mail(str(getNics()))\n", | |
" except Exception as e:\n", | |
" print(e)\n", | |
" time.sleep(500)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment