Proxmox Helper Scripts you can use
Let’s take a look at Proxmox helper scripts that can save you as a Proxmox admin tons of time and effort in doing manual tasks through the GUI. Let’s see how they work, and some examples of Proxmox helper scripts you can use to automate tasks in your environment.
Table of contents
What are Scripts?
Scripts are tiny programs that are usually simple in what they contain but that can be extremely helpful such as providing automation for some type of mundane task.
helper scripts can be run by an admin manually or scheduled by using something like a CRON job to run these automatically at specific times. You can integrate these into other tools like monitoring software so they are triggered for instance if a condition or threshold is met.
How do Scripts work?
Proxmox scripts connect to an API session on your Proxmox node that you specify for the script to connect to. The API is a RESTful API and provides the automation endpoint. Helper scripts can use the API to perform tasks like mass creating or cloning virtual machines. You can also do things like modifying network configurations, and managing backups of VMs and their configuration.
Helper scripts can be run from the host or another machine on the network. They can prompt for user for input, such as the virtual machine’s name to be created or the backup file name. Once the necessary information is provided, the script interacts with the API to perform the rest of the task using automation.
Examples of Scripts
Let’s take a look at a few Proxmox helper scripts you can use in your environment for automating tasks.
Backup script
Backup scripts are very good to use in the environment as they can often help to make sure backups get created for virtual machines. There is nothing worse than thinking you have a VM backup of a crashed VM only to find that one was never created!
The below scripts are just examples and may need modification to work in your specific environment without error when loading. It is important to always test scripts in a non-production environment before running them in a production environment to fully understand that your logic is going to do what you think it will do.
Backup Script:
#!/bin/bash
read -p "Enter the name of the virtual machine to be backed up: " vm_name
read -p "Enter the backup file name: " backup_file
# Backup the specified virtual machine to the specified backup file
qm backup $vm_name $backup_file
You can then restore from the backup file.
Migration Script
If you want to migrate virtual machines between Proxmox hosts, this is another task that can benefit from the automation possibilities of helper scripts and interacting with the API via a automated session.
The admin can get prompted for the name of the virtual machine to be migrated and the name of the target host. Once the necessary information is provided, the script uses the API to migrate the specified virtual machine to the specified target host.
Migration Script:
#!/bin/bash
read -p "Enter the name of the virtual machine to be migrated: " vm_name
read -p "Enter the name of the target host: " target_host
# Migrate the specified virtual machine to the specified target host
qm migrate $vm_name $target_host
It will also be interesting to see what new helper scripts will be used with the new Proxmox VMware migration functionality contained in the new versions of Proxmox. Undoubtedly there will be room to automate the migration of VMware virtual machines to Proxmox using the API for the functionality.
Firewall Script
Proxmox Firewall Script is a script that automates the configuration of the firewall rules by prompting the admin for the IP address and port number to be blocked or allowed. Then the script implements this using the API.
Firewall Script:
#!/bin/bash
read -p "Enter the IP address to be blocked/allowed: " ip_address
read -p "Enter the port number to be blocked/allowed: " port_number
read -p "Enter 'block' to block the IP address/port combination or 'allow' to allow it: " action
if [ $action == "block" ]; then
# Block the specified IP address and port number
pvesh set /cluster/firewall/iptables -ipfilter "in,${ip_address},tcp,dport=${port_number},j=DROP"
echo "IP address ${ip_address} blocked on port ${port_number}"
elif [ $action == "allow" ]; then
# Allow the specified IP address and port number
pvesh set /cluster/firewall/iptables -ipfilter "in,${ip_address},tcp,dport=${port_number},j=ACCEPT"
echo "IP address ${ip_address} allowed on port ${port_number}"
else
echo "Invalid action specified. Please enter 'block' or 'allow'."
fi
Benefits of Scripts
Admins can save a ton of time and streamline their processes by using helper scripts. You can create custom scripts that meet your specific needs with some programming knowledge. Now with resources like Google Gemini and ChatGPT, writing scripts is much easier. If you get stumped you can have AI help you with these tasks.
Also, many online resources provide pre-built scripts that you can use to automate tasks. Proxmox provides a GitHub repository containing a collection of useful helper scripts you can use as a starting point for your own scripts. Additionally, there are many online communities, such as the forum, where users share their scripts and offer support to others.
Scripts FAQs
Why are helper scripts important? Scripts are a great way to introduce automation into your environment. Using scripting and automated tasks helps to make operations much more streamlined, effective, and repeatable.
What technologies can you use for Scripts? You can use built-in Bash scripting for automation, Ansible configuration management, or even PowerShell works well for automated environments.
Why use Proxmox in your environment? It is a great hypervisor with many features and capabilities for running home labs or production workloads.
Wrapping up
Keep in mind these Proxmox helper scripts are only a few of the possibilities when working with Proxmox. Admins can greatly benefit in efficiency and time involved with day-to-day operations by using these types of helper scripts working with the API interface. What scripts are you using in your home lab or production environments?