-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVaderShell.sh
More file actions
executable file
·497 lines (408 loc) · 17.3 KB
/
VaderShell.sh
File metadata and controls
executable file
·497 lines (408 loc) · 17.3 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
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#!/bin/bash
# Load configuration file
source config.cfg
# Detect operating system
OS_TYPE="$(uname -s)"
case "$OS_TYPE" in
Linux*) OS="Linux";;
Darwin*) OS="macOS";;
*) OS="Unknown";;
esac
# ===================== Logging toggle (drop-in) =====================
# Assumes config.cfg already sourced above.
: "${VADERSHELL_LOG_ENABLED:=1}"
: "${VADERSHELL_LOG_MODE:=tee}"
: "${VADERSHELL_LOG_DIR:=$HOME/.vadershell/logs}"
: "${VADERSHELL_LOG_FILE:=}"
: "${VADERSHELL_TRACE:=0}"
if [[ "$VADERSHELL_LOG_ENABLED" == "1" ]]; then
mkdir -p "$VADERSHELL_LOG_DIR"
SCRIPT_NAME="$(basename "$0")"
START_TS="$(date +"%Y%m%d-%H%M%S")"
if [[ -z "$VADERSHELL_LOG_FILE" ]]; then
VADERSHELL_LOG_FILE="$VADERSHELL_LOG_DIR/${SCRIPT_NAME%.*}_${START_TS}.log"
else
mkdir -p "$(dirname "$VADERSHELL_LOG_FILE")"
fi
case "$VADERSHELL_LOG_MODE" in
tee)
exec > >(tee -a "$VADERSHELL_LOG_FILE") 2>&1
;;
ts-file)
# Console: raw; File: timestamped (pure bash)
exec > >(tee >(bash -c '
f="$1"
while IFS= read -r line; do
printf "[%(%Y-%m-%d %H:%M:%S)T] %s\n" -1 "$line"
done >> "$f"
' _ "$VADERSHELL_LOG_FILE")) 2>&1
;;
ts-both)
# Console + File: timestamped (⚠ single-line prompts may appear late on console)
exec > >(bash -c '
f="$1"
while IFS= read -r line; do
printf "[%(%Y-%m-%d %H:%M:%S)T] %s\n" -1 "$line" | tee -a "$f"
done
' _ "$VADERSHELL_LOG_FILE") 2>&1
;;
none|*)
:
;;
esac
echo "[INFO] Detected OS: $OS"
echo "[INFO] Log enabled: $VADERSHELL_LOG_MODE -> $VADERSHELL_LOG_FILE"
if [[ "$VADERSHELL_TRACE" == "1" ]]; then
export PS4='+ $(date "+%Y-%m-%d %H:%M:%S") ${BASH_SOURCE##*/}:${LINENO}:${FUNCNAME[0]:-main}: '
set -x
fi
fi
# =================== End logging toggle block ======================
declare -A vm_list
declare -i total_vms=0
declare -i total_servers=${#SERVERS[@]}
# Dynamically fetch available VMs
fetch_vm_list() {
vm_list=()
total_vms=0
index=1
while read -r line; do
vm_name=$(echo "$line" | awk -F '"' '{print $2}')
vm_list[$index]="$vm_name"
((index++))
done < <(VBoxManage list vms)
total_vms=${#vm_list[@]}
}
# Fetch IP addresses of servers
fetch_server_ips() {
echo "Resolving IP addresses for servers..."
echo "-------------------------------------"
for i in "${!SERVERS[@]}"; do
IFS=":" read -r name ssh_info <<< "${SERVERS[i]}"
# Use 'host' to get the IP, fallback to 'nslookup' if needed
ip_address=$(host "$name" | awk '/has address/ {print $4}')
if [ -z "$ip_address" ]; then
ip_address=$(nslookup "$name" 2>/dev/null | awk '/Address: / {print $2}' | tail -n1)
fi
if [ -n "$ip_address" ]; then
SERVER_IPS[$i]="$ip_address"
else
SERVER_IPS[$i]="Not Found"
fi
done
}
# Connect to C4-Cluster Node
c4_node() {
read -p "Enter SSH user: " username
read -p "Enter Node ID: " device_id
local ip_suffix=$((100 + device_id - 1))
local ssh_command="sudo ssh -tt \
-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
-o ServerAliveInterval=30 -o ServerAliveCountMax=5 -o TCPKeepAlive=yes \
-i \"$DATACENTER_GW_SSH_KEY_PATH\" krit@$DATACENTER_GW_IP \
\"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
-o ServerAliveInterval=30 -o ServerAliveCountMax=5 -o TCPKeepAlive=yes \
$username@${DATACENTER_BASE_IP}${ip_suffix}\""
eval "$ssh_command"
}
# VM Management Menu
vm_management() {
local vm_name=""
while true; do
fetch_vm_list
echo "++++++++++++++ VM MANAGEMENT MENU ++++++++++++++++"
for index in "${!vm_list[@]}"; do
echo "$index - Manage VM: ${vm_list[$index]}"
done
echo "---------------------------------------------------"
echo "$((total_vms+1)). Exit"
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++"
read choice
if [[ $choice -ge 1 && $choice -le $total_vms ]]; then
vm_name="${vm_list[$choice]}"
echo "Managing VM: $vm_name"
vm_operations "$vm_name"
elif [[ $choice -eq $((total_vms+1)) ]]; then
echo "Exiting VM management."
return
else
echo "Invalid choice, please try again."
fi
done
}
# Function to get the IP of a VirtualBox VM
get_vm_ip() {
local vm_name="$1"
local vm_ip=""
local vm_mac=""
#echo "DEBUG: Fetching IP for VM: $vm_name"
# Try getting the IP from guestproperty (VirtualBox Guest Additions required)
vm_ip=$(VBoxManage guestproperty get "$vm_name" "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk -F: '{print $2}' | tr -d ' ')
if [[ -n "$vm_ip" && "$vm_ip" != "No value set!" ]]; then
#echo "DEBUG: IP from guestproperty -> $vm_ip"
echo "$vm_ip"
return
fi
# Check additional network interfaces (Net/1, Net/2 in case Net/0 is not used)
for i in {1..3}; do
vm_ip=$(VBoxManage guestproperty get "$vm_name" "/VirtualBox/GuestInfo/Net/$i/V4/IP" | awk -F: '{print $2}' | tr -d ' ')
if [[ -n "$vm_ip" && "$vm_ip" != "No value set!" ]]; then
#echo "DEBUG: IP from guestproperty (Net/$i) -> $vm_ip"
echo "$vm_ip"
return
fi
done
# Try getting the IP from showvminfo
vm_ip=$(VBoxManage showvminfo "$vm_name" --machinereadable | grep "GuestIPAddress=" | cut -d'"' -f2)
if [[ -n "$vm_ip" && "$vm_ip" != "No value set!" ]]; then
#echo "DEBUG: IP from showvminfo -> $vm_ip"
echo "$vm_ip"
return
fi
# Get VM MAC address (formatted correctly for ARP lookup)
vm_mac=$(VBoxManage showvminfo "$vm_name" --machinereadable | grep "macaddress1=" | cut -d'"' -f2 | sed 's/\(..\)/\1:/g; s/:$//')
#echo "DEBUG: VM MAC Address -> $vm_mac"
# Try checking ARP table using formatted MAC address
vm_ip=$(arp -a | grep -i "$vm_mac" | awk '{print $2}' | tr -d '()')
if [[ -n "$vm_ip" ]]; then
#echo "DEBUG: IP from ARP table -> $vm_ip"
echo "$vm_ip"
return
fi
# Try checking DHCP leases (if VM is using NAT)
vm_ip_list=$(VBoxManage list dhcpservers | grep -Eo '10\.[0-9]+\.[0-9]+\.[0-9]+')
for ip in $vm_ip_list; do
if ping -c 1 -W 1 "$ip" &>/dev/null; then
vm_ip="$ip"
#echo "DEBUG: Found active IP via ping -> $vm_ip"
break
fi
done
# Final check
if [[ -n "$vm_ip" ]]; then
#echo "DEBUG: Selected IP -> $vm_ip"
echo "$vm_ip"
#else
#echo "DEBUG: No IP found for VM $vm_name"
#echo "No IP Found"
#pass
fi
}
# Function to transfer files via SCP
scp_file_transfer() {
source config.cfg # Load configuration variables
echo "Select transfer mode:"
echo "1) Datacenter Node (Deep Copy - Download)"
echo "2) Datacenter Node (Shallow Copy - Download)"
echo "3) Datacenter Node (Shallow Copy - Upload)"
echo "4) Local Server"
read -p "Enter choice (1-4): " choice
if [ "$choice" -eq 1 ]; then
echo "Deep copy from Datacenter Node (3-hop)..."
read -p "Enter target file: " target_file
read -p "Enter destination filename: " dest_filename
echo " ++++++++++++++++++ SCP Deep Copy ++++++++++++++++++"
echo " - Target file: $target_file"
echo " - Destination file name: $dest_filename"
echo "----------------------------------------------------------"
ssh_cmd="sudo ssh ${MOTHERSHIP_USER}@${MOTHERSHIP_IP} \"ssh -tt -i ${DATACENTER_GW_SSH_KEY_PATH} ${DATACENTER_USER}@${DATACENTER_GW_IP} \\\"scp -r ${DATACENTER_USER}@${DUMMY_COMPUTE_NODE}:${target_file} ${DATACENTER_DESTINATION}${dest_filename}\\\"\""
echo $ssh_cmd
eval "$ssh_cmd"
echo "+++++++++++ File copied to the Gateway node +++++++++++++"
ssh_cmd="sudo ssh ${MOTHERSHIP_USER}@${MOTHERSHIP_IP} \"scp -r -i ${DATACENTER_GW_SSH_KEY_PATH} ${DATACENTER_USER}@${DATACENTER_GW_IP}:${DATACENTER_DESTINATION}${dest_filename} ${MOTHERSHIP_DESTINATION}${dest_filename}\""
echo $ssh_cmd
eval "$ssh_cmd"
echo "+++++++++++ File copied to the Mother ship +++++++++++++"
elif [ "$choice" -eq 2 ]; then
echo "Shallow copy from Datacenter Node (via ProxyJump)..."
read -p "Enter target file: " target_file
read -p "Enter destination filename: " dest_filename
echo " ++++++++++++++++++ SCP Shallow Download ++++++++++++++++++"
echo " - Source: ${DATACENTER_USER}@${DUMMY_COMPUTE_NODE}:${target_file}"
echo " - Destination: ${LOCAL_DESTINATION}${dest_filename}"
echo " - Jump host: ${DATACENTER_USER}@${DATACENTER_GW_IP}"
echo "----------------------------------------------------------"
scp_cmd="scp -r -p -J ${DATACENTER_USER}@${DATACENTER_GW_IP} -i ${DATACENTER_GW_SSH_KEY_PATH} -- ${DATACENTER_USER}@${DUMMY_COMPUTE_NODE}:${target_file} ${LOCAL_DESTINATION}${dest_filename}"
echo "$scp_cmd"
eval "$scp_cmd"
echo "+++++++++++ File copied to localhost +++++++++++++"
return
elif [ "$choice" -eq 3 ]; then
echo "Shallow upload to Datacenter Node (via ProxyJump)..."
read -p "Enter local file pattern (e.g., /path/to/*.zip or /path/to/file): " file_pattern
read -p "Delete local files after successful upload? (y/n) [n]: " delete_after
delete_after="${delete_after:-n}"
echo " ++++++++++++++++++ SCP Shallow Upload ++++++++++++++++++"
echo " - Source pattern: ${file_pattern}"
echo " - Destination: ${DATACENTER_USER}@${DUMMY_COMPUTE_NODE}:${DATACENTER_DESTINATION}"
echo " - Jump host: ${DATACENTER_USER}@${DATACENTER_GW_IP}"
echo " - Delete after upload: ${delete_after}"
echo "----------------------------------------------------------"
shopt -s nullglob
files=( ${file_pattern} )
shopt -u nullglob
if [ ${#files[@]} -eq 0 ]; then
echo "No files matched pattern: ${file_pattern}"
return 1
fi
for f in "${files[@]}"; do
echo "Uploading: $f"
if scp -p -J ${DATACENTER_USER}@${DATACENTER_GW_IP} -i ${DATACENTER_GW_SSH_KEY_PATH} \
-- "$f" ${DATACENTER_USER}@${DUMMY_COMPUTE_NODE}:${DATACENTER_DESTINATION}; then
echo "SUCCESS: $f"
if [ "$delete_after" = "y" ] || [ "$delete_after" = "Y" ]; then
rm -f -- "$f"
echo "DELETED: $f"
fi
else
echo "FAILED: $f" >&2
fi
done
echo "+++++++++++ Upload complete +++++++++++++"
return
elif [ "$choice" -eq 4 ]; then
echo "Transferring from Local Server..."
echo "Select server to transfer file from:"
for i in "${!SERVER_IPS[@]}"; do
echo "$((i+1))) ${SERVERS[i]} - IP: ${SERVER_IPS[i]}"
done
read -p "Enter choice (1 to ${#SERVERS[@]}): " server_choice
selected_server_ip=${SERVER_IPS[$((server_choice-1))]}
if [ "$selected_server_ip" == "Not Found" ]; then
echo "Error: Selected server IP not found. Exiting."
return 1
fi
# Extract the username from the selected server (SERVERS array format is "hostname:username@hostname")
selected_server_info=${SERVERS[$((server_choice-1))]}
selected_server_user=$(echo "$selected_server_info" | cut -d ':' -f2 | cut -d '@' -f1)
echo "Transferring from Server IP: $selected_server_ip with user: $selected_server_user"
read -p "Enter target file: " target_file
read -p "Enter destination filename: " dest_filename
echo " ++++++++++++++++++ SCP File Transfer ++++++++++++++++++"
echo " - Target file: $target_file"
echo " - Destination file name: $dest_filename"
echo "----------------------------------------------------------"
ssh_cmd="sudo scp -r ${selected_server_user}@${selected_server_ip}:${target_file} ${MOTHERSHIP_DESTINATION}${dest_filename}"
echo $ssh_cmd
eval "$ssh_cmd"
echo "+++++++++++ File copied to the Mother ship +++++++++++++"
else
echo "Invalid choice! Exiting."
return 1
fi
ssh_cmd="sudo scp -r ${MOTHERSHIP_USER}@${MOTHERSHIP_IP}:${MOTHERSHIP_DESTINATION}${dest_filename} ${LOCAL_DESTINATION}${dest_filename}"
echo $ssh_cmd
eval "$ssh_cmd"
echo "+++++++++++ File copied to the localhost +++++++++++++"
}
# VM Operations (Start, Stop, Status, IP, SSH)
vm_operations() {
local vm_name="$1"
while true; do
echo "++++++++++++++ VM OPERATIONS MENU ($vm_name) ++++++++++++++++"
echo "1 - Start $vm_name"
echo "2 - Stop $vm_name"
echo "3 - Check Status of $vm_name"
echo "4 - Get IP of $vm_name"
echo "5 - SSH into $vm_name"
echo "6 - Back to VM Management Menu"
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++"
read -p "Enter your choice: " choice
case $choice in
1) VBoxManage startvm "$vm_name" --type headless ;;
2) VBoxManage controlvm "$vm_name" poweroff --type headless ;;
3) VBoxManage showvminfo "$vm_name" | grep "State:" ;;
4)
vm_ip=$(get_vm_ip "$vm_name")
if [ -z "$vm_ip" ]; then
echo "Could not retrieve IP. Try using the hostname: $vm_name"
else
echo "IP Address of VM $vm_name: $vm_ip"
fi
;;
5)
vm_ip=$(get_vm_ip "$vm_name")
read -p "Enter SSH username: " ssh_user
read -p "Enter hostname: " ssh_hostname
echo "IP Address of VM $vm_name: $vm_ip"
if [ -n "$vm_ip" ] && [ "$vm_ip" != "No value set!" ]; then
echo "Attempting SSH using IP: $vm_ip"
ssh "$ssh_user@$vm_ip"
else
echo "IP not available, trying to SSH using hostname: $ssh_hostname"
ssh "$ssh_user@$ssh_hostname"
fi
;;
6) return ;;
*) echo "Invalid choice, try again." ;;
esac
done
}
# Main Menu
main_menu() {
while true; do
figlet VaderShell
echo "-------------------------------------"
fetch_vm_list # Refresh VM list
fetch_server_ips # Resolve IPs for servers
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "Available Virtual Machines:"
for index in "${!vm_list[@]}"; do
echo "$index. (VM) ${vm_list[$index]}"
done
# Servers start after the last VM index
server_start=$((total_vms + 1))
echo "-----------------------------------------------------------"
for ((i = 0; i < total_servers; i++)); do
server_index=$((server_start + i))
IFS=":" read -r name ssh_info <<< "${SERVERS[i]}"
echo "$server_index. Server: $name (IP: ${SERVER_IPS[$i]})"
done
gateway_index=$((server_start + total_servers))
c4_index=$((gateway_index + 1))
admin_index=$((c4_index + 1))
vm_manage_index=$((admin_index + 1))
scp_index=$((vm_manage_index + 1))
exit_index=$((scp_index + 1))
echo "$gateway_index. Datacenter Gateway Node"
echo "$c4_index. Datacenter Compute nodes"
echo "$admin_index. Mother Ship (Admin)"
echo "$vm_manage_index. VM Management"
echo "$scp_index. SCP File Transfer"
echo "$exit_index. Exit"
echo "Enter your menu choice [1-$exit_index]: "
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
read choice
if [[ $choice -ge 1 && $choice -le $total_vms ]]; then
echo "You have selected VM: ${vm_list[$choice]}"
sudo ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "${vm_list[$choice]}@10.218.XXX.XXX"
elif [[ $choice -ge $server_start && $choice -lt $gateway_index ]]; then
server_choice_index=$((choice - server_start))
selected_server="${SERVERS[$server_choice_index]}"
IFS=":" read -r name ssh_info <<< "$selected_server"
echo "You have selected the Server: $name (IP: ${SERVER_IPS[$server_choice_index]})"
sudo ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $ssh_info
elif [[ $choice -eq $gateway_index ]]; then
echo "You have selected the Gateway Node"
sudo ssh -tt -i $DATACENTER_GW_SSH_KEY_PATH krit@$DATACENTER_GW_IP
elif [[ $choice -eq $c4_index ]]; then
echo "You are connecting to Datacenter node"
c4_node
elif [[ $choice -eq $admin_index ]]; then
IFS=":" read -r name ssh_info <<< "$ADMIN_SERVER"
echo "You have selected the $name (Mother Ship)"
sudo ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $ssh_info
elif [[ $choice -eq $vm_manage_index ]]; then
vm_management
elif [[ $choice -eq $scp_index ]]; then
scp_file_transfer
elif [[ $choice -eq $exit_index ]]; then
echo "Quitting ..."
exit
else
echo "Invalid option"
fi
echo "+++++ Thank you for using this service!! +++++"
done
}
main_menu