0
votes

I'm trying to make a bash expect script that takes in input like user, host, password, and file names and then copies the files from remote to local. From what I've read so far, scp'ing multiple files from remote to local works just fine when you're assuming the files are coming from ~/, i.e:

scp -r user@host:"file1 file2 file3" .

would copy file1, file2, and file3 from ~/ into the current working directory. But I need to be able to pass in another directory as an argument, so my bash script looks like this (but doesn't work, I'll explain how):

eval spawn scp -oStrictHostKeyChecking=no -oCheckHostIP=no -r $user@$host:$dir/"$file1 $file2 $file3" ~/Downloads

This doesn't work after the first file; the shell raises a "No such file or directory" error after the first file, which I would assume means that the script only works on $dir for the first file, then kicks back into ~/ and of course can't find the files there. I've looked everywhere for an answer on this but can't find it, and it would be super tedious to do this one file at a time.

2
What's with the spawn? Are you doing this in expect? If you are, and you're doing that to send the password, set up ssh keys instead.glenn jackman
I considered doing ssh keys. I suppose I haven't looked into it a whole lot but if I were to in theory put this program that set up ssh keys on someone's machine for them, then what if they don't want ssh keys set? Is there a way to set up ssh keys only for this program and not go through the ~/.ssh folder?bmw417
Not that I'm aware of. However, setting up ssh keys in more secure than storing a password in plain textglenn jackman
You're definitely not wrong there. It'll make it easier on me anyhow, thanks for the help!bmw417

2 Answers

3
votes

Assuming your remote login shell understands Brace Expansion, this should work

scp $user@$host:$dir/"{$file1,$file2,$file3}" ~/Downloads
1
votes

If you want to download multiple files with an specific pattern, you can do the following for example if you want all zip files:

scp -r user@host:/path/to/files/"*.zip" /your/local/path