using ssh with a for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using ssh with a for loop
# 1  
Old 05-13-2008
using ssh with a for loop

How do you do an ssh on a for statement. I have done ssh on individula lines of code before with no problems.

#!/usr/bin/ksh

cat server_list.txt | while read line
do
ssh $line "for i in `lslpp -l |grep tsm`
do
lslpp -Lc $i |grep -v State |tr ':' ',' |awk -F, '{print $2, "," , $3}'
done" > ./test.csv
done;
chmod 777 test.csv

this is what I get the data is blowing up ?

ksh: syntax error at line 3 : `tivoli.tsm.client.ba.32bit.base' unexpected
# 2  
Old 05-14-2008
For starters, the multiple quotes in the string will be confusing it, change the choice of quotes in the awk.

Also, you don't need ksh for that, use a lighter shell (like /bin/sh)
# 3  
Old 05-15-2008
You can avoid some of the quoting issues by using a here document. In my limited testing, this causes the remote hosts' MOTD messages to be displayed, which may or may not be a problem for you; depends on the SSH version and stuff like that, too, I guess.

Code:
#!/bin/sh
while read host
do
  ssh $host <<"__HERE"
    for i in `lslpp -l |grep tsm`
    do
      lslpp -Lc $i |grep -v State |tr ':' ',' |awk -F, '{print $2, "," , $3}'
    done
__HERE
done <server_list.txt > ./test.csv
chmod 644 test.csv

Whatever you really want, chmod 777 is almost guaranteed to be wrong; I changed it to 644.

Looks to me like the redirection to test.csv was also in the wrong place (you would be overwriting it for each host) so I changed that as well.
# 4  
Old 05-15-2008
Hi all, Thank you for all the advise.
Unfortunately there are some interesting interactions going on either from the shell or the ssh in relation to the while.
ERA I tried your suggestion and I got this. Tried the -t & -T but no go
"
Pseudo-terminal will not be allocated because stdin is not a terminal.
Pseudo-terminal will not be allocated because stdin is not a terminal.
Pseudo-terminal will not be allocated because stdin is not a terminal.
Pseudo-terminal will not be allocated because stdin is not a terminal.
"
smiling dragon I did try different varrations of the quoting but still something with while and the ssh.

Went with this for now but I will continue trying to get while to work.
Thanks for the feedback.

#!/usr/bin/ksh

for line in `cat server_list.txt`
do
echo $line
echo "***********"
ssh $line lslpp -Lc |grep tsm |awk -F : '{print $2, "\t", $3}'
echo
done
# 5  
Old 03-05-2009
Please check if this helps:

for i in `cat serverlist`; do ssh $i "for i in \`lspv | awk '{print \$1}'\`; do echo \$i; done"; done
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Ssh disturbs while loop

Hi all, I have observed that ssh exits while loop. #!/bin/ksh typeset -i i typeset -i size set -A server = " " "SERVER1" "SERVER2" i=2 while ] do while read in_module do print $in_module size=`ssh ${server} ls -l $in_module 2>/dev/null | awk '{print $5}'` ... (3 Replies)
Discussion started by: Soham
3 Replies

2. Shell Programming and Scripting

Unable to loop with ssh

I read a file (iplist.txt) ine-by-line in a loop which has the list of all the server hostnames. With each hostname read; I do ssh and fire multiple commands to gather information about that systemas shown below. #!/bin/bash while IFS='' read -r line || ]; do echo "Text read from file:... (3 Replies)
Discussion started by: mohtashims
3 Replies

3. UNIX for Dummies Questions & Answers

SSH Loop command each minute

Hello all :) I want to know how can i do to loop a command each minute for example ? I'm in SSH under Putty ;) Thanks you for your help :) (1 Reply)
Discussion started by: stevefigueras
1 Replies

4. AIX

Need help on for loop inside ssh

Hi, I am having a file like, #cat file Jun 19 13:08 Jun 19 13:08 Jun 19 13:08 Jun 19 13:14 when I run the below comamnd locally it will work fine, IFS=$'\n'; for i in $(cat file) ;do echo "HI $i" ; done And the output is, HI Jun 19 13:08 HI Jun 19 13:08 HI Jun 19 13:08 HI... (1 Reply)
Discussion started by: sumanthupar
1 Replies

5. Shell Programming and Scripting

ssh breaks loop

Here is the smallest extract to demonstrate the problem that I experience. #!/bin/bash r=$1 while read ip do if ] ;then x=`ssh $ip echo "$ip"` else x=`echo "$ip"` fi echo $x done << EOF 192.168.8.241 192.168.8.241 EOF # Any IP with public key set (0 Replies)
Discussion started by: ivolvo
0 Replies

6. UNIX for Dummies Questions & Answers

ssh and for loop

Hi all, I am facing an issue while trying to access a for loop variable inside ssh. Can anyone please help me with what this issue is, the following is the code that I have -> IMPL_LOG_FOLDERS=(transaction_logs invalid_transaction_logs) sshg3 ftp_id@boxname << EOS for log_folder in... (5 Replies)
Discussion started by: anindyabecs
5 Replies

7. Shell Programming and Scripting

ssh inside a for loop

Hi, The requirement is to ssh to unix servers and oracle databases, to perform some monitoring activity. I'm using shell script to perfom this. I pass the server details and database to a variable ... SERVERS="SERVER1 SERVER2 SERVER3" DATABASE="DB1 DB2 DB3" for i in $SERVERS do ssh... (2 Replies)
Discussion started by: senthil3d
2 Replies

8. Shell Programming and Scripting

ssh and executing a for loop

Hi all, I am trying to run a script which is expected to do: on the remote machine, There are two directories /export/home/abc1,/export/home/abc2 i am trying to do, ssh SERVERNAME "for i in `ls -l /export/home/abc*|awk '{print $9}'`; do cd $i; ls -l; done" But its not working ,iam... (11 Replies)
Discussion started by: Jartan
11 Replies

9. Shell Programming and Scripting

ssh stops a while loop

The while loop exits (early) when a simple ssh command is run. #!/bin/ksh #set -x #------------------------------------------------------------------------- # Functions Section #------------------------------------------------------------------------- while : do cat list.txt|while read... (1 Reply)
Discussion started by: bkdias26
1 Replies

10. Shell Programming and Scripting

Pb in while loop with ssh command

Hi I use the following command to check if my_base is active or not : active_db=`${LOCAL_BIN}/ssh -l ${my_user} ${my_service} "ps -ef | grep ora_smon | grep ${my_base} | sed -e \"s/ */ /g\" | cut -d'?' -f2 | cut -d' ' -f3 | cut -d'_' -f3"` When I use a file listing databases to check them... (1 Reply)
Discussion started by: madmat
1 Replies
Login or Register to Ask a Question