Quickly get IP addresses of new VMs

I spin up a lot of VMs using VMware Fusion. I generally keep “clean” generic copies of a few different distros and versions of Linux servers ready to go with my login, an sshd server, ssh keys, and basic settings that I use already set up. When I need to quickly test something manually — usually some new, multi-VM distributed container orchestration or database system — I just make as many copies of the server’s *.vmwarevm file as I need, fire up the VM copies on my laptop, test whatever I need to test, then shut them down. Eventually I delete the copies and recover the disk space.

Depending on where my laptop is running I’ll get a completely random IP address for the VM from the local DHCP server. I would log into the consoles, get the IPs, then log into the various VMs from a terminal. (Cut and paste just works a whole lot better on a terminal than on the VMware console.)

However, since the console screens are up, and I repeat this pattern several times a week, I figured why not save a step and make the ephemeral VMs just show me their IP address on their consoles without having to login, so I added an “on reboot” file called /etc/cron.d/welcome on the master image which updates the /etc/issue file.

/etc/cron.d/welcome looks like this:

@reboot root (/bin/hostname; /bin/uname -a; echo; if [ -x /sbin/ip ]; then /sbin/ip addr; else /sbin/ifconfig; fi) > /etc/issue

When a new VM boots, it writes the hostname, kernel info, and the ethernet config to the /etc/issue file. /etc/issue is displayed on the screen before the login prompt, so now I can just glance at the console, see the IP address, and ssh to the new VM.

Ephemeral VM

Although you’d never want to do this on a production system, it works great for ephemeral, throw-away test VMs.

Hope you find this useful.

Change your Skype availability status on Ubuntu via cron

I used Skype when it first came out and then stopped because there were only a few other people I knew who used it regularly. Since they were all in my local calling area, if I wanted to talk to any of them for free I could just use a phone.

I just started using it again — for work — to talk with customers and consultants who do not live in the Bay Area. However, I found that my Thursday telecommute days were causing problems: Skype would be running on my work computer and people trying to call me would get ring-no-answer because I was actually at home.

Turns out you can start and stop Skype via cron the same way you can start and stop Pidgin Instant Messenger via cron, you just need to set a few environment variables and it works great.

First create the script ~/bin/export_x_info which looks like this:

#!/bin/bash
# Export the dbus session address on startup so it can be used by cron
touch $HOME/.Xdbus
chmod 600 $HOME/.Xdbus
env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.Xdbus
echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.Xdbus
# Export XAUTHORITY value on startup so it can be used by cron
env | grep XAUTHORITY >> $HOME/.Xdbus
echo 'export XAUTHORITY' >> $HOME/.Xdbus

Create this script, type chmod 700 ~/bin/export_x_info so you can execute it, then execute it, then add it to System > Preferences > Sessions > Startup Programs so it will execute every time you start your computer and record the latest session address and XAUTHORITY value.

This script creates a 4-line file ~/.Xdbus with the current session address and XAUTHORITY value. By sourcing this file in the crontab file your scripts can now use dbus to send messages to X-Windows applications. To start and stop Skype at work I added these lines to my work computer’s crontab file:

# Skype on/off
00 09 * * Mon,Tue,Wed,Fri source ~/.Xdbus; /usr/bin/skype &
30 17 * * Mon,Tue,Wed,Fri killall skype

On my home computer I have:

# Skype on/off
00 09 * * Thu source ~/.Xdbus; /usr/bin/skype &
30 17 * * Thu killall skype

So on Thursdays when I telecommute Skype is running at home, the rest of the week it’s running at my office, and at night it’s turned off so customers aren’t calling me in the middle of the night.

When Skype isn’t running other users see your status as “Offline”. When cron restarts Skype it starts up with the same status it had when the process was killed. I just leave my status set to “Online,” so when cron starts Skype it shows everyone that I’m online. When cron kills the Skype process at the end of the day my status changes to “Offline” and people can’t call me.

Hope you find this useful.