Looping connect to different server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looping connect to different server
# 1  
Old 05-11-2007
Looping connect to different server

Hi there. I am attempting to write a script that will read through a flat file, get the server and directory that I want the size for, connect to the server, get the directory size, and send it to an output file. Here is what I have:

while read LINE
do
NAME=`echo $LINE | awk -F'~' '{print ($1)}'`
SERVER=`echo $LINE | awk -F'~' '{print ($2)}'`
DIRECTORY=`echo $LINE | awk -F'~' '{print ($3)}'`
DATABASE=`echo $LINE | awk -F'~' '{print ($4)}'`
SIZE=`/usr/bin/ssh target_account@remote_host du -s $DIRECTORY`
echo $NAME $DATABASE $SIZE >> $MDIR/bldtdout.txt
done < $MDIR/billdtad.txt

My input file(billdtad.txt) looks like:
{NAME}~{SERVER}~{DIRECTORY}~{DBNAME}

I have two rows of data in the input file. Only the first row is being processed.

I have another script that uses this same basic code and works fine. The only difference between the two is that the other script is getting directory sizes on the same server that it is running on, so line 9 looks like:
SIZE=`du -hs $DIRECTORY`

Because the scripts are otherwise the same, I am guessing that my problem has to do with the read loop and the ssh connection inside the loop.

How do I get it to read through the entire file and connect to the different servers?

Last edited by la_womn; 05-11-2007 at 06:36 PM..
# 2  
Old 05-12-2007
Code:
while read LINE 
do
   NAME=`echo $LINE | awk -F'~' '{print ($1)}'` 
   SERVER=`echo $LINE | awk -F'~' '{print ($2)}'` 
   DIRECTORY=`echo $LINE | awk -F'~' '{print ($3)}'` 
   DATABASE=`echo $LINE | awk -F'~' '{print ($4)}'` 
   SIZE=`</dev/null /usr/bin/ssh target_account@$SERVER du -s $DIRECTORY`
   echo $NAME $DATABASE $SIZE >> $MDIR/bldtdout.txt
done < $MDIR/billdtad.txt

Jean-Pierre.
# 3  
Old 05-12-2007
Code:
while IFS='~' && read NAME SERVER DIRECTORY DATABASE dummy
do
    SIZE=`/usr/bin/ssh target_account@$SERVER du -s $DIRECTORY`    
    echo "$NAME $DATABASE \c"
    echo "$SIZE" | tr -s '\n' ' ' 
done < $MDIR/billdtad.txt >  $MDIR/bldtdout.txt

It sounds like you are getting multiple lines back from ssh?

This fixes that problem and simplifies the code little bit...what does the ssh command give when you execute it from the command line?

.. ssh is fussy about being called in certain ways, like when stdin is not a tty.
# 4  
Old 05-12-2007
Jean-Pierre - why /dev/null redirected?
# 5  
Old 05-12-2007
just in the case which ssh requires input, to avoid $MDIR/billdtad.txt be read.

Jean-Pierre.
# 6  
Old 05-14-2007
Thank you! I added </dev/null to my code and it works perfectly. I am going to look into shortening the code by using Jim's suggestions. Thank you both.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to connect from one server(Current) to another server?

Hi, I have something like this. I have connected to one server ServerA and from this ServerA, i need to do ssh connection to ServerB and then execute my queries. #!/usr/bin/expect spawn ssh Myusername@ServerB expect "password" send "Mypassword\r" interact Output.ksh: spawn: not... (5 Replies)
Discussion started by: Samah
5 Replies

2. Linux

How to connect Linux server (configure two way authentication) with Windows server?

Hi my name is Manju. ->I have configure the two way authentication on my linux server. ->Now I am able to apply two way authenticator on particuler user. ->Now I want to map this linux server to my AD server. ->Kindly tell me how to map AD(Active Directory) with this linux server. ... (0 Replies)
Discussion started by: manjusharma128
0 Replies

3. Shell Programming and Scripting

Connect to one server from other server without asking password

Hi , I am using redhat linux 5.I.I need a command or script which need to login from one server to another without asking password.This can be done even providing password on the command line is also fine.I have tries sshpass but it didn't work.ssh key generations is not preferred here. ... (7 Replies)
Discussion started by: muraliinfy04
7 Replies

4. Shell Programming and Scripting

Connect to server-1 from server-2 and get a file from server-1

I need to connect to a ftp server-1 from linux server-2 and copy/get a file from server-1 which follows a name pattern of FILENAME* (located on the root directory) and copy on a directory on server-2. Later, I have to use this file for ETL loading... For this I tried using as below /usr/bin/ftp... (8 Replies)
Discussion started by: dhruuv369
8 Replies

5. Shell Programming and Scripting

Connect (SSH) to Windows server via Linux server through a script and passing command.. but failing

I am trying to connect to Windows server via Linux server through a script and run two commands " cd and ls " But its giving me error saying " could not start the program" followed by the command name i specify e g : "cd" i am trying in this manner " ssh username@servername "cd... (5 Replies)
Discussion started by: sunil seelam
5 Replies

6. UNIX for Advanced & Expert Users

Public key to connect from one ftp server to other server

How to generate public key to connect from one ftp server to other server to use in scripting. (1 Reply)
Discussion started by: sridhardwh
1 Replies

7. Linux

Generate public key to connect from one ftp server to other server

How to generate public key to connect from one ftp server to other server to use in scripting. (0 Replies)
Discussion started by: sridhardwh
0 Replies

8. Programming

Looping connect call for a non blocking socket

will there be any unexpected results on looping connect call for a non blocking socket to determine the connection based on error code. I am getting connection unsuccessful intermittently and so wondering whether is the timeout 500 millisec not sufficient or looping connect cause any unexpected. ... (7 Replies)
Discussion started by: satish@123
7 Replies

9. Shell Programming and Scripting

Connect to Server B from Server A and Archive the file

Hi I need to connect to a ServerB from Server A eg: Server A : 172.20.273.51, un: xxx, pwd: xxx Server B: 172.20.273.51, un:yyy, pwd: yyy Need to copy the files(with name starts with IME*) from /grid/pc/IME* to /grid/pc/archive What will be the command or script to... (3 Replies)
Discussion started by: vsmeruga
3 Replies

10. Windows & DOS: Issues & Discussions

To Connect to Windows server from Unix server

Hi i am writing a script in unix where after some validations, i require the script to connect to a windows server and then kich off a batch file there. i tried ftp and got the error message that "the remote host refused an attempted connect operation". I am able to connect to this unix... (4 Replies)
Discussion started by: vidzz911
4 Replies
Login or Register to Ask a Question