Find the answer to your Linux question:
Results 1 to 7 of 7
hello! I'm using expect to log in to cisco routers and get their show run/vers. I've hit a snag expecting the prompts, so what I now plan to do is ...
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Posts
    26

    Expect: Creating variables on the fly

    hello!

    I'm using expect to log in to cisco routers and get their show run/vers. I've hit a snag expecting the prompts, so what I now plan to do is use "show run | i hostname" (which will return any line with "hostname" on it) and then grab the hostname (the next string) and use it as a variable. Is this even possible?
    How can this be done?

  2. #2
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,422
    What language is it?
    New Users, please read this..
    Google first, then ask..

  3. #3
    Just Joined!
    Join Date
    Mar 2008
    Posts
    26
    Expect.

    That's based on TCL by the way.

    *edit* Wait, did you mean the "sh run |i hostname"? That's just a cisco IOS command, and it's not relevant - it's a remote comand I'm using to get the output - it's how to process the output I'm interested in.

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I'm not an expect expert. But my O'Reilly book on expect says:
    expect stores the matched characters [from the input] in a variable called expect_out(0,string).
    Since I don't know how to use variables in expect, you get to figure that out. :) Go here.

    Hope this gets you started.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Just Joined!
    Join Date
    Mar 2008
    Posts
    26
    I wish I had that book. I've been getting extracts from it all day from O'reilly's site. However, I've tried using $expect_out(0,string) verbose, and it doesn't ever work for me.

    You see $expect_out is an array, and the only thing that seems to be in the array for me is the variable (buffer), which is basically the last command/response up to the last expect. I can get that to output 4 lines of code:
    Prompt
    good info
    useless info
    prompt

    and I just need the "info" I've bolded in green as my variable. I've created a workaround, so it's not as important now, but I had this idea as a flash of inspiration last night, and if it's workable, it would be helluva useful for getting specific prompts on cisco equipment.

    Thanks again for replying Bill - I think I might need to get me that book!

    >_> Maybe I can get work to buy it...

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    What happened when you went to the linky?
    --
    Bill

    Old age and treachery will overcome youth and skill.

  7. #7
    Just Joined!
    Join Date
    Mar 2008
    Posts
    26
    Oh, I've been there before tbh, and I found it very unhelpful. the o'reilly site is much better, chapter 3 is online, and each section has exerpts you can read. I've learned more from that in one day (today) than I have in the 2 previous days I spent sat here figuring it out.

    O'reilly rules man.

    I sound like I work for o'reilly now

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...