Skip to content

Instantly share code, notes, and snippets.

@NeoCat
Last active November 23, 2016 19:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NeoCat/9903497 to your computer and use it in GitHub Desktop.
Save NeoCat/9903497 to your computer and use it in GitHub Desktop.
#!/bin/bash
type=ipv4
function usage() {
echo "usage: $(basename "$0") [OPTIONS] domain"
echo " -6 get ipv6 address (default: ipv4)"
echo " -h show this help"
echo " -n show interface name"
echo " -p show prefix"
echo " -i interface specify interface (default: all)"
exit
}
while getopts "6hi:np" flag; do
case $flag in
6) type=ipv6;;
i) filter='select(.name == "'"$OPTARG"'") |';;
n) name='.name + ":\t" + ';;
p) prefix='+ "/" + (.prefix | tostring)';;
h) usage;;
esac
done
shift $((OPTIND-1))
[ -z "$1" ] && usage
sudo virsh qemu-agent-command "$1" \
'{"execute":"guest-network-get-interfaces"}' | \
jq -r '
.return [] | '"$filter"'
select(."ip-addresses") |
{name, addr: (."ip-addresses"[] | select(."ip-address-type"=="'$type'") |
."ip-address" '"$prefix"')} |
'"$name"'.addr'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment