Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #363672
    gw1500se
    Participant

    I’m having a problem using scp in a shell script. It works fine when I run it from the command line. However, when I use it in a script it prompts for a password. If I use ssh in a script it works as well. What is different about scp, why does it behave differently in a script then on the command line and how can I remote copy a file in a script without needing a password? TIA.

    #363677
    andrina
    Participant

    I normally try to avoid using ssh and scp in a shell script – try using something like expect instead – i.e. (the $argv 0 is going to take the password from execution of the file):

    #!/usr/bin/expect
    spawn scp /path/to/file user@machine:"/path/to/directory/"
    expect "ssword:"
    send [lindex $argv 0]\r
    expect eof                                                                                   
    
    

    Course – this is all assuming you don’t have ssh keys set up – assuming you have keys set up – what does you scp line look like?

    #363678
    gw1500se
    Participant

    Thanks for the reply. I do indeed have ssh keys set up . I can ssh and scp from the command line with no problem. Also I can use ssh in a script but not scp. The command is quite simple:

    scp -p /usr/local/bin/somefile [email protected]:/usr/local/bin
    

    Of course the script uses variables but the resulting command is the same.

    #363680
    gw1500se
    Participant

    Although incomplete at this point here is the script:

    #!/bin/bash -x
    #
    
    dirlist="/usr/local/bin /usr/local/sbin"
    
    for this_dir in $dirlist ; do
    	files_here=`ls $this_dir`
    	files_there=`ssh [email protected] "ls $this_dir"`
    	for this_file_here in $files_here ; do
    		scp -p $this_file_here [email protected]:$this_dir
    	done
    done
    
    

    The ssh works fine. The scp prompts for a password.

    #363686
    andrina
    Participant

    Is the script being run as the user admin? Try replacing your scp with the following:

    scp -p $this_file_here myremote.mydomain.com:”$this_dir”

    #363687
    gw1500se
    Participant

    I’ve run it as both root and admin. Both behave the same. I’ll give the quotes a try but I can’t see how that would cause scp to prompt for a password.

    #363701
    gw1500se
    Participant

    As suspected, the quotes did not help. This is so strange since it works from the command line.

    #363703
    andrina
    Participant

    Have you tried throwing in the expect script for a test? Also, what the default shell you’re using when testing interactively? Is it the same shell you script in? Anything significant/custom in your interactive environment that isn’t getting translated into the script? Also – in my last note – along with the quotes I’d removed the “user@” portion of the scp – did you try that?

    #363705
    gw1500se
    Participant

    I found the problem. I am too embarassed to tell you what it was. Call it a senior moment. Sorry for the bother. Oops!

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.

Comments are closed