-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip.py
82 lines (53 loc) · 1.63 KB
/
ip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
##ìîíèòîðèíã, òåñòîâûé ñêðèïò äëÿ ïðîâåðêè çíàíèé ïî ïèòîíó
import ipaddress
import subprocess
import platform
import cx_Oracle
import os
usr = 'S-------'
pwd = '---------'
hst='--------.ru'
prt='1521'
db='----'
ro_com='---'
sysobj = '.1.3.6.1.2.1.1.2.0'
def ping(ip):
operating_sys = platform.system()
if operating_sys == 'Windows':
ping_command = 'ping ' + ip + ' -n 1 -w 20'
else:
ping_comand= 'ping ' + ip + ' -c 1'
shell_needed = True if operating_sys == 'Windows' else False
ping_output = subprocess.run(ping_command,shell=shell_needed,stdout=subprocess.PIPE)
success = ping_output.returncode
return True if success == 0 else False
def snmpget(ip, mib):
operating_sys = platform.system()
shell_needed = True if operating_sys == 'Windows' else False
cmd ='snmpwalk -v2c -On -c ' + ro_com + ' ' + ip + ' ' + mib
p = subprocess.Popen(cmd.split(),shell=shell_needed,stdout=subprocess.PIPE)
out,err = p.communicate()
res1 = str(out.strip(), 'utf-8')
res2=res1.split(':')
if len(res1.split('='))>1:
res=res2[1].strip()
else:
res=res2
assert 0 == p.wait()
return res
mydsn=cx_Oracle.makedsn(hst, prt, service_name=db)
mycon = cx_Oracle.connect(user=usr,password=pwd, dsn=mydsn)
mycur = mycon.cursor()
mycur.execute('set role FASTCOM_MAIN identified by --------')
mycur.execute("select ipaddress from tomtel.mn_t_switch WHERE ipaddress like '192.168.%.%'")
myres = mycur.fetchall()
#cnt=len(myres)
#print('All: ' + str(cnt))
for myip in myres :
ip = str(myip[0])
if ping(ip):
print('Host ' + ip + ' snmp out: ' + snmpget(ip,sysobj))
else:
print('host down: ' + ip)
mycur.close()
mycon.close()