Script output as input for next command

Hi All,
Hoping you can help as im in desperate need... I'm very new to unix scripting so apoligies,

I have setup an expect script in order to log into a node on our network, This will provide an output as per the below

*********** information:
    *************: n/a
    TEST IP : n/a
    ****************: n/a                  ***************: n/a

  ********* Information:
    **********: 345:66:25434
    **********: 345:66:8468
    *********: n/a
    *******************: n/a
    *******************:
      ***** ***** **: 33:345:66
      TEST IP : 123.45.67.89

What I need to be able to do is read the IP address next to where it says TEST IP and then use this in another command. Problem I have is the IP does not always display in the bottom TEST IP line sometimes it can be in the first TEST IP line and N/A will be next to the second.

Essentially I need to read the IP address next to TEST IP and then use in another command. Would anyone be kind enough to help me out please. Sorry for the stars but as its security info I cant show it.

Thanks In advance.

This should output IP from lines with TEST IP

awk -F"[ :]+" --posix '$0~/TEST IP/ && $4~/^([0-9]{1,3}\.){3}[0-9]{1,3}$/ {print $4}' file
awk '/TEST IP/&&!/n\/a/{print $NF}' file

Thanks for your response's guys, So this is a print output from within an expect script. I need the IP addresses to be read and then I want to execute another command using that ip as the variable.

How would you recommend is best to do this.

Log to a file and run the awk on that file when the time comes.

if the next script is occuring almost simultaneously with the first script, you can just set a variable to the value of the ip address and then run the next command (see sample below) ...

MYIP=$(awk '/TEST IP/ && !/n\/a/ {print $2}'  file)
ping $MYIP 

If this condition is true for more than one IP
MYIP=$(awk '/TEST IP/ && !/n\/a/ {print $2}' file)
you can use exit to get only the first.
MYIP=$(awk '/TEST IP/ && !/n\/a/ {print $2;exit}' file)
This will make this work ping $MYIP

man xargs
awk '/TEST IP/&&!/n\/a/{print $NF}' file |xargs ping -n 1

Hi Guys
Thanks for your responses See below which is what I put together based on the feedback.. However its not working please can you advise what im doing wrong

spawn ssh "testuser\@****************"
expect -re "testuser@************* password:"
send "********\r";
expect -re "*********>"
log_file "log.txt" ;
send "show ******************** *******\r"
expect -re "************************"
log_file;
set MYIP=$(awk '/TEST IP/ && !/n\/a/ {print $2;exit}' file)
send "******** ******* ******* ****** *** $MYIP"

the MYIP bit should pickup an ip address from the show command output (output example below)

Idle Information:
    ******: 24580
    ******: 8468
    *******: n/a
    ***********: n/a
    ****************:
      ***************: 43:24:43
      TEST IP: 100.77.16.11

This then needs to run a command using the IP shown in the TEST IP from the text output.

The error i seem to get is

can't read "2": no such variable
    while executing
"set MYIP=$(awk '/TEST IP/ && !/n\/a/ {print $2;exit}' file)"
    (file "./Lookup" line 19)

Any Help you can offre would be much appreated

Try change
set MYIP=$(awk '/TEST IP/ && !/n\/a/ {print $2;exit}' file)
to
MYIP=$(awk '/TEST IP/ && !/n\/a/ {print $2;exit}' file)

Tried that mate and got the below.

can't read "2": no such variable
    while executing
"MYIP=$(awk '/TEST IP/ && !/n\/a/ {print $2;exit}' file)"
    (file "./lookup" line 19)

Get rid of that double quotes which is causing variable replacement.

Also use $NF instead of $2 to get IP address:

"MYIP=$(awk '/TEST IP/ && !/n\/a/ {print $2;exit}' file)"

to

MYIP=$(awk '/TEST IP/ && !/n\/a/ {print $NF;exit}' file)

Hi Yoda,
Tried that and got the below

can't read "NF": no such variable
    while executing
"MYIP=$(awk '/TEST IP/ && !/n\/a/ {print $NF;exit}' file)"
    (file "./sublookup" line 19)

Post the complete script.

You need to do the parsing/extraction natively within the expect script. The solutions given so far require a spawning of 'awk' which I'm not sure if expect allows.
I'm no expect guru, but maybe other will help.

#!/usr/bin/expect

set timeout 20

set ms [lindex $argv 0]

log_user 1

#Testnode

spawn ssh "testuser\@*************"
expect -re "testuser@****************'s password:"
send "************\r";
expect -re "Testnode>"
log_file "log.txt" ;
send "show ******************* $ms\r"
expect -re "Indicator"
log_file;
MYIP=$(awk '/TEST IP/ && !/n\/a/ {print $NF;exit}' file)
send "show *********************************** $MYIP"

Can anyone add to the above ?

Any help is appreaciated as im stuck!

Study tk and tcl tutorials online, as expect is made from, and scripted with, these languages.

Alternatively, I have written expect-like scripts that mostly ran in the parentheses piped to stdin, examining the log file where stdout and stderr are being written. This rescues me from learning another script langage. It does have minor problems getting control of /dev/tty if that is where passwords and such are read from. For instance, a primitive remote smtp/esmtp email client from telnet:

$ (
  >>my_mailer_log
 
  <script that examines log file my_mailer_log for progress and generates input>
 ) | telnet mailhost 25 > my_mailer_log 2>&1

Hi Guys,
Just to follow up really, I managed to complete what i needed by using a perl script which then called an expect script in order to login to the relivant elements and run the commands required.

Thanks to everyone who helped.