Copy a file on remote servers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy a file on remote servers
# 1  
Old 11-27-2009
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

Code:
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

# 2  
Old 11-27-2009
Quote:
Originally Posted by sexyTrojan
(it doesn't work somehow). Thanks in
Do you get some errors ?

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

This executes cp for me in the remote server:
Code:
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.
# 3  
Old 11-28-2009
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
# 4  
Old 11-28-2009
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:
Code:
#!/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:
Code:
#!/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

# 5  
Old 11-28-2009
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
Code:
for index in $array ; do
ssh $index

so I tried
Code:
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.
# 6  
Old 11-28-2009
Yes, `pwd` will execute pwd on the local server.

Try:

Code:
ssh $index "echo \$(pwd)"

or just
Code:
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)
Code:
$ 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
Code:
array="....."


Last edited by Scott; 11-28-2009 at 11:22 AM..
# 7  
Old 11-28-2009
I'm using bourne/bash shell, yup it works for me though. I think you need to do this
Code:
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.

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

i'm running out of ideas
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Diff on remote servers file systems

Iam trying to compare two file systems on two hosts basically to check them to be in sync I dont have rsync so trying to use diff Let me know how to do it.... Thanks (3 Replies)
Discussion started by: baanprog
3 Replies

2. Shell Programming and Scripting

Script to overwrite & before that keep copy a file on many servers

I have ssh password less auth enable & script does the job well as well #/bin/bash for i in `cat ip` do scp /etc/resolv.conf root@$ip done But I need to take backup of the file i will overwrite .. is there any simple way ? Kindly respond (5 Replies)
Discussion started by: heman96
5 Replies

3. Shell Programming and Scripting

shell script to take input from a text file and perform check on each servers and copy files

HI all, I want to script where all the server names will be in a text file like server1 server2 server3 . and the script should take servernames from a text file and perform copy of files if the files are not present on those servers.after which it should take next servername till the end of... (0 Replies)
Discussion started by: joseph.dmello
0 Replies

4. Shell Programming and Scripting

Append Text To Remote Servers File Via SSH

Gentleman & Ladies, Please make me feel like and novice and explain why this is not working? I am attempting to ssh to a remote server via ssh and keys. I want to inject a file on the remote server with text. I am not achieving this. I would like to echo/inject the text on the remote... (1 Reply)
Discussion started by: abacus
1 Replies

5. Shell Programming and Scripting

update a common file in different/remote servers

Dear All, I need to update a common file (ie. same path and same filename) but located in different servers. (ie. remotely located ~ able to ssh from my server). Can someone help me how to go ahead with this? (Since I'm manually editing that file in 20 servers :() (1 Reply)
Discussion started by: techychap
1 Replies

6. Shell Programming and Scripting

unix shell script which inserts some records into a file located in remote servers...

All, I need to write an unix shell script which inserts some records into a file located in remote servers. * Get the input from the user and insert according the first row. It should be in ascending order. 123451,XA,ABA 123452,XB,ABB 123453,XC,ABC 123455,XE,ABE 123456,XF,ABF 123458,XG,ABG... (2 Replies)
Discussion started by: techychap
2 Replies

7. Shell Programming and Scripting

Copy a file on remote server

I have ssh keys setup and running properly between two servers. I have a Korn shell script that is logging into the remote server and needs to backup the authorized_keys and/or authorized_keys2 files. The following syntax works perfectly ------------------------------------- ssh... (1 Reply)
Discussion started by: sunsysadm2003
1 Replies

8. Shell Programming and Scripting

restore mysql dump file in many remote servers?

Hi all, I want to restore DB file in many mysql servers, i already using script for sending the dumpfile in all servers, but it's just annoying if i have to restore the dumpfile in all servers, i want just execute 1 script, and will restore in all remote mysql servers. I make script but not... (2 Replies)
Discussion started by: blesets
2 Replies

9. HP-UX

help me to copy remote file

I want to copy dump generated from oracle database to my local DAT drive. Currently I am copying remote file to local drive thru rcp command and later copy it to local DAT. Pls. help me in this. Thanks Man Mohan email address removed (8 Replies)
Discussion started by: manmohan73
8 Replies

10. UNIX for Dummies Questions & Answers

Remote file copy

I facing a problem with Unix command "rcp". I unable to perform a rcp between host machines. I have religiously followed the man pages, but still unable to solve the problem. Do i check for anything to perform this command? Pls help....thanks =) (3 Replies)
Discussion started by: lchunl
3 Replies
Login or Register to Ask a Question