capsul.org webapp
at main 1.5 kB view raw
1#!/bin/sh -e 2# 3# ip-dhcp-host.sh - add or remove a mac address --> ipv4 mapping 4 5action="$1" 6network_name="$2" 7mac_address="$3" 8ipv4_address="$4" 9 10[ "$action" != 'add' ] && [ "$action" != 'delete' ] && printf 'you must set $action to either add or delete (1st arg)\n' && exit 1 11[ "$network_name" = '' ] && printf 'you must set $network_name (2nd arg)\n' && exit 1 12[ "$mac_address" = '' ] && printf 'you must set $mac_address (3rd arg)\n' && exit 1 13[ "$ipv4_address" = '' ] && printf 'you must set $ipv4_address (4th arg)\n' && exit 1 14 15# for some reason libvirt appears to remember every dhcp lease even after it has expired. 16# adding a new ip-dhcp-host will fail if there already is one with the same ip address. 17# so we will try to delete any that already exist with that IP address just in case! 18if [ "$action" = 'add' ]; then 19 printf "trying to delete any existing (probably expired) dhcp associations for $ipv4_address..." 20 virsh net-update "$network_name" delete ip-dhcp-host "<host ip='$ipv4_address' />" --live --config || true 21 22 printf "adding $mac_address --> $ipv4_address to $network_name" 23 virsh net-update "$network_name" "$action" ip-dhcp-host "<host mac='$mac_address' ip='$ipv4_address' />" --live --config 24else 25 printf "removing $mac_address and $ipv4_address from $network_name if they exist" 26 virsh net-update "$network_name" "$action" ip-dhcp-host "<host mac='$mac_address' />" --live --config || true 27 virsh net-update "$network_name" "$action" ip-dhcp-host "<host ip='$ipv4_address' />" --live --config || true 28fi 29 30 31