2

The following ping script is running just fine in both script form and interactive shell

ping-only.sh

date
for i in 10.1.1.1 10.1.1.3 10.4.1.13
    do  echo -e "\nIP: $i"
    ping -c 1 $i | grep from
done

However, when I run the SSH shell script, it just stuck at the first host

ssh-get-banner-only.sh

date
for i in 10.1.1.1 10.1.1.3 10.4.1.13
    do  echo -e "\nIP: $i"
    timeout 1 ssh $i
done

Sample output

user@APIC> ./ssh-get-banner-only.sh
Mon May  6 04:43:24 UTC 2019

IP: 10.1.1.1
###############################################################
#                 Authorized access only!                     # 
###############################################################

It work just fine when I paste the same code manually to the shell

user@APIC> date
Mon May  6 04:36:33 UTC 2019
user@APIC> for i in 10.1.1.1 10.1.1.3 10.4.1.13
> do  echo -e "\nIP: $i"
> timeout 1 ssh $i
> done


IP: 10.1.1.1
###############################################################
#                 Authorized access only!                     # 
###############################################################

Password:

IP: 10.1.1.3
###############################################################
#                 Authorized access only!                     # 
###############################################################

Password:

IP: 10.4.1.13
###############################################################
#                 Authorized access only!                     # 
###############################################################

Password:
user@APIC>
2
  • Are you sure the same interpreter is used when executing the script manually vs executing your script? Try to find out the shell interpreter with echo "$SHELL" and then add a shebang to the first line of your script. Commented May 6, 2019 at 14:12
  • @waza-ari, user@APIC:~> echo $SHELL /bin/bash user@APIC:~> Doesn't matter whether the shebang is there or not. Still getting the same output Commented May 7, 2019 at 2:47

1 Answer 1

0

To address this issue, you can try modifying the script to use the -o ConnectTimeout option of the ssh command, which specifies the maximum time to wait for the SSH connection to be established. Here's an updated version of the script using this option:

#!/bin/bash

date
for i in 10.1.1.1 10.1.1.3 10.4.1.13; do
    echo -e "\nIP: $i"
    ssh -o ConnectTimeout=1 $i "echo Connected"
done

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.