0% found this document useful (0 votes)
31 views9 pages

finalSH - AlphaOmega

Uploaded by

usama.fayyaz5512
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views9 pages

finalSH - AlphaOmega

Uploaded by

usama.fayyaz5512
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

nano final.

sh (ctrl x)
----------------------
#!/bin/bash
choice=10
while [ "$choice" -ne 0 ];
do
clear
echo -e " The ALL in One by Areeb"
echo -e "------------------------------------"
echo -e "Press 1 for Making a Network\n"
echo -e "Press 2 for Importing Backup(currently in trial)\n"
echo -e "Press 3 for Making Clone(NEW VMS) :\n"
echo -e "Press 4 for Changing Mac Addresses :\n"
echo -e "Press 5 for Deleting a VMs :\n"
echo -e "Press 6 for renaming a VMs :\n"
echo -e "Press 7 for Getting Macs of VMs :\n"
echo -e "Press 8 for cloning & assigning macs :\n"
echo -e "Press 9 for Getting old VMs Macs :\n"
echo -e "Press 10 for Converting Templete into VM:\n"
echo -e "Press 0 for exiting :\n"
echo -e "----------------------------------------\n"

read choice
if [[ choice -eq 1 ]];
then
clear
#!/bin/bash
echo " MAKING A NETWORK :"
# Define the bridge name and network interface to bridge
echo -n "Please enter Linux Bridge Name"
read BRIDGE_NAME
echo -n "Please Enter Default link Network"
read net
BRIDGE_IP="10.10.10.1/24"

# Create the bridge


ip link add name $BRIDGE_NAME type bridge

# Assign an IP address to the bridge


ip addr add $BRIDGE_IP/$BRIDGE_NETMASK dev $BRIDGE_NAME

# Bring the bridge up


ip link set dev $BRIDGE_NAME up

# Configure the bridge in /etc/network/interfaces


echo "" >> /etc/network/interfaces
echo "# Bridge configuration" >> /etc/network/interfaces
echo "auto $BRIDGE_NAME" >> /etc/network/interfaces
echo "iface $BRIDGE_NAME inet static" >> /etc/network/interfaces
echo " address $BRIDGE_IP" >> /etc/network/interfaces

# Append the specified post-up and post-down rules at the end of


/etc/network/interfaces
cat <<EOL >> /etc/network/interfaces

# Additional Post-up and Post-down rules


post-up echo 1 > /proc/sys/net/ipv4/ip_forward
post-up iptables -t nat -A POSTROUTING -s '10.10.10.1/24' -o $net -j
MASQUERADE
post-down iptables -t nat -D POSTROUTING -s '10.10.10.1/24' -o $net -j
MASQUERADE
post-up iptables -t raw -I PREROUTING -i fwbr+ -j CT --zone 1
post-down iptables -t raw -D PREROUTING -i fwbr+ -j CT --zone 1

EOL

# Restart the networking service


systemctl restart networking

echo "Bridge $BRIDGE_NAME created and configured with IP


$BRIDGE_IP/$BRIDGE_NETMASK."

fi

if [[ choice -eq 2 ]];


then
sudo apt-get install sshpass

# Set your local and remote server details


local_server="/var/lib/vz/dump/"
remote_server="[email protected]"
remote_password="Integration2023!" # Replace with the actual password

# List of remote backup file paths


remote_backup_paths=(
"/var/lib/vz/dump/vzdump-qemu-276-2023_10_30-15_03_20.vma.zst"
"/var/lib/vz/dump/vzdump-qemu-9999-2023_10_30-15_02_18.vma.zst"
)

# Loop through the list of remote backup paths and transfer each file
for remote_backup_path in "${remote_backup_paths[@]}"; do
# Check if the backup file exists on the remote server
sshpass -p "$remote_password" ssh $remote_server "[ -f $remote_backup_path ]"
if [ $? -ne 0 ]; then
echo "Backup file $remote_backup_path does not exist on the remote server."
else
# Transfer the backup from the remote server to the local server using SCP
sshpass -p "$remote_password" scp $remote_server:$remote_backup_path
$local_server
# Check the SCP exit status to ensure the transfer was successful
if [ $? -eq 0 ]; then
echo "Backup file $remote_backup_path transferred successfully."
else
echo "Error transferring the backup file $remote_backup_path."
fi
fi
done
qmrestore "/var/lib/vz/dump/vzdump-qemu-9999-2023_10_30-15_02_18.vma.zst" 9999
qmrestore "/var/lib/vz/dump/vzdump-qemu-276-2023_10_30-15_03_20.vma.zst" 3000
echo "Press Enter to continue..."
read

fi

if [[ choice -eq 3 ]];

then
#!/bin/bash
clear
echo -e " MAKING CLONES"
echo -n "Enter starting id:"
read first_vm_id
echo -n "Enter ending id:"
read last_vm_id
echo -n "Enter which vm from you want to clone:"
read clone_id
echo -n "Enter your cloned vms names:"
read vm_name
echo -n "Enter Bot ID that you want to concatinate with vms names:"
read i
echo -e "Press "0" for full Cloning & "1" for link Cloning "
read g
if [[ g -eq 0 ]];
then
for ((st=first_vm_id; st<=last_vm_id; st++,i++)); do

qm clone $clone_id $st --full=True --name="$vm_name-$i" ;


done
fi
if [[ g -eq 1 ]];
then
for ((st=first_vm_id; st<=last_vm_id; st++,i++)); do

qm clone $clone_id $st --full=false --name="$vm_name-$i" ;


done
fi
echo "Please!!!!!! wait for few seconds for VMS to Start"
sleep 30

for ((st=first_vm_id; st<=last_vm_id; st++)); do

qm start $st

done
clear
echo"Yours New Mac Addresses are !!!!!!!"

# Check if the provided IDs are valid numbers


if ! [[ "$first_vm_id" =~ ^[0-9]+$ ]] || ! [[ "$last_vm_id" =~ ^[0-9]+$ ]]; then
echo "Invalid input. Please enter valid VM IDs (numeric values)."
exit 1
fi

# Loop through the range of VM IDs and get MAC addresses


for ((vm_id = first_vm_id; vm_id <= last_vm_id; vm_id++)); do
# Define the path to the VM configuration file
vm_config="/etc/pve/qemu-server/$vm_id.conf"

# Check if the VM configuration file exists


if [ -f "$vm_config" ]; then
# Use grep and awk to extract the MAC address
mac_address=$(grep -E "net0:.*" "$vm_config" | awk -F'=' '{print $2}' | cut
-d',' -f1)
echo "$vm_id $mac_address"
else
echo "VM configuration file for VM $vm_id does not exist."
fi
done
echo "Cloning has been done! Press Enter to continue..."
read
fi
if [[ choice -eq 4 ]];

then
#!/bin/bash
clear
echo -e " CHANGING MAC ADDRESSES"

# Define the common bridge name


common_bridge="vmbr99"

# Specify the range of VM IDs


echo -n "Start ID: "
read start_vm_id
echo -n "End ID: "
read end_vm_id

# Loop through the specified range of VM and container IDs


for ((vm_id=start_vm_id; vm_id<=end_vm_id; vm_id++)); do
echo "Resetting MAC addresses for VM/Container ID: $vm_id"

# Check if the common bridge is connected to the VM


if qm show $vm_id --machine-readable | grep -q "net0: $common_bridge"; then
# Delete the network device connected to the common bridge
qm set $vm_id --delete net0

# Start the VM or container


qm start $vm_id

echo "MAC address reset for VM/Container ID: $vm_id"


else
echo "VM/Container ID: $vm_id is not connected to $common_bridge.
Skipping..."
fi
done

echo "All MAC addresses have been reset for VMs connected to $common_bridge within
the specified range."

for ((st=start_vm_id; st<=end_vm_id; st++)); do

qm shutdown $st

done

for ((st=start_vm_id; st<=end_vm_id; st++)); do

qm start $st

done
echo "Press Enter when all vms are started to continue..."
read
fi

if [[ choice -eq 5 ]];


then
#!/bin/bash
clear
echo -e " DELETING THE VMS"
# Prompt for the range of VM IDs to delete
echo -n "Enter the starting VM ID: "
read start_id

echo -n "Enter the ending VM ID: "


read end_id

# Loop through the specified range and delete the VMs


for ((id=start_id; id<=end_id; id++)); do
qm destroy $id
echo "Deleted VM $id"
done
echo "Press Enter to continue..."
read
clear
fi

if [[ choice -eq 6 ]];


then
clear
echo -e " RENAMING THE VMS"
#!/bin/bash
echo -n "enter start id:"
read s_id
echo -n "enter ending id:"
read e_id
echo -n "enter new name:"
read N_display_Prefix
echo -n "enter bot id you want to concatinate:"
read i

for ((id=s_id; id<=e_id; id++,i++)); do

qm set "$id" --name "$N_display_Prefix-$i"

echo "$id rename done"

done
echo "Press Enter to continue..."
read
clear
fi
if [[ choice -eq 7 ]];
then
clear
#!/bin/bash
echo -e " GETTING MACS OF THE VMS"
# Ask for the first and last VM IDs
read -p "Enter the first VM ID: " first_vm_id
read -p "Enter the last VM ID: " last_vm_id

# Check if the provided IDs are valid numbers


if ! [[ "$first_vm_id" =~ ^[0-9]+$ ]] || ! [[ "$last_vm_id" =~ ^[0-9]+$ ]]; then
echo "Invalid input. Please enter valid VM IDs (numeric values)."
exit 1
fi

# Loop through the range of VM IDs and get MAC addresses


for ((vm_id = first_vm_id; vm_id <= last_vm_id; vm_id++)); do
# Define the path to the VM configuration file
vm_config="/etc/pve/qemu-server/$vm_id.conf"

# Check if the VM configuration file exists


if [ -f "$vm_config" ]; then
# Use grep and awk to extract the MAC address
mac_address=$(grep -E "net0:.*" "$vm_config" | awk -F'=' '{print $2}' | cut
-d',' -f1)
echo "$vm_id $mac_address"
else
echo "VM configuration file for VM $vm_id does not exist."
fi
done
echo "Press Enter to continue..."
read
clear
fi
if [[ choice -eq 420 ]];
then
rm final.sh
rm mac.txt
choice=0
clear
fi
if [[ choice -eq 8 ]];
then
clear
#!/bin/bash

# Input file containing VM IDs and their corresponding MAC addresses


input_file="mac.txt"

# Check if the input file exists and is readable


if [[ ! -f "$input_file" ]] || [[ ! -r "$input_file" ]]; then
echo "Error: Input file '$input_file' does not exist or is not readable."
exit 1
fi

# Function to assign MAC addresses to VMs


assign_mac_addresses() {
local network_bridge="$1" # Access the network bridge via the first argument

while IFS=' ' read -r vm_id mac_address; do


# Debugging output
echo "Read line: VM ID: $vm_id, MAC Address: $mac_address"

# Regular expressions for validating VM ID and MAC address


vm_id_regex='^[0-9]+$'
mac_address_regex='^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$'

# Check if the line contains a valid VM ID and MAC address


if [[ $vm_id =~ $vm_id_regex ]] && [[ $mac_address =~
$mac_address_regex ]]; then
# Construct the VM configuration file path
vm_config="/etc/pve/qemu-server/$vm_id.conf"
# Check if the VM configuration file exists and is writable
if [[ ! -f "$vm_config" ]] || [[ ! -w "$vm_config" ]]; then
echo "Error: Configuration file for VM ID $vm_id does not exist or
is not writable."
continue
fi

# Debugging output
echo "Assigning MAC address $mac_address to VM ID $vm_id"

# Remove the previous line starting with net0


sed -i '/^net0:/d' "$vm_config"

# Append the new line with the specified MAC address for net0
echo "net0: e1000=$mac_address,bridge=$network_bridge" >> "$vm_config"

echo "MAC address assigned successfully for VM ID $vm_id"


else
echo "Invalid input format: VM ID: $vm_id, MAC Address: $mac_address"
fi
done < "$input_file"
}

# Cloning VMs
clear
echo -e " MAKING CLONES"
echo -n "Enter starting id:"
read first_vm_id
echo -n "Enter ending id:"
read last_vm_id
echo -n "Enter which vm from you want to clone:"
read clone_id
echo -n "Enter your cloned vms names:"
read vm_name
echo -n "Enter Bot ID that you want to concatenate with vms names:"
read i
echo -n "please enter network bridge from network"
read n
echo -e "Press '0' for full Cloning & '1' for link Cloning "
read g
case $g in
0)
for ((st=first_vm_id; st<=last_vm_id; st++,i++)); do
qm clone $clone_id $st --full=True --name="$vm_name-$i"
# Call function to assign MAC addresses after each cloning
assign_mac_addresses "$n" # Pass the network bridge value to the
function
done
;;
1)
for ((st=first_vm_id; st<=last_vm_id; st++,i++)); do
qm clone $clone_id $st --full=false --name="$vm_name-$i"
# Call function to assign MAC addresses after each cloning
assign_mac_addresses "$n" # Pass the network bridge value to the
function
done
;;
*)
echo "Invalid option selected. Please select '0' or '1'."
;;
esac

echo "Cloning has been done! please restart the bots"


read

fi
if [[ choice -eq 9 ]];
then
clear
#!/bin/bash
echo -e " GETTING MACS OF THE VMS in Mac.txt"
rm mac.txt

# Ask for the first and last VM IDs


read -p "Enter the first VM ID: " first_vm_id
read -p "Enter the last VM ID: " last_vm_id

# Check if the provided IDs are valid numbers


if ! [[ "$first_vm_id" =~ ^[0-9]+$ ]] || ! [[ "$last_vm_id" =~ ^[0-9]+$ ]]; then
echo "Invalid input. Please enter valid VM IDs (numeric values)."
exit 1
fi

# Output file
OUTPUT_FILE="mac.txt"

# Loop through the range of VM IDs and get MAC addresses, and store in the output
file
for ((vm_id = first_vm_id; vm_id <= last_vm_id; vm_id++)); do
# Define the path to the VM configuration file
vm_config="/etc/pve/qemu-server/$vm_id.conf"

# Check if the VM configuration file exists and is readable


if [ -f "$vm_config" ] && [ -r "$vm_config" ]; then
# Use grep and awk to extract the MAC address
mac_address=$(grep -E "net0:.*" "$vm_config" | awk -F'=' '{print $2}' | cut
-d',' -f1)
echo "$vm_id $mac_address" >> "$OUTPUT_FILE"
else
echo "VM configuration file for VM $vm_id does not exist or is not
readable."
fi
done

echo "MAC addresses and VM IDs have been stored in $OUTPUT_FILE"


echo "Press Enter to continue..."
read
fi
if [[ choice -eq 10 ]];
then
#!/bin/bash

# Prompt the user for the VM ID


read -p "Enter the VM ID: " vm_id

# Check if the VM ID is provided


if [ -z "$vm_id" ]; then
echo "VM ID is required. Exiting."
exit 1
fi

# Define the path to the VM config file


vm_config="/etc/pve/qemu-server/${vm_id}.conf"

# Check if the config file exists


if [ ! -e "$vm_config" ]; then
echo "VM config file not found. Exiting."
exit 1
fi

# Open the VM config file and remove the "template: 1" line
sed -i '/^template: 1$/d' "$vm_config"

echo "Configuration for VM $vm_id updated successfully."


fi
done
------------------------------------------------------------------------------
chmod +x final.sh
./final.sh

You might also like