Connecting to multiple unix server from unix server using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Connecting to multiple unix server from unix server using shell script
# 1  
Old 11-25-2009
Connecting to multiple unix server from unix server using shell script

Hi Gurus,

I'm a unix newbie and I would like to connect to multiple unix servers from unix server using shell script i.e from server a to server b,c,d etc. I want to copy the files from unix server a to server b, c, d. I can access staright using ssh without the need to have password and user name once I have accessed to server A. However when I ran my script, I can only access to server B. The command below it, was not ran.

Thanks in Advance

Code:
#!/bin/sh
 
######## I'm in currently in server A########
varA=`ls /root/myDoc`
array=( serverB serverC serverD serverE serverF serverG )
for index in array
do
ssh ${array[index]}
echo "I am in $PWD"
for file in $varA ; do
cp $file /myDoc/${file%}.`date '+%Y%m%d'` 2>/dev/null
done
done
exit 0

# 2  
Old 11-25-2009
You need use scp directly. Such as:
Code:
scp YOUR_FILE USERNAME@SERVER:/PATH

# 3  
Old 11-25-2009
I modified the code a little. Please make required changes before you run on your server.
Code:
#!/bin/sh
 
######## I'm in currently in server A########
TODAY=$(date '+%Y%m%d')
for serverindex in serverB serverC serverD serverE serverF serverG ;  do
  scp -r /root/myDoc/* $serverindex :~/$file.$TODAY 2>/dev/null 
  # Better check $? exit code to verify if the file copy is successful ..
  echo "$file : is copied to server >> $serverindex << as filename: $file.$TODAY .. hopefully .."   
done

exit 0

man scp

-r Recursively copy entire directories.
# 4  
Old 11-25-2009
Thanks a lot guys for helping. But first, I want to check if the files in server A also exist in server B,C,D etc, I want to back up the files first in those servers before scp ing over. I can do this by using an ls /myDoc/ in server A and store those result in a variable which then the file is backed up in server B according to the result of the ls command which I performed in server A. However i am having problem running the command in the script after ssh ing into one of the server.

Thanks again in advance
# 5  
Old 11-25-2009
Java

add something like

Code:
FILES=( $(ssh host "ls ~/| sort") )

Now you got array of file names ... create similar for local files also and compare them ... as string.
# 6  
Old 11-25-2009
Thanks a lot once again, i guess there is no need to compare string because it will just do a backup regardless and if the file doesn't exist on the server B etc, it will just throw the error into dev/null. However please check if my code is correct before i try it on my server. Thanks in advance

Code:
TODAY=$(date '+%Y%m%d')
FILENAME=($(ssh serverA "ls ~/myDocA/| sort") )
array=( serverB serverC serverD serverE serverF serverG )
for index in array; do
  for file in $FILENAME ; do
  cp $file ${array[index]} :~/myDoc/${file%}.bak  #will this work? copying from remote server
  done
scp -r /~/myDocA/* ${array[index]} :~/myDoc/$file.$TODAY 2>/dev/null 
  # Better check $? exit code to verify if the file copy is successful ..
  echo "$file : is copied to server >> ${array[index]} << as filename: $file.$TODAY"   
done

exit 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Connecting to Windows server from UNIX through script

I am trying to connect to a Windows server say 10.1.1.10. This servers has a folder named RAJ in which there are multiple .zip files. All these zip files contain a text file called XYZ.txt. Now i have to merge the content of these XYZ.txt files from each of the .zip file and create a new text... (1 Reply)
Discussion started by: swapniljadav
1 Replies

2. Shell Programming and Scripting

FTP from windows to unix server using unix shell script

Hi, Is it possible to ftp a huge zip file from windows to unix server using unix shell scripting? If so what command i need to use. thanks in advance. (1 Reply)
Discussion started by: Shri123
1 Replies

3. Shell Programming and Scripting

Unix shell script to Copy files from one Windows server to another Windows server.

Can anybody please help me on how to code for the below requirement: I need to write a shell script (on different unix server) to copy files from multiple folders (ex. BRN-000001) from one windows server (\\boldls-mwe-dev4)to a different windows server(\\rrwin-ewhd04.ecomad.int). This shell... (4 Replies)
Discussion started by: SravsJaya
4 Replies

4. UNIX for Dummies Questions & Answers

Connecting to Unix server through Oracle

Hi, I need some help regarding oracle with unix script Actually i have a shell script and i want that it gets triggered through a oracle Db.I dont have any idea how is it done, even whether it is possible....pls help.....!!!! (3 Replies)
Discussion started by: fidelis
3 Replies

5. UNIX and Linux Applications

Error while connecting to MySQl Server 5.1.34 on UNIX platform.

Hi, I have MySQL 5.1.34 installed on Solaris and Linux machine. MySQl installed Solaris Machine - A.A.A.A Remote Machine - B.B.B.B I have user in mysql.user as below: | user | password | host |... (2 Replies)
Discussion started by: amit_27
2 Replies

6. UNIX for Dummies Questions & Answers

Connecting to UNIX server

Hi, My knowledge bucket of UNIX is almost empty. I have developed an application in EXCEL which can create XML files (Metadata) from Binaries. These binaries are stored in UNIX server on regular intervals. Currently I am transferring all binaries(GBs of files) to WINDOWS using WINSCP and... (2 Replies)
Discussion started by: bobs
2 Replies

7. Shell Programming and Scripting

How to connect unix server to unix server through shell scripts

Hi, I would like to connect UNIX server to UNIX server through shell scripts and i have some concepts . But i am totally confused how to connect UNIX server to UNIX server throuth running a script. It will be greatful to me if any buddy will help me. with simple example please. Thanks in... (2 Replies)
Discussion started by: phatan
2 Replies

8. Shell Programming and Scripting

How to connect unix server to unix server through shell scripts

Hi, I would like to connect UNIX server to UNIX server through shell scripts and i have some concepts . But i am totally confused how to connect UNIX server to UNIX server throuth running a script. It will be greatful to me if any buddy will help me. Thanks in advance. Phatan:) (2 Replies)
Discussion started by: phatan
2 Replies

9. Shell Programming and Scripting

Connecting to remote unix server using java?

I need help writing java code that can connect to a remote unix server, and run a script on that server. I have scoured the internet, but I have been unable to find proper documentation on how this can be accomplished. Any help is appreciated thanks. (1 Reply)
Discussion started by: developncode
1 Replies

10. UNIX for Advanced & Expert Users

connecting UNIX/Linux to NTP server

Hello i want to connect my solaris & Linux boxes to ntp server i used the command /usr/sbin/ntpdate -s -b -p 8 -u <NTP-IP> and added the NTP server as server in /etc/ntp.conf please help in completing the process and verifying it (1 Reply)
Discussion started by: learn82
1 Replies
Login or Register to Ask a Question