This is part of code in an expect script, I have a problem with searching IP addresses from a file whose name is in the variable $filename.

#!/usr/local/bin/expect -- 
###Made by Etihkru####

set env(TERM) vt100
set env(SHELL) /bin/sh
set env(HOME) /usr/local/bin
exp_internal 1
set PASSWORD eri
set SIU [lindex $argv 0]
match_max 1000
if {$SIU == ""} {
puts "Missing name of SIU. Run command as perl SIUADD FRTXXX"

spawn ssh mashost
set USER admin
set PASS hidden

expect {
    "assword"  {send "$PASSWORD\r"}

        expect "ranosusr@rn2osscs603"
        send -- "cd /var/opt/ericsson/edd/ARNE_SIU \r"
        expect "ranosusr@rn2osscs603"
        send -- "grep -il $SIU *\r"
        set prompt {ranosusr@rn2osscs603> }
        expect -re "(FXL\\S+\\.xml).*$prompt$"
        set filename $expect_out(1,string) 
        send -- "cat $filename | grep 'url=' \r"
        expect -re "something.*$prompt$" //Expect this ip adreess ???
        set IP $expect_out(1,string)//put it to variable IP ???

spawn ssh admin@$ip
expect {
    -re "RSA key fingerprint" {send "yes\r"}
    timeout {puts "Host is known"}

expect {
    "assword"  {send "$PASS\r"}

expect "Osmon>"
send -- "resumePMMeasurements STN=0,MeasurementDefinition=0 sftp://pmup-rn2sossv605:[email protected]/GRAN/rn2sossv605/$SIU/NeTransientUp \r"
expect "Osmon>"
send -- "getalarmlist \r"
expect -re "(\Operation Succeed\)"
send -- "rev \r"
expect "Osmon>"
send -- "subscribe 1 \r"
expect "Osmon>"
send -- "getsubscriptionstatus 1 \r"
expect "Osmon>"

This is the ouput:

 expect: does "> cat FXL704_FRTAMX_SIU_ARNE.xml | grep 'url=' " (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no
    expect: does "> cat FXL704_FRTAMX_SIU_ARNE.xml | grep 'url=' \r\r\n" (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no
                          <emUrl url=""/>    
    expect: does "> cat FXL704_FRTAMX_SIU_ARNE.xml | grep 'url=' \r\r\n                  <emUrl url=""/>\r\r\n" (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? no
    expect: does "> cat FXL704_FRTAMX_SIU_ARNE.xml | grep 'url=' \r\r\n                  <emUrl url=""/>\r\r\nranosusr@rn2osscs603> " (spawn_id exp4) match glob pattern "ranosusr@rn2osscs603"? yes

I need the pure IP address, cleared from other characters (e.g., because I need to use it in next SSH login, so I need to assign it to a variable $IP.

  • Please show us your input as well. Are we talking IPv4 or 6?
    – terdon
    Mar 13, 2014 at 14:06
  • ipv4, what do you mean on input, when I connect to server it looks like the second line above, if this is what you meant? Mar 13, 2014 at 14:10
  • No, you are trying to grep an IP within a file. It would help to know what the rest of the line looks like. Is the IP on its own line? Surrounded by text? Other numbers? What kind of data could we expect to see in $filename?
    – terdon
    Mar 13, 2014 at 14:12
  • aha, ok, so the ipaddress is in xml. file <connectionStatus string="ON"/> <Connectivity> <DEFAULT> <emUrl url=""/> <ipAddress ip_v4=""/> <hostname string=""/> <nodeSecurityState state="ON"/> <boardId string=""/> <Protocol number="0"> ..........there are two address, they are the same, and I need to grep one of them:) Mar 13, 2014 at 14:13
  • Maybe you forget to escape the first curly brace.
    – cuonglm
    Mar 13, 2014 at 14:22

1 Answer 1


Your main problem is this line

cat $filename | grep {1,3\}\.\{1,3\}\.\{1,3\}\.\{1,3\} \r"

Apart from the useless use of cat, the main mistake is that {1,3} means "match 1,2 or 3" times, but your not telling it what to match. The syntax is also wrong, {} need to be escaped for regular grep. What you are looking is better written as:

grep -Po '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' filename

This uses PCRE syntax (-P) where \d means "digits" and which I find easier but to use standard grep regular expressions (BREs) you could do:

grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'
  • The -o makes grep print only the matching part of the line.

  • You never want to match the end of line character, there's no need. You should always try and make your regular expressions as simple as possible. It is always best to use the smallest, simplest expression that matches your data.

  • You most certainly don't want to search for \r. While Windows does end lines with \r\n, most systems don't and in any case, there's little point in looking for it. If you need to match to the end of a line, use $ which will work in all cases.

Anyway, to save in a variable do this:

ip=$(grep -Po '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' filename)

You can now echo $ip to print the IP.

Based on your last edit, I am assuming that you want the ip defined by the ipAddress tag. I will now give you the obligatory warning: NEVER use regular expressions to parse XML or similar formats. That said, to get only the ipAddress IP, do this:

ip=$(grep -Po 'ip_v4="\K[0-9.]+' $filename)

The \K discards whatever was matched before it so you get only the IP part and since you know that the IP you are looking for is always preceded by ip_v4=", you can simply look for the longest string that contains only numbers and ..

  • well it's not working, when I use ip=$(grep -Po 'ip_v4="\K[0-9.]+' $filename) I got ite to file. Writing to stdout instead.\r\nThe file '-import' can not be found.\r\nranosusr@rn2osscs603" invalid command name "0-9." while executing "0-9." invoked from within "send -- "ip=$(/usr/xpg4/bin/grep -Po 'ip_v4="\K[0-9.]+' $filename) \r"" (file "siu1" line 37) Mar 13, 2014 at 14:43
  • @user3319356 Why are you sending the \r? That does not end a line, you need \n (*nix) or \r\n (Windows). When typing commands, please use backticks (``` ` ``) to format them. If your command was send -- "ip=$(/usr/xpg4/bin/grep -Po 'ip_v4="\K[0-9.]+' $filename) \r", you probably also need to escape the ", try send -- "ip=$(/usr/xpg4/bin/grep -Po 'ip_v4=\"\K[0-9.]+' $filename)"
    – terdon
    Mar 13, 2014 at 14:50
  • this is the #!/usr/local/bin/expect , shouldn't the variable be se as set VAR [something]? Mar 13, 2014 at 14:50
  • @user3319356 sorry, I'm not really familiar with expect syntax, I can only help you with getting the shell command right.
    – terdon
    Mar 13, 2014 at 14:51
  • yes, still is not working, but thanks for help...This expect is killing me also Mar 13, 2014 at 14:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .