#!/bin/sh -e # # ip-dhcp-host.sh - add or remove a mac address --> ipv4 mapping action="$1" network_name="$2" mac_address="$3" ipv4_address="$4" [ "$action" != 'add' ] && [ "$action" != 'delete' ] && printf 'you must set $action to either add or delete (1st arg)\n' && exit 1 [ "$network_name" = '' ] && printf 'you must set $network_name (2nd arg)\n' && exit 1 [ "$mac_address" = '' ] && printf 'you must set $mac_address (3rd arg)\n' && exit 1 [ "$ipv4_address" = '' ] && printf 'you must set $ipv4_address (4th arg)\n' && exit 1 # for some reason libvirt appears to remember every dhcp lease even after it has expired. # adding a new ip-dhcp-host will fail if there already is one with the same ip address. # so we will try to delete any that already exist with that IP address just in case! if [ "$action" = 'add' ]; then printf "trying to delete any existing (probably expired) dhcp associations for $ipv4_address..." virsh net-update "$network_name" delete ip-dhcp-host "" --live --config || true printf "adding $mac_address --> $ipv4_address to $network_name" virsh net-update "$network_name" "$action" ip-dhcp-host "" --live --config else printf "removing $mac_address and $ipv4_address from $network_name if they exist" virsh net-update "$network_name" "$action" ip-dhcp-host "" --live --config || true virsh net-update "$network_name" "$action" ip-dhcp-host "" --live --config || true fi