Last active
February 23, 2016 14:07
-
-
Save koniu/837a574e7af377e37d67 to your computer and use it in GitHub Desktop.
get networking facts from bridged libvirt guests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# myvirt-facts - get networking facts from bridged libvirt guests | |
# https://gist.github.com/koniu/837a574e7af377e37d67 | |
# author <[email protected]> | |
# | |
# usage: | |
# myvirt-facts <domain> [ip|arp|hostname] | |
# | |
# for convenience symlink to: | |
# myvirt-ip | |
# myvirt-arp | |
# myvirt-hostname | |
# myvirt-ssh | |
domains() { | |
echo | |
echo Domains: | |
virsh -q list --all | |
echo | |
} | |
fail() { | |
domains | |
exit 1 | |
} | |
usage() { | |
echo "usage: $0 <domain>" | |
fail | |
} | |
get_host_ip() { | |
ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/' | |
} | |
fill_arp() { | |
for i in $(seq 1 254); do | |
ping -c 1 -n -q -r -t 1 -s 1 -W 1 \ | |
`get_host_ip | cut -d. -f1-3`.$i > /dev/null & | |
done | |
} | |
get_guest_state() { | |
virsh -q domstate $1 | |
} | |
get_guest_iface() { | |
virsh -q domiflist $1 || fail | |
} | |
get_guest_arp() { | |
get_guest_iface $1 | awk '{print $5}' | |
} | |
get_guest_ip() { | |
arp -en | grep `get_guest_arp $1` | head -n1 | awk '{print $1}' | |
} | |
get_guest_hostname() { | |
host `get_guest_ip $1` | cut -d\ -f5 | cut -d\. -f1 | |
} | |
# no domain name provided | |
[ -z $1 ] && usage | |
# refresh arp table | |
fill_arp | |
# output based on invocation | |
case `basename $0` in | |
*virt-facts) | |
state=`get_guest_state $1` | |
iface=`get_guest_iface $1` | |
arp=`get_guest_arp $1` | |
ip=`get_guest_ip $1` | |
hostname=`get_guest_hostname $1` | |
case $2 in | |
"") | |
echo "Domain: $1" | |
echo "State: $state" | |
echo | |
echo "Hostname: $hostname" | |
echo "IP: $ip" | |
echo "ARP: $arp" | |
;; | |
*) | |
get_guest_$2 $1 | |
esac | |
;; | |
*virt-ssh) | |
ip=`get_guest_ip $1` | |
cmd=$(echo $0 | cut -d\- -f2) | |
shift 1 | |
echo % $cmd -l $USER $ip $@ | |
$cmd -l $USER $ip | |
break | |
;; | |
*virt-*) get_guest_$(echo $0 | cut -d\- -f2) $1 ;; | |
*) echo wrong name; fail ;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment