-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbashvm-remove-port-forwarding.sh
53 lines (42 loc) · 1.34 KB
/
bashvm-remove-port-forwarding.sh
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
#!/bin/bash
# Prompt user for VM details
read -ep "Enter the VM name: " vm_name
# If the variable is empty then don't continue
if [ -z "$vm_name" ]; then
echo "Invalid response. Please enter a VM name."
exit 1
fi
# Get the network name
net_name=$(virsh dumpxml "$vm_name" | grep "source network" | awk -F"'" '{print $2}')
if [[ -z "$net_name" ]]; then
net_name="default"
fi
# Get the bridge name
int_name=$(virsh net-dumpxml "$net_name" | grep "bridge name" | awk -F"'" '{print $2}')
if [[ -z "$int_name" ]]; then
int_name="virbr0"
fi
# Database file and table name
db_file="bashvm.db"
net_table=""$net_name"_table"
# Check if the database exists
if [ ! -f "$db_file" ]; then
echo "Database file '$db_file' does not exist. Nothing to delete."
exit 1
fi
# Confirm the record exists before deleting
record_count=$(sqlite3 $db_file "SELECT COUNT(*) FROM $net_table WHERE vm_name = '$vm_name';")
if [ -z "$record_count" ]; then
echo "No record found for VM name '$vm_name'."
exit 1
fi
# Delete the record
sqlite3 $db_file <<EOF
DELETE FROM $net_table WHERE vm_name = "$vm_name";
EOF
# Port forwarding removal
sed -i "/#$vm_name#/,/###$vm_name###/d" /etc/libvirt/hooks/qemu
echo ""
echo "Port forwarding rules for $vm_name has been deleted."
# Confirm deletion
echo "Record for VM name '$vm_name' has been deleted from the database."