We help IT Professionals succeed at work.

Unix Korn / Bourne Shell script to prompt step by step /simplify scp copying

sunhux
sunhux asked
on

The computer operators 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
      Or to copy an entire directory,
  scp -P2222 -r source_dir_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 Korn/Bourne Shell script (as our servers
don't have bash), call it ncp to prompt operators step by step ie :


In most cases,
- the operators copy files (about 85-95% of the times) rather than entire directory
- the operators use putty (so they can easily select & paste filenames)

So ideally the requirements for the script :

Just type "ncp"  (I'll put this script in the path)

&  it will prompt them :

Question / prompt 1 : 1) Are you copying entire directory/folder? y/n/Y/N (default n or just ENTER
  as most of the times, it's files copying) :
If y, Question /prompt 2: 2) Enter source subdirectory name :
If n (or just <ENTER>), then prompt for following questions:

Question / prompt 3 : 3) Pls input source file1 (hit ENTER if no more source file) :
Question / prompt 4 : 4) Pls input next source file (hit <ENTER>) if there's no more) :
Question / prompt N: N)     ..... (to input the Nth file) .....
Last Question/prompt :   Pls input the destination drive & folder (eg: /c/temp format) :
Echo  "for text files, pls use WordPad or MSWord to open/read them on Windows"

lastly, if the script could input the PC's administrator's password (ie required by scp),
(let's call it  pcpassword), that would be ideal, if not, then let the operators enter the
password manually.


We don't want to use private+public key pairs due as we hv to go thru cumbersome
Change Requests
Comment
Watch Question

Author

Commented:

Ideally the script validates that the inputs for Question 1 for y/n/Y/N/<ENTER>  & if other values are entered,
it re-prompt the operator.

I'm not sure if scp < passwordfile  would work whereby the scp command reads the
passwordfile for the PC's administrator password.  If need to install expect tool, then
forgo the password portion as I'll get operators to enter password manually.  Just
that I want to simplify it to the extent the operators dont hv to enter password as this
PC's password will never be changed

Commented:
Before we move forward with this, are you sure you don't want to take a step back and see if there is an  even more efficient approach than the script?  What level of technical proficiency are the operators?

If not, then the first question is - are all the source files in the same location, or would they be in different paths?

Author

Commented:

Yes, I'm sure.

The operators usually would be asked by Developers to copy files say abt 7-15 times/day
& the path is given in email, so the operators just need to "cd path_given".

Don't ask me to winscp as the security team refused it

Commented:

I would never ask anyone to winscp - that would demand they have to use windows which would be nasty :)

So they would be more proficient at cutting and pasting filenames than they would using tab completion and wildcards.

The reason I asked was because if they say "file" and then enter 500 filenames, and then mistype the destination path, they have to start again cut-pasting 500 filenames.

Whereas if they used the command line:

   scp -p2222 file1 file2 file3 file4 ... file500 admin@dest:/path

It would just be a case of up-arrow and fix then "return" and they are done.

To simplify things, I think you can treat a file or directory as the same.  Because if you use the -r switch, but only specify files, nothing happens - it doesn't recurse "into" the files.  So consider this:

files=''

while true; do
    read -p "Enter file or directory name, or enter to continue? " filename
    if [ -n "$filename" ]; then
         files="$files $filename"
         echo "Filelist $files"
    else
          break
    fi
done
read -p "Destination? " $dest

scp -P2222 -r $files administrator@localhost:$dest









Author

Commented:

Oh, ok, I just learnt that
  scp -r source_file .....            would work just the same as
  scp source_file  .....

Ok, would you be able to do one enhancement: destination validation : so tt destination
entered must be in  /drive/directory   format, otherwise the script would prompt against
to reenter the destination.

drive must be a single alphabet of [a-z] or [A-Z] & preceded by /
while destination must be preceded by /


thanks
Commented:
Yeah - the way I have done it would allow multiple files *and* directories to be tranferred in one go.  The following tests the dest for the format

 /[single A-Z or a-z]/[any characters/



files=''

while true; do
   read -p "Enter file or directory name, or enter to continue? " filename
   if [ -n "$filename" ]; then
        files="$files $filename"
        echo "Filelist $files"
   else
         break
   fi
done

while true; do
      read -p "Destination? " dest;  
      if [[ "$dest" =~ \/[a-zA-Z]\/.*\/$ ]]
        then  
            break;
      fi
done

scp -P2222 -r $files administrator@localhost:$dest

Author

Commented:

The last script works well on Korn shell.

But in Bourne shell in HP-UX, the line below gave a problem :

      if [[ "$dest" =~ \/[a-zA-Z]\/.*\/$ ]]

Are you able to customize / fix this so that it works for both shells?

Author

Commented:

I have also tried inserting on the 1st line of the script

#!/usr/bin/ksh

but it still gave the same error

Author

Commented:

Weird, on my Linux boxes, it worked fine with Bash or Korn shell.

On my HP-UX Ver B11.11, whether I switch to Korn or Bourne shells, it would
give "invalid syntax error" with that line.  I switch by running "/usr/bin/ksh" before
executing the script

Commented:
Regex isn't supported in bash until version 3.0 - the most current is v4.1.7

I suspect you are running a fairly old version of bash on HP-UX?

  bash --version

Author

Commented:

I don't have bash shell at all on my HP-UX boxes.

Any way you can workaround the script codes?
ie avoid the Regex (regular expression)

i suspect even my Korn or Bourne Shell in HP-UX has issue

Commented:
Ohh bourne, not Bourne Again SHell.

Ok, do you have expr installed?

expr from the command line should tell you.

Commented:
I'll assume you do, here is the patch:

   if   [ `expr match "$dest" '/[a-zA-Z]/.*/$'` -ne 0 ]

Author

Commented:

Thanks, I'll only be able to test it tomorrow.

Just before I left office, I collected the following information about the HP-UX servers:

# echo $SHELL
/sbin/sh
# /sbin/sh --version
--version: A specified flag is not valid for this command.

root@nspppdb1:/ > /sbin/ksh -v
/sbin/ksh -v

# /usr/bin/ksh -version
Current option settings
allexport        off
bgnice           off
emacs            off
errexit          on
gmacs            off
ignoreeof        off
interactive      on
keyword          off
markdirs         off
monitor          off
noexec           off
noclobber        off
noglob           off
nolog            off
nounset          off
privileged       off
restricted       on
trackall         off
verbose          on
vi               on
viraw            off
xtrace           off

#
# uname -a
uname -a
HP-UX hostname B.11.11 U 9000/800 1236689390 unlimited-user license

Author

Commented:

expr is not installed in my HP-UX,

any other way?

Commented:
Well, that is strange...  expr is a standard tool on HP-UX 11

I guess you cannot just install it or bash.

Well have to try something more clumsy, hang on.
Commented:

Ok, please tell me you have grep!

  if [ `echo "$dest" | grep -e '/[a-zA-Z]/.*/$'`  ]

Author

Commented:

I got the same repeated (ie multiple lines of same message) error :
 "No query process pipe"

Traced that the problem lies with the line :
    read -p "Destination? " dest;  


It's also confirmed that I don't have expr in my HP-UX : very weird hardened boxes that I have

Commented:
Is it the semi-colon that is sending it off I wonder?

That line is exactly the same as the one further up.  Or perhaps the trace method you are using is indicating the following line?

Could you try removing the semi-colon, and if that doesn't make a difference, change the if line to

  if true

This will always be true and so always execute the code, but it will show us whether the if construct is what is breaking things.

I tested this in a bourne shell without problems, but the cut and pasting may have introduced something.

Author

Commented:

Just realized the rather old version of our HP-UX B11.11 is the culprit :
I've tried removing the ;  but the HP-UX man page for "read" showed that
it only support the syntax   "read -r identifier"  so  "-p" is not valid.

So both the "read -p ....." lines in the last script will face same issue:
   read -p "Enter file or directory name, or enter to continue? " filename
   &
   read -p "Destination? " dest;  

Now, I can't place the prompt message & 'read' on the same line,
so I have to do :
echo "Enter file or directory name, or enter to continue?"
read -r filename

What a nuisance HP-UX  I've got

Commented:
Ok, but that should work right?  It isn't a showstopper?

Author

Commented:

Yes, just a little mis-cosmetics only, this case can be closed