Unable to loop with ssh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to loop with ssh
# 1  
Old 11-14-2018
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.

Code:
#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
    echo "Text read from file: $line"
    ssh user1@$line "df -h; kstat cpu_info|grep core_id|sort -u|wc -l"
done < "$1"

The loop shows the correct output for only the first server and then terminates.

How can I make the loop traverse the entire list of servers(hostnames) ?
# 2  
Old 11-14-2018
That may sounds strange, but as matter of fact: ssh eats your loop arguments here. This specific implementation of a loop does not work with an ssh-command involved. Use a method like this instead:


Code:
for line in $(cat "$1"); do
          echo "Text read from file: $line"
       ssh user1@$line "df -h; kstat cpu_info|grep core_id|sort -u|wc -l"

done

This way the whole arguments are read at the begin of the loop and ssh is prevented from slurping up whole stdin.

Last edited by stomp; 11-14-2018 at 08:11 AM.. Reason: fixed syntax error
# 3  
Old 11-14-2018
I'm getting error using your suggestion:

Code:
./testssh.sh: line 9: syntax error near unexpected token `echo'
./testssh.sh: line 9: `          echo "Text read from file: $line"'

------ Post updated at 08:00 AM ------

Nevermind, the do was missing in the for loop. Thank you ... it now works !!
# 4  
Old 11-14-2018
Quote:
Originally Posted by stomp
That may sounds strange, but as matter of fact: ssh eats your loop arguments here. This specific implementation of a loop does not work with an ssh-command involved.
Yes it does. It requires a workaround but it does.

Also, that's a dangerous use of backticks and there's never any good reason to use it.

Code:
#!/bin/bash
while IFS='' read -u5 -r line || [[ -n "$line" ]]; do
    echo "Text read from file: $line"
    ssh user1@$line "df -h; kstat cpu_info|grep core_id|sort -u|wc -l"
done 5< "$1"

This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Unable to login in ssh terminal

Hi guys when ever i tried to connect aix server in my institute through ssh terminal the pop is coming like network is unreachable .Am using MacBook air the other guys who are using putty software in windows they can easily login in tho the server through remotely . Is there any one can... (3 Replies)
Discussion started by: aashishb007
3 Replies

2. UNIX for Advanced & Expert Users

Unable to ssh to remote server

Hi, I have two SunOs sparc servers mac1 and mac2. I have exchanged keys between them inorder to passwordless login ssh from mac1 to mac2. However, it is failing after authentication. Part of the debug is as below. Please suggest whats wrong and how do i fix that!! Note: i do not have... (1 Reply)
Discussion started by: mohtashims
1 Replies

3. HP-UX

Unable to ssh to server in HP-UX.

Hello Experts, I'm Unable to ssh to server after killing few processes on /opt filesystems. Can you please help me to resolve this. (3 Replies)
Discussion started by: purushottamaher
3 Replies

4. Shell Programming and Scripting

Unable to run command after ssh

Hello, I am trying to create a ksh script to login to server and collect gather output of some command to troubleshoot some issue. DATE=`date +%b.%d.%Y.%M.%H` echo " Enter emp id to login to server" read Eid Eid=$Eid echo " Enter hostname of the system" read HOST HOST=$HOST... (2 Replies)
Discussion started by: saurabh84g
2 Replies

5. Shell Programming and Scripting

Unable to run application using ssh

I'm testing a C++ based application (HLR) in my solaris system. Whenever i start the application remotely from some other solaris server using ssh command the application throws an error and goes down. command i used: ssh root@192.168.151.77 "./start_hlr.sh" Below is the error observed : ... (1 Reply)
Discussion started by: Arun_Linux
1 Replies

6. HP-UX

Unable to connect SSH from HP-UX

Hi, I'm trying to connect from an HP-UX with SSH2 client(ssh2 3.1.2 on hppa1.1-hp-hpux11.00) to an SSH2 server on a VxWorks system. The SSH connection is failing with the below connection logs: > /usr/local/bin/ssh2 -v -l testuser 10.10.10.10 debug: Ssh2/ssh2.c:1391: Using file... (4 Replies)
Discussion started by: ysafi
4 Replies

7. Solaris

unable to ssh to remote host

server is ok, I can login on console. however, when I use SSH teachia, there is no repsond. i have check ps-ef | grep ssh, it shows ok. restart ssh too. still not working. Anything else I need to check? # ps -ef | grep ssh root 24706 1 0 Jun 12 ? 0:00... (7 Replies)
Discussion started by: uuontario
7 Replies

8. Linux

Unable to ssh using identity keys

Hi all, i am trying to ssh into a remote server without password, i tried the command with the verbose command 'ssh -v -l user1 10.10.10.10'OpenSSH_3.6.1p2, SSH protocols 1.5/2.0, OpenSSL 0x0090701f debug1: Reading configuration data /etc/ssh/ssh_config debug1: Applying options for * debug1:... (0 Replies)
Discussion started by: new2ss
0 Replies

9. HP-UX

Unable to install SSH

Hello!! I have an HP-UX 11.23 box. I downloaded an SSH depot file form HP website. Using SAM, I tried to install the depot file. However, SAM declared "it cannot find any compatible depot file to install". Due to that, I tried using command line 'swinstall' just like HP tutorial taught me.... (8 Replies)
Discussion started by: jembalang
8 Replies

10. AIX

Unable to ssh out

As the title says, I'm unable to ssh out of boxA to other nodes on the network. I am running 5.2 ML6. The version of ssh client is: openssh.base.client 4.1.0.5301 COMMITTED Open Secure Shell Commands The program installed okay with no errors. When I try to ssh to another node on... (6 Replies)
Discussion started by: outtacontrol
6 Replies
Login or Register to Ask a Question