Tags

, , , ,

We have several Red hat servers node001 through node065 which are only accessible from outside via a central server, central.com. I can think of three different solutions to connect to a node, say node010, via ssh:
1. First SSH to central.com then to node010
2. Forward port 22 of node010 to local machine’s port 2200 via central.com
$ ssh kousik@central.com -L 2200:node010:22
Then ssh to localhost’s port 2200
$ ssh localhost -p 2200
3. $ ssh -YC -t kousik@central.com ssh -YC kousik@node$num

It’s the last one that caught my attention recently — so much so that I put a whole function in the ~/.bash_aliases on my local machine:

#
# A function to ssh to a node: put this in the ~/.bash_aliases file
# The input is the node number
# Example: to ssh to node010 type go2 followed by either 10, 0010 or 0000010 .....
# ..... which is `arithmetically' equal to 10.
#
go2() { num="$*";
  echo "you entered '"$num"' ";
  num="${num#[Nn]ode}"; # Ignore "node", if entered, and consider the rest 
  if [ "$num" -ge 1 2>& /dev/null ] && [ "$num" -le 65 2>&/dev/null ]; then
     num=`echo $num|bc` # for correct arithmetic conversion to a decimal integer
     if [ $num -lt 10 ]; then
        num="00$num";
     else
        num="0$num";
     fi;
     echo "Logging on to  node$num in 1s";
     sleep 1;
     ssh -YC -t kousik@central.com ssh -YC kousik@node$num;
  else
     echo "Please enter a valid integer (1--65)"
  fi;
}

The first conditional statement checks to see if the input is really an integer and is within the correct range: 0-65. The redirections of STDERR to /dev/null are there to suppress appearance of error messages due to non-integer inputs. The reason for using bc for correct arithmetic expansion may be clear from my comment here.

Advertisements