How to make access to test machines easier using shell functions

On our daily work, we have to deal with a quite large number of test systems (usually spread across the world), and perform some operations on these test systems. Even though we automate the testcase execution, it’s useful to have ssh/console access to a machine when you need it nevertheless [1].

After some time having to look on wiki pages the access data for my systems (hostname, passwords, users), I figured I could speed up my work by defining shell aliases to the machines I commonly access at work. Reading a bit more about the subject, it became clear to me that the use of shell functions would be a better solution, since you can do way more stuff with them.

The approach I chose was to write a shell script kept under my source code folder, and then source it on your ~/.bashrc file as follows:

source /path/to/code/test-machines.sh

So that when you start a new shell instance, the functions defined on this script get loaded.

Another aspect of software testing is that our test systems need to be constantly re-installed over and over, therefore generating a new ssh fingerprint every time you do the re-install (and making your ssh to complain loudly when you try to access those machines). In order to work around this, you can call ssh with appropriate options so that it doesn’t cache the fingerprints.

SSH_TEST='/usr/bin/ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'

StrictHostKeyChecking=no means that ssh won’t ask you if you want to accept the new fingerprint, it will just accept it. Also, it will sent the fingerprint it just got from the machine re-installed and send it to the vast fields of /dev/null land ๐Ÿ™‚ so you won’t get complaints from ssh every time your test system gets re-installed [2].

This way, we can create a base test_system() function that will be used by the actual machine access functions, like:

test_system() {
local host=$1
local profile=$2
local user='user'
local pass='password'
echo "Client partition information"
echo "HMC LPAR profile: $profile"
echo "Hostname: $host"
echo "User: $user"
echo "Password: .pasroot"
echo "ssh $user@$host"
$SSH_TEST $user@$host

Notice here that since I do test on power platforms, I put information about lpar profiles. You can customize this base class to better suit your needs, obviously. The idea is to show information about your test systems in a handy way (ie, you can cut and paste info directly from your terminal emulator, not having to look somewhere else), as well as executing the ssh access function at the same time. With a base class like that, you can choose a short name for your system, define some parameters and then just pass them to the base function:

foo() {
local host='dev4e-foobar.domain.com'
local profile='foo'
test_system $host $profile

So when you type foo at a shell prompt, you’ll start an ssh connection to the test machine, as well as print handy information like:

lucas@freedom:~$ foo

Client partition information
HMC LPAR profile: foo
Hostname: dev4e-foobar.domain.com
User: user
Password: password
ssh user@dev4e-foobar.domain.com

This is good for bug reports where somebody needs access to your test systems, you can just copy and paste that info right away on the bug report. To summarize, you can assemble your list of test systems, and access those systems (with all the bells and whistles of shell completion) in a faster and more convenient way.

[1] I agree that there might be better ways to do test machine management, but this was a simple and surprisingly effective solution for my personal needs.

[2] That should go without saying, but keep in mind that this is approach is useful for test machines you have in your lab, not production systems. If the fingerprint of your server changed and you didn’t know about it, you definitely have a problem ๐Ÿ™‚


2 thoughts on “How to make access to test machines easier using shell functions

  1. Hey, nice post! I always wanted to know how to make ssh stop telling me the firgerprint’s changed after I reinstall a bunch of test machines.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s