Question : Unix Shell script to simplify scp via reverse ssh tunnel


I need to do scp from several Unix (HP-UX & Linux) servers to a dedicated
PC on a frequent basis via Tcp2222.

So on those one dozen over Unix servers, I would issue :
  scp -P2222 source_file_on_Unix administrator@localhost:/g/temp

where -P2222 is to make scp to copy via Tcp2222 while /g/temp
is the drive & directory on the PC.  I'll need a Shell script, call it ncp
to simplify/shorten the command (as I frequently need to type this
scp command) such that, I just need to type:

  ncp file1 file2 fileN pc:/x/d

whereby file1, file2, ..., fileN can be up to 12 source files (ie 0<N<=12) on the
Unix server (scp allows multiple source files & when the script detected
pc:  it will substitute "pc:" with "administrator@localhost:" while x is the
drive which I may specify (can be C drive, F drive ie thumbdrive or a
network mapped drive on the PC) while  d  is the target directory name
on the PC.  The target filename(s) will assume the source filename(s)

Answer : Unix Shell script to simplify scp via reverse ssh tunnel

Version will full data checking:

#!/bin/bash
function readVal(){
       while true; do
               echo -n "$1"
               read val
               [ -n "$2" ] && [ -z "$val" ] && val="$2" && break;
               if eval "$3"; then
                       break;
               fi
               eval echo "$4"
       done
}

echo "ncp v1.0 by rumi"
readVal "Are you copying entire directory/folder? (y/N) " "n" '[[ "$val" =~ y|n|Y|N ]]' 'Put y or n.'
case "$val" in
       Y|y)
               readVal "Enter source directory : " "" '[ -d "$val" ]' 'Directory \"$val\" does not exist. Try again.'
               params="-r $val"
               ;;
       N|n)
               i=1
               while true; do
                       readVal "Enter source file$i : " "_" '[ -f "$val" ]' 'File \"$val\" does not exist. Try again.'
                       case "$val" in
                               "_")
                                       break;
                                       ;;
                               *)
                                       params="$params \"$val\""
                                       i=$(($i+1))
                                       ;;
                       esac
               done
               ;;
esac

readVal "Enter destination drive & folder (eg: /c/temp format) : " "" '[[ "$val" =~ /./.* ]]' 'Format should be \"/c/temp\". Try again.'

echo "performing scp -P2222 $params administrator@localhost:$val..."
scp -P2222 $params administrator@localhost:$val



rgds
rumi
Random Solutions  
 
programming4us programming4us