cvjones360
cvjones360

Reputation: 33

bash capture output from remote machine into a variable

I'm trying to capture several remote device's output using the cli command then grep 717GE and print to screen for multiple hosts. As you see below its capturing the IP from $hosts and passing it to $outip, instead of what is shown when you use the same command from the command line. I thought the variable would capture the data returned from the given command. Could someone assist me and help me understand what I'm doing wrong? I'm really interested in learning so please hold off on the snarky comments. If possible.

for host in ${hosts[@]}; do
  seven=( $(cli $hosts show gpon ont summary -n --max=3 --host ) )
  outip=( $(grep 717GE $seven) )
  echo $outip
done

output:

+ for host in '${hosts[@]}'
+ seven=($(cli $hosts show gpon ont summary -n --max=3 --host ))
++ cli 10.100.112.2 show gpon ont summary -n --max=3 --host
+ outip=($(grep 717GE $seven))
++ grep 717GE 10.100.112.2 grep: 10.100.112.2: No such file or directory

Upvotes: 0

Views: 290

Answers (1)

that other guy
that other guy

Reputation: 123610

Don't use var=( .. ) unless you want to create an array, and use a bash here-string grep something <<< "$var" (equivalent to echo "$var" | grep something) to search for matching lines (otherwise you're saying that $var contains a list of filenames to search, and some options to set for grep):

for host in ${hosts[@]}; do
  # Assign as variable rather than array, and use $host instead of hosts
  seven=$(cli $host show gpon ont summary -n --max=3 --host )
  # Grep with "$seven" as text input and not as a list of filenames
  outip=$(grep 717GE <<< "$seven")
  echo "$outip"
done

Upvotes: 1

Related Questions