Copy a file on remote servers

Hey Unix Gurus,

I'm having trouble in copying a file on 5 different servers, first how can you do it locally (i.e without the need to ssh to the server you want to copy the file) and if you need to ssh how do u run a command within that server. Please see my code below(it doesn't work somehow). Thanks in advance


array=( serverA serverB serverC serverD serverE)
FILENAME=`ls $dir | sort`
for index in $array ; do
for file in $FILENAME ; do
  ssh ${array[index]} "cd myDoc/; cp $file $file%.bak   2>/dev/null"
  done
done

exit 0



Do you get some errors ?

Or no errors, and no expected behavior ( not copied also ? ).

This executes cp for me in the remote server:

ssh root@192.168.1.33 "cp test-script /tmp/t.tttt"

the test.script is available in the home directory of the root. Is that myDoc is also available over there ?

If yes, try checking with verbose option of cp, and look at it, and try executing pwd, and trouble shoot further.

Thanks for your reply, no errors is generated because the error is thrown into dev/null and when I tried inserting pwd within the loop, it's saying that i'm still in my local machine and Yes myDoc is available in those servers

Hi.

If it's not working you should remove the /dev/null redirection to see what the problem might be.

You want to copy the files from the local server to the remote server, or you just want to back up the files on the remote servers?

i.e. for the former:

#!/bin/bash
array="serverA serverB serverC serverD serverE"
for index in $array ; do
for file in *; do
  # backup files on remote server which happen to have the same names
  # as files on the local server...
  ssh $index "cd myDoc; cp $file $file%.bak   2>/dev/null"

  # use scp to copy local file to remote server...
  scp $file $index:myDoc/$file
  done
done

for the latter, to rename the remote files:

#!/bin/bash
array="serverA serverB serverC serverD serverE"
for index in $array ; do
  ssh $index 'cd myDoc; for file in *; do cp $file $file%.bak 2>/dev/null; done'
done

HI thanks for the help, yes I want to back up the files on 5 different remote servers according to the result of "ls /myDoc/" in local machine, the 5 servers which mirrors to each other and copy the files over to remote servers . But after i debugged it, i found out the root of the problem was

for index in $array ; do
ssh $index 

so I tried

array=( serverA serverB serverC serverD serverE)
for index in $array ; do
ssh $index "echo `pwd`"

and the result was I'm still in my local machine and it was printed 5 times (due to loop). The ssh fails to connect to remote servers, but no errors being generated. i can ssh no problem in a command line (without the need to supply any login or password). But somehow in the script it doesn't work. I don't know why this is occuring.

Yes, `pwd` will execute pwd on the local server.

Try:

ssh $index "echo \$(pwd)"

or just

ssh $index "echo \$PWD"

Or use single quotes, and remove the backslash.

Which shell are you using? This doesn't work for me (bash on osx)

$ cat Test
array=(1 2 3 4 5)

for I in $array; do
  echo $I
done

$ ./Test
1

That's why I changed it to

array="....."

I'm using bourne/bash shell, yup it works for me though. I think you need to do this


array=(1 2 3 4 5)

for I in ${array[@]}; do
  echo $I
done

Btw i found out that if i use -1 (which indicates type 1 protocol) parameter in the ssh, it prompts me for server's password. I have generate public and private keys and copy pub key to remote servers yet i'm still prompted for password.

NOTE: Pub/private keys are already existed but it requires passphrase and i don't know this passpharse so I generate another pub/private key & appended my pub key on authorized_keys on the remote servers.


array=( serverA serverB serverC serverD serverE )
for index in ${array[@]} ; do
ssh -1 $index "echo `pwd`"

i'm running out of ideas

Or you can just use a list:

for i in serverA serverB serverC serverD serverE; do
  ssh -1 $i "echo `pwd`"
done

I was going to suggest what Scrutinizer suggested, but he beat me too it. :frowning:

Are you using keychain? It's available in the repositories of most Linux distros.

With it, I am prompted for my passphrase the first time I login after a reboot of my local machine. It keeps a process running, even if I logout, that maintains the passphrase in memory. Then when I use ssh or scp (even via cron when logged out) the keychain process supplies the passphrase. It's as safe as your local login.

Either loops work, the problem is ssh-ing to the servers itself. I can ssh serverB on the command line without being prompted for a password. However when i use the script to do it, i'm being prompted for a password and i don't know the password to access these servers. Do you think i should add the known host on remote server ? I'm not using keychain -- I don't even know the passpharase of the existing keys. What should i do?

If you don't know either the password or the SSH passphrase for any username on the servers, one wonders if you should be attempting this. If you are doing something that you are supposed to be doing, then you should reasonably demand the access you need to do it.

The way to get SSH to work without a password is to append your public key from the local machine to the ~/.ssh/authorized_keys file on the remote machine. When the known_hosts file give me trouble, I delete it and it starts over.