Network Automation Scripts

Overview

Network automation scripts for Proxmox VE. Automate network configuration, monitoring, and management.

Basic Network Scripts

Create Network Bridge

#!/bin/bash
# create-bridge.sh - Create network bridge
 
BRIDGE=$1
PHYSICAL=${2:-eth0}
 
# Check if bridge exists
ip link show $BRIDGE >/dev/null 2>&1 && { echo "Bridge exists"; exit 1; }
 
# Create bridge
ip link add $BRIDGE type bridge
ip link set $BRIDGE up
 
# Add physical interface
ip link set $PHYSICAL master $BRIDGE
 
echo "Bridge $BRIDGE created"

VLAN Bridge

#!/bin/bash
# create-vlan-bridge.sh - VLAN bridge
 
VLAN=$1
BRIDGE=$2
 
# Create VLAN interface
ip link add link vmbr0 name $BRIDGE.$VLAN type vlan id $VLAN
 
# Create bridge for VLAN
ip link add $BRIDGE type bridge
ip link set $BRIDGE.$VLAN master $BRIDGE
 
echo "VLAN bridge $BRIDGE created"

Bond Scripts

Create Network Bond

#!/bin/bash
# create-bond.sh - Create bonded network
 
BOND=$1
DEVICES="$2 $3"  # e.g., "eth0 eth1"
 
# Create bond
ip link add $BOND type bond mode 802.3ad
 
# Add devices
for dev in $DEVICES; do
    ip link set $dev master $BOND
done
 
echo "Bond $BOND created"

IP Management

Assign IP to VM

#!/bin/bash
# set-vm-ip.sh - Set VM IP
 
VMID=$1
IP=$2
GATEWAY=${3:-192.168.1.1}
 
# Get container
pct exec $VMID -- bash -c "ip addr add $IP/24 dev eth0"
pct exec $VMID -- bash -c "ip link set eth0 up"
pct exec $VMID -- bash -c "ip route add default via $GATEWAY"
 
echo "IP $IP assigned to VM $VMID"

Monitoring Scripts

Network Monitor

#!/bin/bash
# monitor-network.sh - Monitor network
 
for bridge in $(ip link show | grep ": br" | cut -d: -f2 | tr -d ' '); do
    RX=$(cat /sys/class/net/$bridge/statistics/rx_bytes)
    TX=$(cat /sys/class/net/$bridge/statistics/tx_bytes)
    echo "$bridge: RX $RX TX $TX"
done

Firewall Automation

Quick Firewall Setup

#!/bin/bash
# setup-firewall.sh - Basic firewall
 
# Disable routing
echo 0 > /proc/sys/net/ipv4/ip_forward
 
# Clear existing rules
iptables -F
iptables -t nat -F
 
# Basic rules
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -P INPUT DROP
 
echo "Basic firewall configured"

Troubleshooting Scripts

Network Diagnostics

#!/bin/bash
# network-diagnostics.sh - Network diagnostics
 
echo "=== Network Interfaces ==="
ip link show
 
echo -e "\n=== IP Addresses ==="
ip addr show
 
echo -e "\n=== Routing ==="
ip route show
 
echo -e "\n=== Bridge Status ==="
brctl show
 
echo -e "\n=== Network Stats ==="
ip -s link

Keywords

network automation bridge bonding firewall vlan


Back to Proxmox VE