-
Notifications
You must be signed in to change notification settings - Fork 0
/
VBoxGetIP
executable file
·477 lines (384 loc) · 10.7 KB
/
VBoxGetIP
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!/bin/bash
# -------------------------------------------------------------------
#
# Shell program to Bash Script Template.
#
# Copyright 2016, <gabodevelop@gmail.com>.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# Description:
#
#
# NOTE: You must be the superuser to run this script.
# (WARNING!: Contains security info. Do not set world-readable.)
#
# Usage:
#
# script_tpl_1.sh [ -h | --help ] [-a] [-b argom]
#
# Options:
#
# -h, --help Display this help message and exit.
#
# Revision History:
#
# 04/07/2016 Start date
#
# -------------------------------------------------------------------
# -------------------------------------------------------------------
# Constants
# -------------------------------------------------------------------
# Impostazione di lingua
LANG=it_IT.UTF-8
export LANG
PROGNAME=$(basename $0)
PRG=`basename $0 .sh` # senza eventuale .sh
VERSION="0.0.1"
VER="1.0"
LAST_UPD="17-11-2006"
COL_UL=`tput smul`
COL_BLINK=`tput blink`
ACTDIR=`pwd`
SCRIPTDIR=$(dirname $0)
OGGI=`date +%Y%m%d`
OGGI_ITA=`date +%d-%m-%Y`
ORA_ITA=`date +%H:%M:%S`
if [ -z "$HOSTNAME" ]; then
HOSTNAME=`hostname`
fi
# -------------------------------------------------------------------
# Base Variables
# -------------------------------------------------------------------
LOGDIR="/var/log"
LOGFILE="${LOGDIR}/${PRG}.log"
# -------------------------------------------------------------------
# Standard Functions
# -------------------------------------------------------------------
function clean_up
{
# -----------------------------------------------------------------------
# Function to remove temporary files and other housekeeping
# No arguments
# -----------------------------------------------------------------------
rm -rf ${TMPDIR}
}
function error_exit
{
# -----------------------------------------------------------------------
# Function for exit due to fatal program error
# Accepts 1 argument:
# string containing descriptive error message
# -----------------------------------------------------------------------
echo "${PROGNAME}: ${1:-"Unknown Error"}" >&2
clean_up
exit 1
}
function graceful_exit
{
# -----------------------------------------------------------------------
# Function called for a graceful exit
# No arguments
# -----------------------------------------------------------------------
clean_up
exit $1
}
function signal_exit
{
case $1 in
INT) echo "$PROGNAME: Program aborted by user" >&2
clean_up
exit
;;
TERM) echo "$PROGNAME: Program terminated" >&2
clean_up
exit
;;
*) error_exit "$PROGNAME: Terminating on unknown signal"
;;
esac
}
function make_temp_files
{
# Use user's local tmp directory if it exists
if [ -d ~/tmp ]; then
TEMP_DIR=~/tmp
else
TEMP_DIR=/tmp
fi
# Temp file for this script, using paranoid method of creation to
# insure that file name is not predictable. This is for security to
# avoid "tmp race" attacks. If more files are needed, create using
# the same form.
TMPDIR=$(mktemp -q -d "${TEMP_DIR}/${PROGNAME}.$$.XXXXXX")
if [ "$TMPDIR" = "" ]; then
error_exit "cannot create temp file!"
fi
TEMP_FILE1="$TMPDIR/tmp_file1"
TEMP_FILE2="$TMPDIR/tmp_file2"
TEMP_FILE3="$TMPDIR/tmp_file3"
}
function usage
{
# -----------------------------------------------------------------------
# Function to display usage message (does not exit)
# No arguments
# -----------------------------------------------------------------------
echo "Usage: ${PROGNAME} [OPTIONS ...] [FILE]..."
echo " type '${PROGNAME} -h' for more help"
}
function helptext
{
# -----------------------------------------------------------------------
# Function to display help message for program
# No arguments
# -----------------------------------------------------------------------
local tab=$(echo -en "\t\t")
cat <<- -EOF-
${PROGNAME} ver. ${VERSION}
This is a script to read IP address of a running VM.
$(usage)
OPTIONS:
-h, --help Display this help message and exit.
-EOF-
}
function root_check
{
#####
# Function to check if user is root
# No arguments
#####
if [ "$(id | sed 's/uid=\([0-9]*\).*/\1/')" != "0" ]; then
error_exit "You must be the superuser to run this script."
fi
}
function sino()
{
SINO=""
DEF=""
TMP=""
MSG=`echo "$1" | cut -c1`
if [ "$MSG" = "-" ]; then
MSG=""
else
MSG="$1"
fi
if [ -z "$2" ]; then
TMP="$1"
else
TMP="$2"
fi
TMP=`echo $TMP | tr [:lower:] [:upper:]`
DEF=`echo $TMP | cut -c2`
echo -n "$1 :"
while [ "$SINO" != "S" -a "$SINO" != "N" ]; do
read SINO
[ -z "$SINO" ] && SINO=$DEF
SINO=`echo $SINO | tr [:lower:] [:upper:]`
done
}
function proseguo()
{
local RISP=""
while [ "$RISP" != "S" ]; do
echo -ne "\n Proseguo ('S' per Si, CTRL+C per interrompere) : "
read RISP
# RISP=`echo $RISP | tr [:lower:] [:upper:]`
done
}
function pinvio()
{
echo -ne "\n Press [ENTER] to continue. "
read RISP
echo " "
}
function valuta_parametri() {
# Si raccoglie la stringa generata da getopt.
STRINGA_ARGOMENTI=`getopt -o hB:s::S -l help,backup-dir:,backup-directory:,savelast::,simulate -- "$@"`
# Inizializzazione parametri di default
SIMULATE=""
SAVELAST=""
BACKUP_USER_DIR=""
# Si trasferisce nei parametri $1, $2,...
eval set -- "$STRINGA_ARGOMENTI"
while true ; do
case "$1" in
-h|--help)
shift
helptext
graceful_exit
;;
-S|--simulate)
shift
SIMULATE="Yes"
;;
-B|--backup-dir*)
# echo "Opzione b, argomento «$2»" # debug
[ -z "$2" ] && graceful_exit 99
BACKUP_USER_DIR="$2"
shift 2
;;
-s|--savelast)
case "$2" in
"") echo "Opzione s/savelast, senza argomenti" # debug
shift 2
graceful_exit 1 # ecc. ecc.
;;
*)
SAVELAST="$2" # ecc..
shift 2
;;
esac
;;
--) shift
break
;;
*) echo "Errore imprevisto!"
exit 1
;;
esac
done
# ARG_RESTANTI="$@"
ARG_RESTANTI=()
for i in `seq 1 $#`
do
eval a=\$$i
# echo "$i) $a"
ARG_RESTANTI[$i]="$a"
done
# [ -z "$ARG_RESTANTI" ] && help "$PRG: ${COL_LTRED}ERRORE${COL_RESET}: pochi parametri!\n" 99
}
# -------------------------------------------------------------------
# Custom Functions - Da qui le funzioni personalizzate per questo
# script
# -------------------------------------------------------------------
function funzione_tpl
{
# ------------------------------------------------------------------------
# uso della funzione:
# Arguments:
# 1 arg1 (required)
# 2 arg2 (optional)
# ------------------------------------------------------------------------
# Fatal error if required arguments are missing
if [ "$1" = "" ]; then
error_exit "funzione_tpl: missing argument 1"
fi
# Function Logic here
return
} # end of funzione_tpl
# -------------------------------------------------------------------
# Program starts here
# -------------------------------------------------------------------
##### Initialization And Setup #####
# Set file creation mask so that all files are created with 600 permissions.
umask 066
# Eventuale root_check
# root_check
# Trap TERM, HUP, and INT signals and properly exit
trap "signal_exit TERM" TERM HUP
trap "signal_exit INT" INT
# Create temporary file(s)
make_temp_files
##### Command Line Processing #####
if [ "$1" = "--help" ]; then
helptext
graceful_exit
fi
# Posso usare questa chiamata per gli argomenti
# oppure quello + semplice che si vede dopo
# valuta_parametri $@
# -- se uso questa, alla fine ho i parametri restanti in : $ARG_RESTANTI
valuta_parametri $@
##### Main Logic #####
# echo "Arg restanti = $@"
# echo "arg a = $varopt_a, arg b = $varopt_b, arg c = $varopt_c"
LISTRUNNINGVMS_COMMAND="/usr/bin/VBoxManage list runningvms "
# echo "Arg restanti = " ${ARG_RESTANTI[*]} "(" ${#ARG_RESTANTI[*]} ")"
# echo "SIMULATE=$SIMULATE SAVELAST=$SAVELAST BACKUP_USER_DIR=$BACKUP_USER_DIR"
runningvms=`$LISTRUNNINGVMS_COMMAND | wc -l`
if [ $runningvms -eq 0 ]; then
echo "$PRG: error: No running VirtualMachine found."
graceful_exit
fi
params=${#ARG_RESTANTI[*]}
$LISTRUNNINGVMS_COMMAND \
| sed "s/ {.*//" | cut -c2- | rev | cut -c2- | rev \
> $TEMP_FILE1
# if [ $params -eq 0 ]; then
# dai lista macchine e fai scegliere ....
:
vms=()
ciclo=0
while read line
do
vms[$ciclo]="$line"
((ciclo++))
done < $TEMP_FILE1
# wc -l $TEMP_FILE1
# echo ${#vms[@]}
# GABODebug - inizio
echo "Here are VMs:"
for ((i=0;i<${#vms[@]};i++))
do
echo $((i+1))") " ${vms[$i]}
done
echo
# GABODebug - fine
# fi
risp=""
numvm=${#vms[@]}
while [ -z "$risp" ]
do
echo -ne "VM Number (0=exit) : "
read risp
if [ "$risp" = "0" ]; then
graceful_exit
break
fi
echo -n $risp | sed "s/\s*//g" | grep -Pq "\d+"
isnum=$?
echo -n $risp | sed "s/\s*//g" | grep -Pq "\D+"
isalp=$?
# echo isnum = $isnum
# echo isalp = $isalp
if [ $isnum -eq 0 -a $isalp -ne 0 ]; then # Ok son solo cifre numeriche
scelta=$((risp+0))
else
echo "Error: choice must be numeric."
echo
risp=""
continue
fi
if [ $scelta -gt $numvm -o $scelta -lt 1 ]; then
echo "Value is out of the allowed range. (1-$numvm)"
echo
risp=""
continue
fi
[ $scelta -gt 0 ] && ((scelta--))
vmname="${vms[$scelta]}"
done
IP=`VBoxManage guestproperty enumerate "$vmname" | grep "Net\/1\/V4\/IP" | grep -o "value: [^,]*" | cut -f2 -d":" | tr -d [:blank:]`
if [ -n "$IP" ]; then
echo "$vmname IP is $IP"
else
# VBoxManage guestproperty enumerate <vmname>"
property=`VBoxManage guestproperty enumerate "$vmname" | grep -io "/.*Net.*v4.*Ip" | tr -d [:blank:]`
if [ -z "$property" ]; then
echo "Sorry, IP cannot be determined for VM ${vmname}."
echo "Maybe Virtualbox Guest Addons not installed?"
fi
# .... es .... VBoxManage guestproperty get <vmname> "/VirtualBox/GuestInfo/Net/0/V4/IP"
# VBoxManage guestproperty get "$vmname" "/VirtualBox/GuestInfo/Net/0/V4/IP"
VBoxManage guestproperty get "$vmname" "$property"
fi
graceful_exit