Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Variable Within File being Read

Help
scotttyso
2010-07-22
2013-05-02
  • scotttyso
    scotttyso
    2010-07-22

    I have written a small script to login to a router and run some show commands and expect output.  I would like the lines to be entered into a separate file and read and evaluated.  My problem is when a line from the file has a variable that is defined in the script the string is not read as a variable it is just read as text.

    Here is the contexts of the text file named SHOWCOMMANDFILE.txt
    show ip int Dialer1:Internet address is 24.$Octet.35.5/32:
    show ip int Dialer1:Address determined by IPCP:

    Here is my Script

    #!/usr/bin/expect
    set env(SHELL) "/bin/bash"
    set hostname

    spawn $env(SHELL)
    send "telnet $hostname\r"
    expect "sername" { send "username\r" }
    expect "assword" { send "password\r" }
    set Octet "101"
    expect "R5#"
    set devices "R5"
    set IS_OK 1
    foreach i $devices {
    set SHOWCMDS
    while { != -1} {
    set showcmd [lindex  0]
    set text_to_check [lindex  1]
    send "\r$showcmd\r"
    expect "$showcmd"
    expect {
    "$text_to_check" { }
    "R5#" { }
    }
    }
    }

    The second line I can match fine

    With the first line in the file when I check the debug I see
    does "\r\nDialer1 is up, line protocol is up\r\n  Internet address is 24.101.35.5/32\r\n  Address determin" (spawn_id exp4) match glob pattern "Internet address is 24.$Octet.35.5/32"? no

    But what I am trying it to do is have it interpret the line it is reading with the variable and I would hope to see the following from the debug
    does "\r\nDialer1 is up, line protocol is up\r\n  Internet address is 24.101.35.5/32\r\n  Address determin" (spawn_id exp4) match glob pattern "Internet address is 24.101.35.5/32"? yes

    This works if I get rid of $Octet in the file that I am reading from and replace it with 101 but it can change so I need it to be a variable.

     
  • scotttyso
    scotttyso
    2010-07-22

    Sorry, Any help would be greatly appreciated.

     
  • ender
    ender
    2010-07-22

    I'm not sure this is the best way but it works.

    In the file of commands change show ip int Dialer1:Internet address is 24.$Octet.35.5/32:
    to
    show ip int Dialer1:set text_to_check "Internet address is 24.$Octet.35.5/32":

    then in the script make it
    eval [lindex  1]

     
  • scotttyso
    scotttyso
    2010-07-22

    Actually I figured it out and I had to put it like:
    [puts [lindex  4]]

    But after thinking about it I wanted to be able to have the ability to have the show command also have the ability to have variables in it and the suggestion you have given works for what I want to do.

    Thank you very much again.

     
  • scotttyso
    scotttyso
    2010-07-22

    Ahm.. Let me correct myself. 

    My stuff was incorrect.  What you suggested got me on the road where I needed to be.  I can leave the original file like it was I just changed it to the following, which is similar to what you suggested

    [eval set output \"[lindex  1]\"]

     
  • scotttyso
    scotttyso
    2010-07-22

    Thanks Again