Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8892367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:58:50+00:00 2026-06-14T22:58:50+00:00

I’m pretty new to scripting in general. I’m writing an expect script that ssh’es

  • 0

I’m pretty new to scripting in general. I’m writing an expect script that ssh’es into a Cisco switch, and runs the “show cdp neighbors” command to get a list of all the devices connected to the switch. I then save the output into a variable and exit the ssh session.
I have the username and password being set in the included file.

#!/usr/bin/expect -f
#exp_internal 1

source accountfile
set timeout 10
spawn $env(SHELL)
expect "#"
send "ssh $USERNAME@<hostname>\r"
expect {
  "continue connecting" {
    send_user "Adding host to ssh known hosts list...\n"
    send "yes\n"
    exp_continue
  }
  "Do you want to change the host key on disk" {
    send_user "Changing host key on disk...\n"
    send "yes\n"
    exp_continue
  }
  "assword:" {
    send "$PASSWORD\r"
  }
}
expect "#"
send "term len 0\r"
expect "#"
send "show cdp neighbors\r"
expect "#"
set result $expect_out(buffer)
send "exit\r"
expect "#"

So then I want to take $result and look for lines that contain ‘ R ‘, and save those lines to a file (R with spaces on either side indicates a router, which is what I’m interested in)

The problem is that if the name of a connected device is long, it puts the name of the device on one line, and then the rest of the data about the device on the next line. So if I match the ‘ R ‘ string, I won’t get the name of the device, since the name is on the previous line.

Device ID        Local Intrfce     Holdtme     Capability Platform  Port ID
...
<device_name_really_long>
                 Gig 2/0/52        171             R S I  WS-C6509  Gig 3/14
<device_name2>   Gig 2/0/1         131             H P M  IP Phone  Port 1
...

Any ideas? there’s probably a regex that would do it, but I don’t know squat about regex.

SOLVED: thanks to Glenn Jackman

I ended up having to add an expect condition to check if I had a full buffer, so my final code looks like this:

#!/usr/bin/expect
#exp_internal 1
match_max 10000
set expect_out(buffer) {}
set timeout 30

source accountfile
spawn $env(SHELL)
expect "#"
send "ssh $USERNAME@ol2110-3750stack.sw.network.local\r"
expect {
    "continue connecting" {
        send_user "Adding host to ssh known hosts list...\n"
        send "yes\n"
        exp_continue
    }
    "Do you want to change the host key on disk" {
        send_user "Changing host key on disk...\n"
        send "yes\n"
        exp_continue
    }
    "assword:" {
        send "$PASSWORD\r"
    }
}
expect "#"
send "term len 0\r"
expect "#"
send "show cdp neighbors\r"
set result ""
expect {
    {full_buffer} {
        puts "====== FULL BUFFER ======"
        append result $expect_out(buffer)
        exp_continue
    }
    "#" {
        append result $expect_out(buffer)
    }
}
send "exit\r"
expect "#"
set devices [list]
set current_device ""
set lines [split $result "\n"]
foreach line $lines {
    set line [string trim $line]
    if {[llength $line] == 1} {
        set current_device $line
        continue
    }
    set line "$current_device$line\n"
    if {[string match {* R *} $line]} {
        lappend devices $line
    }
    set current_device ""
}

puts $devices
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:58:52+00:00Added an answer on June 14, 2026 at 10:58 pm
    set devices [list]
    set current_device ""
    foreach line [split $result \n] {
        if {[llength [split [string trim $line]]] == 1} {
            set current_device $line
            continue
        }
        set line "$current_device$line"
        if {[string match {* R *} $line]} {
            lappend devices $line
        }
        set current_device ""
    }
    
    # devices list should now contain "joined" routers.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.