Scripting with SSH

I have a script to write where I need to run some of the commands on another host.  The script users will be interacting with a menu and running some commands on the local host and some on a remote host.  SSH is a great way to run those commands on the remote host, but each time my script comes to one of the ssh commands, I must re-enter the password.  I could set up SSH keys so it works without a password, but I'm making the script available to several people.  Some use keys, and some do not.  I don't want set up of SSH keys to be a prerequisite of running the script, and I also don't want the users to have to type their password a hundred times. 

So I studied the situation and found the ControlMaster option of ssh.  It allows you to establish a session in SSH, then use the session to run multiple commands.  There are a lot of pages on the web talking about how to use this feature, but nothing really good describing how to script with it.  So this is what I came up with:


# This script demonstrates use of an ssh control master process.  The script
# connects via ssh to a remote host and authenticates once.  Then the session
# is re-used repeatedly for the remainder of the script without prompts for
# the ssh password appearing again.  Also works when using public key
# authentication.  The script runs much faster when using a control master
# process because each ssh does not need to be re-authenticated.  And public
# key authentication can be optional for the script users in a script that
# would otherwise have excessive repeated password prompts.


# start the control master process for this shell instance
ssh -o ControlPath=~/.ssh/master-$$ -o ControlMaster=auto -f -N $REMHOST 2>/dev/null 1>&2
if [ $? -gt 0 ]; then
  echo "ERROR: error authenticating to ssh server"
  exit 1

# example additional ssh processes that use the established session
ssh -o ControlPath=~/.ssh/master-$$ $REMHOST id
ssh -o ControlPath=~/.ssh/master-$$ $REMHOST hostname
ssh -o ControlPath=~/.ssh/master-$$ $REMHOST hostid
ssh -o ControlPath=~/.ssh/master-$$ $REMHOST who

# end the current control master process
PROC=`ps -ef |grep "ssh -o ControlPath=\~/.ssh/master-$$" | grep -v grep | awk '{print $2}'`
if [ -n "$PROC" ]; then
  kill $PROC

# housekeeping
# find socket files over 24 hours old and kill the associated processes
# for each socket file, do loop
find ~/.ssh -name "master-*" -type s -mtime +0 | while read FILEN; do
  FILEB=`basename $FILEN`
  # kill process associated with socket file, if process running
  PROC=`ps -ef |grep "ssh -o ControlPath=\~/.ssh/$FILEB" | grep -v grep | awk '{print $2}'`
  if [ -n "$PROC" ]; then
    kill $PROC
  sleep 1
  # if the socket file still exists, find out if that is because the process
  # did not die, or if it is a stale socket file that just needs to be deleted
  if [ -f $FILEB ]; then # if socket file still exists deal with it
    PROC=`ps -ef |grep "ssh -o ControlPath=\~/.ssh/$FILEB" | grep -v grep | awk '{print $2}'`
    if [ -z "$PROC" ]; then
      rm $FILEB # remove stale socket file
      echo ERROR: process $PROC associated with socket file $FILEB did not die

# end of script

I don't expect the housekeeping section to actually find anything to clean up very often, but it sure will be nice of the script to clean up after itself if it finds something left over from previous days.

If anybody knows a more graceful way to end the control master process, I would like to see it.  It would be great if processes started with ssh -f would die when the shell that invoked them reached the end, or if the process number for the backgrounded process was available in $! or some other variable.


Digital conversion of dual tuner standard def DVR setup

This computer is not that old, but it is old and slow enough to not do HD


My wife bought a Dell XPS-400 a couple of years ago.  It came with a dual TV tuner card and Microsoft XP Media Center Edition (XP MCE).  It worked its way from the desktop to basically take over our entertainment center.  Several years ago when VCR's became very inexpensive, I got a stack of 4 and kept them busy all the time recording this and that.  We stopped watching live TV and recorded everything we were interested in, and skipped commercials.  It was great.  Then the dual tuner computer moved in and the VCR's went away.  Now we could record and time shift to avoid commercials.  All of this was based on over-the-air. 


A few months before the digital switch, I bought a dual digital tuner.  I never could get it to work because the playback of shows and live TV stuttered and paused something terrible.  It was useless.  I tried all sorts of things to fix it, including souping up the computer, re-imaging it, updating it. The recordings were ridiculous in size, about 8 GB of data per hour of recorded programming.  Recording them in this HD format and later converting them with MCE Buddy and other programs like that didn't work worth a damn because the audio would often not match with the picture.  Basically the 2 year old computer just was not going to do HD TV.  I googled and hunted all over the Internet to find out why I couldn't pick up just standard def TV.  I couldn't find any standard def tuner cards.  Even the tech support for companies such as Hauppauge thought when I said standard def I wanted analog, no matter how I explained it.  I'm watching TV on a 30 inch tube set TV.  I certainly don't need high def.  Obviously digital to analog converter boxes pick up and show standard def just fine. 


I came to the conclusion there is not a sufficient market for supporting people like me who want an over the air dual DVR.  I like the 6 or 7 shows we watch, and I really don't want to watch commercials or deal with the networks who keep switching what night or time my favorite shows are on.  I could rant about those guys, but I won't.  Since there's no market for over the air dual DVR technology to feed a small screen TV, there's probably nobody reading this blog article either.  But yet I write.


I was bummed out thinking as recently as just a week ago that I would have to buy a new $800 to $1200 PC just to record over the air HD, only to down convert it to composite video to show on a 30 inch tube set TV.  Or go back to cable TV and rent a DVR from those guys.  I don't really have extra money just laying around to spend on TV programming.  When we had cable, all we did was watch the local broadcast stations anyway.  That's why we yanked the cable and went back to antenna.  And this computer is a 3 GHz Pentium D.  I didn't think it was a dinosaur until I started to try to record and watch HD on it. 


What I ended up doing


It is a miracle that I actually found the 2 IR blasters that came with this MCE computer.  I hooked up a GE digital to analog converter box, set up the IR blaster, trained MCE for the remote codes, and it went very well.  There are a couple of tricks you have to do to get this to work.  One is you have to tell MCE during your TV signal setup that you are using cable or satellite.  If you tell it antenna, it assumes you do not have a set top box to program.  This means for the program guide you need to pick a provider that has the channels you want, although they will be the wrong channel numbers.  Then you have to re-assign channel numbers to have an accurate guide. 


The other trick is a little more complicated, but I got through it. The GE converter box expects when you type a channel number on the remote you must press - before the last digit.  For example, if you want to watch channel 4, you must type 4-1.  In the cable and satellite world of TV there are no - characters, so there isn't any such character on the Microsoft remote either.  As it turns out, I wanted to pick up channels 4.1, 5.1, 8.1, 11.1, 13.1, 21.1, 27.1, and 58.1.  No where in all those numbers would I be required to type 9.  When I trained MCE on the remote codes for the GE box, I pressed the - key when it asked to learn 9.  So in order to switch to channel 58.1, I type 5891 on the Microsoft remote.  I could have used 0 for the - key as well, but I picked 9.


I figured out all of this in an evening, and it went fairly well.  So the next evening I decided to hook up another GE converter.  The first converter I hooked up to the analog TV in port, and there is only one of those on the back of the computer for both analog tuners.  But there is also an S-video input port.  So I hooked up the second converter to that input.  Then I had to go through the complete setup all over again.  This time when it asked me if I wanted to configure 1 tuner or 2, I picked 2. 


So I'm back in business again, watching free TV with a dual tuner DVR.