0

Here I'm trying to remotely execute a command on multiple devices using ssh using while loop in bash however I'm getting output for the first IP in source file but not for others.

cat HPE_FRAMES_IP
10.123.112.184
10.224.20.120
10.18.133.53
10.18.133.28
10.18.133.14
172.18.171.91
10.1.180.99
10.1.180.70
10.1.180.77
10.1.180.95

cat HPE_FRAMES_IP | while read IP ; do ssh Reports@$IP showsys | awk '{print $2,$5}' ;  done

--Name--- Nodes
ABCHP8401 SL-NUMBER
  • @Weijun Zhou can you please help me with this – user3016638 Mar 20 at 12:52
  • It doesn't really make a difference whether awk is executed remotely or locally I suppose? – Weijun Zhou Mar 20 at 12:56
  • 'cat HPE_FRAMES_IP | while read IP ; do ssh Reports@$IP "showsys | awk '{print $2,$5}'" ; done' showsys: Invalid option: | – user3016638 Mar 20 at 12:59
  • Give ssh the -n option, otherwise it will gobble up all of its input and your while loop will run just once. – Mark Plotnick Mar 21 at 13:04
4

For loop seems to be good option here:

for i in $(cat HPE_FRAMES_IP); do ssh Reports@$i showsys | awk '{print $2,$5}' ;  done
  • 'for i in $(cat HPE_FRAMES_IP); do ssh Reports@$IP showsys | awk '{print $2,$5}' ; done' ssh: Could not resolve hostname : Name or service not known ssh: Could not resolve hostname : Name or service not known ssh: Could not resolve hostname : Name or service not known ssh: Could not resolve hostname : Name or service not known ssh: Could not resolve hostname : Name or service not known – user3016638 Mar 20 at 13:02
  • 1
    @$i. Not @$IP. – Weijun Zhou Mar 20 at 13:04
  • already modified :) – Vivek Kanadiya Mar 20 at 13:09
  • I'm responding to the comment by user3016638. – Weijun Zhou Mar 20 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.