-
Notifications
You must be signed in to change notification settings - Fork 2
/
vm_findby_ip.py
executable file
·105 lines (77 loc) · 2.4 KB
/
vm_findby_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Saul Bertuccio
# Date: ven 12 luglio 2017
# Elenco tag e categorie per tutte le VM o le VM in una cartella del Vcenter
from __future__ import print_function
# Custom libs
from libs.datacenter import Datacenter
from libs.utils import checkDate, getBaseArgs
from configs.vcenterparams import VcenterParams
from pyVim.task import WaitForTasks
import argparse
import time
import getpass
import sys
import copy
import json
import csv
from datetime import datetime
# DEBUG
from pprint import pprint
def csvAdapt(data):
d = {}
for k, v in data.iteritems():
d[k] = v if type(v) == str else '|'.join(str(x) for x in v)
return d
def getWriter(ftype, fh, fields):
if ftype == 'csv':
csv_writer = csv.DictWriter(fh, fieldnames=fields)
csv_writer.writeheader()
return lambda dict: csv_writer.writerow(csvAdapt(dict))
csv_writer.writerow
elif ftype == 'json':
return lambda dict: fh.write(json.dumps(dict) + '\n')
else:
raise Exception("Errore: parametro tipo %s non supportato" % ftype )
def GetArgs():
"""
Gestione degli argomenti
"""
PARS=VcenterParams()
parser = getBaseArgs(PARS)
parser.add_argument('-i', '--ip', required=True, action='store', help='Ip da ricercare')
args = parser.parse_args()
return args
def main():
# Parsing argomenti
args = GetArgs()
if args.password:
password = args.password
else:
password = getpass.getpass(prompt='Inserire la password per l\'host %s e l\'utente %s: ' % (args.host,args.user))
# Connessione al vcenter e oggetto DataCenter
try:
vmdtc = Datacenter(host=args.host,
user=args.user,
password=password,
port=int(args.port),
datacenter = args.datacenter)
except Exception, e:
sys.stderr.write(str(e))
sys.exit(1)
try:
vms = vmdtc. getVirtualMachineIpAddress(args.ip)
except Exception, e:
sys.stderr.write(str(e))
sys.exit(1)
if not vms:
sys.stderr.write("Attenzione: nessuna Macchina virtuale trovata con ip %s\n" % args.ip)
sys.exit(1)
# Get list of categories name removing default VSPHERE categores
for vm in vms:
print(vm.name)
#
# Avvio l'applicazione
if __name__ == "__main__":
main()