Home › Forums › OS X Server and Client Discussion › Questions and Answers › scp weirdness
- This topic has 9 replies, 3 voices, and was last updated 19 years, 6 months ago by
gw1500se.
-
AuthorPosts
-
October 18, 2005 at 3:12 pm #363672
gw1500se
ParticipantI’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.
October 18, 2005 at 4:30 pm #363677andrina
ParticipantI 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?
October 18, 2005 at 4:42 pm #363678gw1500se
ParticipantThanks 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.
October 18, 2005 at 4:53 pm #363680gw1500se
ParticipantAlthough 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.
October 18, 2005 at 6:18 pm #363686andrina
ParticipantIs 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”
October 18, 2005 at 6:34 pm #363687gw1500se
ParticipantI’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.
October 19, 2005 at 11:41 am #363701gw1500se
ParticipantAs suspected, the quotes did not help. This is so strange since it works from the command line.
October 19, 2005 at 1:05 pm #363703andrina
ParticipantHave 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?
October 19, 2005 at 2:39 pm #363705gw1500se
ParticipantI found the problem. I am too embarassed to tell you what it was. Call it a senior moment. Sorry for the bother.
-
AuthorPosts
- You must be logged in to reply to this topic.
Comments are closed