SSHing to other hosts in ‘while read’ loop

The while read construct relies on receiving a stream of data through stdin to work. Under normal circumstances ssh also requires access to stdin. This means that without special care a while read loop that contains a call to ssh will only execute once because on the first loop ssh will attach to and drain stdin. This can be demonstrated by:

# cat host_list.txt
server1
server2
server3

# while read RMTHOST; do ssh ${RMTHOST} 'hostname'; done < host_list.txt
server1

#

According to man 1 ssh, the -n flag will cause ssh to not try to read from stdin. This fixes the problem and allows ssh to be used within a while read loop.

# cat host_list.txt
server1
server2
server3

# while read RMTHOST; do ssh -n ${RMTHOST} 'hostname'; done < host_list.txt
server1
server2
server3

#

vi Quick Reference

vi Quick Reference

UNIX tips: Learn 10 good UNIX usage habits

When you use a system often, you tend to fall into set usage patterns. Sometimes, you do not start the habit of doing things in the best possible way. Sometimes, you even pick up bad practices that lead to clutter and clumsiness. One of the best ways to correct such inadequacies is to conscientiously pick up good habits that counteract them. This article suggests 10 UNIX command-line habits worth picking up — good habits that help you break many common usage foibles and make you more productive at the command line in the process. Each habit is described in more detail following the list of good habits.

http://www.ibm.com/developerworks/aix/library/au-badunixhabits.html

Piping from cat was a habit I had for a long time. I’m mostly over it, but it still rears its head from time to time.

The Definitive Guide to Bash Command Line History

Let me teach you how to work efficiently with command line history in bash.

This tutorial comes with a downloadable cheat sheet that summarizes (and expands on) topics covered here…

http://www.catonmat.net/blog/the-definitive-guide-to-bash-command-line-history/

Forward X over SSH through an intermediary host

One of the many useful features of SSH is X forwarding, which allows you to run a program on a remote host and have the GUI for that application display on your workstation as if it were running locally.  But what if you need to run a program on host2, which is only accessible through an intermediary host, host1?

SSH’s ability to forward arbitrary ports to the rescue!

First, log into host1 with X forwarding enabled:

$ ssh -X user@host1

Next, from host1, log into host2 as such:

$ ssh -R 9000:localhost:9000 user@host2

A guide to GNU Screen

http://magazine.redhat.com/2007/09/27/a-guide-to-gnu-screen/