Using ssh in Script

I’ve been trying to write this script to roll over one of the log files on the lab computers. I also want it to copy the rolled file to our server. Now, I’ve already written two different scripts that accomplish this task. One resides on each computer and does the rolling task (I execute it on all the computers from ARD). The other is on the server and does the sftp bit.

Even though I’ve already got the working scripts, it’s of course not good enough. I’m accessing the remote computers in each script in one way or another. I’m also pretty much manipulating the same files. I should be able to combine the two scripts into one.

So, I wrote this script that combined both of the previous scripts. It has two basic parts. The first part uses ssh to log into the remote computer and does the following tasks:

Copy the log file and name the new one to include a date stamp gzip the rolled log file delete the old log file

The second part simply uses sftp to get the rolled log file.

Unfortunately, the ssh part of the log file just does not work. Clearly, the problem is that I just do not understand how to run ssh from a batch file (or at least not interactively). Here is the relevant part of the script:


echo "OK, starting ssh block"
for remotehost in $computer_list; do
 echo "OK, rolling log on ${remotehost}"
 ssh $remotehost <<EOF
  log_file=usertracking-${remotehost}.log
  new_log_file=usertracking-${date}-${computername}.log

  cd ${log_source:?"Directory does not exist."}
  cp ${log_file} $new_log_file
  gzip -fq $new_log_file
  new_log_file=${new_log_file}.gz
  rm $log_file
  echo "New log file name: ${new_log_file}"
  echo "Finished rolling log file"
EOF
done

This generates the following error after the ssh line:

Pseudo-terminal will not be allocated because stdin ↵
is not a terminal.

I pretty much understand what this error means, but have no idea how to fix it. After doing a lot of searching, I came up with this format for calling ssh:

ssh -v -q -o "BatchMode=yes" $remotehost

It doesn’t work for me though. At least, I don’t know how to use it. There is lots of information online about how to set up the authorization keys (which I’ve done), but not how to actually run it from a script. The stuff that I did find looked like it was written in latin.

So, after deciding that I was outmatched by the ssh batchmode, I decided I should try and write the whole thing just using sftp. Just one problem though, I can’t cp a file in sftp! There is no way for me to copy a remote file will using sftp. The only way I can think of to get around this is to GET the file, rename it on my local machine, and then PUT the renamed copy back on the remote computer. But, well, that’s just dumb. So, now I’m stuck. And grumpy.

You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Comments

1. NerdStarGamer » Blog Archive » Useful Shell Tricks

[…] of them have to do with clever uses of SSH. This really would have come in handy back when I was trying to write a script that processed user tracking logs on our lab […]

2. K. Arenn

Thanks for your help, this is really informative stuff! Can’t wait to read some more.



appointive
appointive
appointive
appointive