-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_json.py
More file actions
executable file
·67 lines (60 loc) · 3.05 KB
/
search_json.py
File metadata and controls
executable file
·67 lines (60 loc) · 3.05 KB
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
#!/usr/bin/env python3
import sys, json, argparse, re
from pprint import pprint
from passwd import HomeDir
#HomeDir = '/home/akrush/telegram_bot/'
def search_vm_json( args ):
argslist = args.split()
parser = argparse.ArgumentParser( )
# parameters
parser.add_argument('-ip', dest='ip', help="guest ip")
parser.add_argument('-mac', dest='mac', help="guest mac")
parser.add_argument('-esxi', dest='esxi', help="esxi host")
parser.add_argument('-status', dest='status', help="vm power status")
parser.add_argument('-note', '-desc', dest='note', help="note")
parser.add_argument('-name', dest='name', help="vm name")
parser.add_argument('-path', dest='path', help="vm storage path")
parser.add_argument('-snap', dest='snap', help="vm whitch snapshots")
parser.add_argument('-all', dest='all', help="full info", action='store_true')
args = parser.parse_args( argslist )
jsonfile = HomeDir + 'inventory_json/vmware.json'
json_data=open(jsonfile)
jdata = json.load(json_data)
json_data.close()
res = {}
for key, value in jdata.items():
if args.name is not None and re.match( args.name , value['Name'] ):
res[key] = value
#if args.ip is not None and ( re.match( args.ip+"'", str( value['guest ip'] ) ) or args.ip in value['Note']):
if args.ip is not None:
inip = 0 # переменная обозначающая то что мы в цикле нашли нужный ip адрес
for i in value['guest ip']:
if re.match( args.ip, i ):
inip = 1
if inip != 1: # если ip не найдет пробуем его найти в Note у машины
if re.findall( args.ip, str( value['Note'] ) ):
inip = 1
if inip == 1:
res[key] = value
elif args.mac is not None:
inmac = 0
for m in value['macaddress']:
if re.match( args.mac, m ):
inmac = 1
#( re.match( args.mac, str( value['macaddress'] ) ) or args.mac in value['Note'] ):
if inmac == 1:
res[key] = value
elif args.esxi is not None and args.status is None and re.match( args.esxi, str( value['esxi'] )):
res[key] = value
elif args.esxi is not None and args.status == '1' and re.match( args.esxi, str( value['esxi'] ) ) and value['Status'] == "poweredOn":
res[key] = value
elif args.esxi is not None and args.status == '0' and re.match( args.esxi, str( value['esxi'] ) ) and value['Status'] == "poweredOff":
res[key] = value
elif args.note is not None and re.findall( args.note, str( value['Note'] ) ):
res[key] = value
elif args.path is not None and re.match( args.path, str( value['path'] ) ):
res[key] = value
elif args.snap is not None and len(value['snapshot']) > int(args.snap):
res[key] = value
return ( res )
#print (stg(search_vm_json('-esxi ars-vm12.srv.local -status 1')))