Expect script to save configuration from a router


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expect script to save configuration from a router
# 1  
Old 07-08-2015
Expect script to save configuration from a router

Hy guys,

My name is Alex, i am new here and I hope to find some answers. I am trying to run a expect script to telnet to a mikrotik router, run a command (export), and save the output of that commant to a file (outputfile.txt). The problem is that only part of the output is saved to outputfile.txt.

Here is my code:

Code:
#!/usr/bin/expect -f
exp_internal 0
log_user 0
match_max -d 1000000

set timeout 30
spawn telnet 192.168.255.100
expect "Login: "
send "mktbk\r"
expect "Password: "
send "password\r"
expect "> "
send "export\r"
expect eof
puts [open outputfile.txt w] $expect_out(buffer)
expect "> "
send "quit\r"

I guess it's a buffer problem ..... got any ideas?

Thanks,
Alex
# 2  
Old 07-08-2015
Is the saved output (in the file), always the same or does the content vary?
# 3  
Old 07-08-2015
Hy,

I got that figured out, right now I am trying to do a foreach loop, I want this script to read a file that have some hosts in it and save the config for each one:

Here is the code that I am trying:

Code:
#!/usr/bin/tclsh
package require Expect
log_user 0
match_max -d 10000000
set timeout 30

set tdate [clock format [clock seconds] -format %Y%m%d]

#set host 192.168.255.100
set user mktbk\r
set pass password\r


foreach {host} {argv0} {





set name $tdate-$host
spawn telnet $host

expect "Login: "
exp_send $user

expect "Password: "
exp_send $pass

expect "> "
exp_send "export\r"

expect "> "
exp_send "quit\r"
expect eof

set fd [ open $name w ]
puts $fd $expect_out(buffer)
close $fd

}

The error that i get is:

Code:
[alex@samba scripts]$ ./script.sh host.txt 
send: spawn id exp4 not open
    while executing
"exp_send $user"
    ("foreach" body line 11)
    invoked from within
"foreach {host} {argv0} {





set name $tdate-$host     
spawn telnet $host

expect "Login: " 
exp_send $user

expect "Password: "
exp_send $pass

exp..."
    (file "./script.sh" line 14)
[alex@samba scripts]$

Got any ideas?

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Expect script returning string following a found expect.

I'm fairly new to scripting so this might not be possible. I am using Expect with Cisco switches and need to capture the string after finding the expect request. For example, when I issue "show version" on a Nexus switch, I'm looking to capture the current firmware version: #show version ... (0 Replies)
Discussion started by: IBGaryA
0 Replies

2. Shell Programming and Scripting

Expect script brokes when Ping file save

Hi, For some reason I was needed to ping some URL and save the output to a file through Expect script, following is a very basic of that script, but it fails everytime. When I just copied the same 'ping' line to normal Terminal and run, it runs fine. Please, help. #!/bin/sh spawn ping -c 3... (1 Reply)
Discussion started by: santanu4ver
1 Replies

3. Programming

Reading a router configuration file

Hello C specialists, I'm trying to write a program to read out a binary configuration file produced by a router. But the output of Name and Value is cryptic. What's going wrong? The structure of the binary file is very simple: struct nvram_tuple { char *name; char *value; ... (5 Replies)
Discussion started by: digidax
5 Replies

4. IP Networking

How to show Cisco Router Running Configuration in Third Party Application

Hey everyone, I have a few question. 1. Is it possible to display cisco 'show run' output command to the application ?? 2. And is there any ways to log in to the router instead of using telnet from telnet application??? Thanks in advance (0 Replies)
Discussion started by: franzramadhan
0 Replies

5. Shell Programming and Scripting

Expect script help needed- script failing if router unavailable

Hey all. Sometimes I'm tasked to change some router configs for the entire network (over 3,000 Cisco routers). Most of the time its a global config parameter so its done with a loop and an IP list as its the same configuration change for all routers. This is working OK. However, sometimes an... (3 Replies)
Discussion started by: mrkz1974
3 Replies

6. Linux

How to save crontab configuration when using SSH?

Hi, I'm trying to save a crontab configuration using SSH, acessing a Linux machine from a Windows desktop. So, in the prompt I type "crontab -e", to edit crontab, and a window is opened. But after writing the changes, I press Save button but it seems not working, because when... (1 Reply)
Discussion started by: Roger75
1 Replies

7. Shell Programming and Scripting

Expect, save to file and remove before prompt

I have an Expect script which works very well. It logs into my remote routers and runs some commands and then to the next until finished. I need two things, first I need to save the output to a file from where the log_user 1 begins. expect << EOF set timeout 15 #set var "exit " match_max... (1 Reply)
Discussion started by: numele
1 Replies

8. Shell Programming and Scripting

Need help with Expect script for Cisco IPS Sensors, Expect sleep and quoting

This Expect script provides expect with a list of IP addresses to Cisco IPS sensors and commands to configure Cisco IPS sensors. The user, password, IP addresses, prompt regex, etc. have been anonymized. In general this script will log into the sensors and send commands successfully but there are... (1 Reply)
Discussion started by: genewolfe
1 Replies

9. UNIX for Dummies Questions & Answers

how to save the output of command in tcl/expect

hi, everyone: I just wonder how to save the output of command, I mean everything, save as a string into a variable. another question is I try to ls the details of a directory, but it works in the shell, not in the script. for example code: ls -ltr *se100* | grep ^- | tail -1 | awk '... (1 Reply)
Discussion started by: allenxiao7
1 Replies

10. Shell Programming and Scripting

strange expect script behavior, or am i misunderstanding expect scripting?

Hello to all...this is my first post (so please go easy). :) I feel pretty solid at expect scripting, but I'm running into an issue that I'm not able to wrap my head around. I wrote a script that is a little advanced for logging into a remote Linux machine and changing text in a file using sed.... (2 Replies)
Discussion started by: v1k0d3n
2 Replies
Login or Register to Ask a Question