Error Executing a script(SSH problem)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Error Executing a script(SSH problem)
# 1  
Old 07-14-2010
Error Executing a script(SSH problem)

Hi i m writing a script which will fetch data from different machines and display it on single terminal.

I have created a file named SERVERNAMES containing ip address of machines i need to monitor.
Then for every IP i m issuing ssh command to get date on that machine.
However When i m trying it on the script it is just giving me output of first server and it is ignoring rest servers mentioned in file.

ssh works find without password for all machines mentioned in SERVERNAMES.


Code:
# cat SERVERNAMES
10.180.45.231
10.180.45.235
10.180.45.238
10.180.45.211

Script
Code:
#!/bin/bash

while read line
do
#Calculate Load on remote machine.
Date=`ssh -T -q $line date`
echo "Date on $line is:$Date"
done < SERVERNAMES

Code:
output.
Date on 10.180.8.231 is:Wed Jul 14 10:48:46 IST 2010

please help as i m not getting the output for .
Code:
10.180.45.235
10.180.45.238
10.180.45.211

Is there anything wrong in ssh?
# 2  
Old 07-14-2010
I had the same problem, seems that it's a problem to use ssh connection in a while loop, the loop works just for the first time


using a "for" loop solved this problem for me

Code:
for line in $(cat servernames.txt)
do
Date=`ssh -T -q $line date`
echo "Date on $line is:$Date"
done

# 3  
Old 07-14-2010
working code u just need to add -n option.

Code:
#!/bin/bash

while read line
do
#Calculate Load on remote machine.
Date=`ssh -T -q -n $line date`
echo "Date on $line is:$Date"
done < SERVERNAMES

# 4  
Old 07-14-2010
The problem is that the loop and ssh (by default) read from the same stdin, and ssh "consumes" (eats) the input to feed it to the remote command, that would otherwise have been read by the loop. Otherwise, you wouldn't be able to run a shell on the remote system. Even when running a specific command, that command might still read from stdin, and since ssh reads from stdin you can supply data to it using the local stdin. Use ssh -n option to redirect stdin from /dev/null (prevents reading from stdin) and use ssh -T that runs the command without attempting to allocate a tty. ssh waits until all connections to stdin (from the ssh perspective) are closed. With an interactive shell the stdout of all child processes is connected to the shell, which maintains a single connection to the terminal. So when the shell terminates that means all connections to the stdin of the ssh session are closed. Without a shell all children are directly connected so the ssh session is forced to stay around.
This User Gave Thanks to royalibrahim For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remote script via SSH not executing

I have worked on multiple scenarios to execute remote script via ssh. This problem I am not able to resolve. 2 linux hosts. Server1, Server2 on Server1 I have script called ~/scripts/start_standalone.sh XXXX cd $JBOSS_HOME NODENAME=xyz; IP_ADDR=`hostname`; MGMT_IPADDR=`hostname`;... (3 Replies)
Discussion started by: oraclermanpt
3 Replies

2. Linux

Executing a script in remote machine through ssh

How to execute a script in remote machine through ssh I have a script test.sh which does some backup activity in remote machine. Wanted to keep backup also in remote machine. ssh -l username <remote machine> "commands to be exceuted as ; separted" but how to put the script in the place of... (5 Replies)
Discussion started by: sanvel
5 Replies

3. Shell Programming and Scripting

Executing a background script using ssh keys

Greetings, i've been working with a user-friendly menu on ksh to allow users execute scripts located on a remote server, so they wont have to login and manually launch those scripts every single time. This is a HP-UX box and currently on a /usr/bin/ksh shell. I've setup ssh keys on both... (1 Reply)
Discussion started by: nbriozzo
1 Replies

4. Shell Programming and Scripting

having problem while executing the script

Hi i am getting stuck while executing the script in the below line .Please help me out if ;then Name=`grep -i $Size $FILE|awk '{print $(NF-1),$NF}' |head -1` else Name="$Nam" fi (3 Replies)
Discussion started by: soumyamishra
3 Replies

5. Shell Programming and Scripting

executing command in a remote machine through ssh - shell script

Hi All, i have two machines like x and y . my requirement is i should connect to machine Y from x through ssh connection . and do some operation such as copy and move and delete files in Y machine . i tried with this code but it is doing in machine x only . and i need to exit from Y when... (1 Reply)
Discussion started by: rateeshkumar
1 Replies

6. Shell Programming and Scripting

Problem executing a script

Hi Friends, When I try to execute one of my script script1.shl, I am getting the error message as "/tmp/sh1871978.13: cannot create" . I could not find the specified sh* file in the /tmp directory and I also checked the disk space of /tmp and it is only 60% filled. Please help me on... (5 Replies)
Discussion started by: mr_manii
5 Replies

7. Shell Programming and Scripting

Executing a script on a remote system via SSH

Hello all, I have a relatively simple script I wrote to generate a count of errors broken down. What I would like to do is execute this script from another server so that I don't actually have to log in to the server to run the check. The script on what we'll call "Server A" is: ... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

8. Shell Programming and Scripting

Problem with executing script using ssh

I'm writing a script which is meant to ssh into a remote machine, sed a file there, and write the output to a new file on the remote machine. Obviously the easy way to do this is to write and execute the script on the remote machine, but I'm trying to do this all via ssh since I like to keep and... (4 Replies)
Discussion started by: maethlin
4 Replies

9. Shell Programming and Scripting

Problem executing a script

The script startserver.sh has permissions to execute. Still the nohup command returns error with 'No such file or directory' Any sugggestions: $ nohup ./startserver.sh & Error $ nohup: appending output to `nohup.out' nohup: cannot run command `./startserver.sh': No such file or... (4 Replies)
Discussion started by: hemangjani
4 Replies

10. Shell Programming and Scripting

Error while executing glance using ssh

Hi, I'm running glance ussing ssh from a "HP-UX" machine for a Sun machine. Here is a sample code:- x:- HP-UX y:- Sun Extracting a value from glance outuput. ssh x@y "/opt/perf/bin/glance -f -adviser_only -iterations 2 -maxpages 1 > glance.op ; echo `head -1 glance.op | a wk '{print... (0 Replies)
Discussion started by: bishweshwar
0 Replies
Login or Register to Ask a Question