-
-
Save tomassedovic/3728817 to your computer and use it in GitHub Desktop.
Simple script for detecting IP address of running virtual machine
This file contains hidden or 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 | |
if [[ $# == 0 ]]; then | |
echo Usage: | |
echo "virt <command> <vm> args" | |
echo Command is: ip, ssh, scp, sshfs | |
exit 1 | |
fi | |
cmd="$1"; shift | |
destination="$1" | |
user=$(echo $destination | awk '{split($0,array,"@")} END {print array[1]}') | |
domain=$(echo $destination | awk '{split($0,array,"@")} END {print array[2]}') | |
if [ -z "$domain" ]; then | |
domain=$user | |
user=`whoami` | |
fi | |
ip=$(virt-ip $domain) || exit 1 | |
shift | |
host="$user@$ip" | |
case "$cmd" in | |
ip) | |
echo $ip | |
;; | |
ssh) | |
virt-ssh "$host" $@ | |
;; | |
scp) | |
virt-scp "$host" $@ | |
;; | |
sshfs) | |
virt-sshfs "$host" $@ | |
;; | |
esac |
This file contains hidden or 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 | |
# Inspired by Lars Kellogg-Stedman's script: | |
# http://blog.oddbit.com/post/getting-the-ip-address-of-a-libvirt-domain | |
function virsh_cmd { | |
virsh -c qemu:///system $@ | |
} | |
which xmllint &>/dev/null || (echo "xmllint not found, install libxml2 to get it." 1>&2 && exit 1) | |
if [[ $# == 0 ]]; then | |
echo Enter a virtual machine name: 1>&2 | |
echo 1>&2 | |
virsh_cmd list 1>&2 | |
exit 1 | |
fi | |
# Quit if we got an unknown domain name. `virsh` will print the error messages. | |
domain_xml=$(virsh_cmd dumpxml $1) || exit 1 | |
# Get the MAC address of the first interface. | |
mac=$(echo "$domain_xml" | | |
xmllint --xpath //interface'[1]/mac/@address' - | | |
sed 's/.*="\([^"]*\)"/\1/' | |
) | |
# Get the ip address assigned to this MAC from dnsmasq | |
ip=$(awk -vmac=$mac '$2 == mac {print $3}' /var/lib/libvirt/dnsmasq/default.leases) | |
echo $ip |
This file contains hidden or 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 | |
scp -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" $@ |
This file contains hidden or 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 | |
ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" $@ |
This file contains hidden or 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 | |
sshfs -o "StrictHostKeyChecking=no" -o "UserKnownHostsFile=/dev/null" $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to use
/var/lib/libvirt/dnsmasq/default.leases
where libvirt maintains the leased ip addresses. More robust than parsing the logs and whatnot.