44 lines
1.2 KiB
Fish

function setvmip --description "Set a VM's Cloud-Init IP"
if test (count $argv) -lt 2
echo "Usage: setvmip <vmid> <ip/cidr> [gateway] [ipconfig]"
return 1
end
set -l vmid $argv[1]
set -l new_ip $argv[2]
set -l new_gw $argv[3]
set -l config $argv[4]
test -n "$config"; or set config ipconfig0
set -l qmconfig (qm config "$vmid")
set -l name (string match -rg '^name: (.*)' $qmconfig)
set -l current (string match -r "^$config: .*" $qmconfig)
if test -z "$current"
echo "Error: $config not found on VM $vmid."
return 1
end
set -l old_ip (string match -rg 'ip=([^, ]+)' $current)
set -l old_gw (string match -rg 'gw=([^, ]+)' $current)
test -n "$new_gw"; or set new_gw "$old_gw"
echo
printf "VM: %s (%s)\n" "$vmid" "$name"
printf "Config: %s\n" "$config"
printf "IP: %s → %s\n" "$old_ip" "$new_ip"
printf "Gateway: %s → %s\n" "$old_gw" "$new_gw"
echo
read -P "Apply? [y/N]: " confirm
if not string match -iqr '^y(es)?$' "$confirm"
echo "Aborted."
return 0
end
qm set "$vmid" --$config "ip=$new_ip,gw=$new_gw"
end