Sponsored Content
Top Forums Shell Programming and Scripting Need help with Expect script for Cisco IPS Sensors, Expect sleep and quoting Post 302344838 by genewolfe on Monday 17th of August 2009 07:08:07 PM
Old 08-17-2009
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 some problems I haven't been able to solve.

Delays in prompts, Sleep timers:
1. Sometimes the script appears to send the next command before the next prompt appears in which case the correct command is skipped and the one afterwards sent and accepted. The commands usually need to be entered in order so this usually produces an error. For example Expect sends "service signature-definition sig0" and the sensor receives it but the sensor takes 5-10 seconds to provide the next prompt. The possibility of this long delay which will cause an error has forced me to insert a sleep timer after each command is sent in my loop. One problem with that is if the delay is even longer than my sleep timer I still get an error. Another problem is the script takes forever because there's a 10 second pause after every command. Is using a sleep timer the only or best solution to this problem?

Quoting, Escaping \. in Regex
2. From what I can tell in Expect if I don't quote the command "configure terminal" it's received at the IPS sensor as 2 separate commands so I have to quote it. I don't know if this is causing this problem but for some reason when I use this script the regex string 10\.1\.1\.100 ends up being sent to the IPS sensor without the backslashes 10.1.1.100 but I don't want the regular expression dot . evaluated I just want an IP address with periods i.e. with the periods escaped with the backslash. Something about my Expect script is causing this problem because when I log into an IPS sensor using ssh and copy and paste the commands into the IPS sensors manually the regex string is saved with the backslashes. I've tried removing the quotes and I've also tried double quoting and neither work. Does anyone know what I'm doing wrong here or what I could try to use Expect to send a regex string with backslashes intact?

Code:
#!/usr/bin/expect

log_user 1

set timeout 10

set user "user"

set password "password"

set ip_list {
	10.1.1.10
	10.1.2.10
	10.1.3.10
	10.1.4.10
	}

set cmd_list {
	"configure terminal"
    "service signature-definition sig0"
	"signatures 60000 0"
	"alert-severity high"
	"sig-fidelity-rating 100"
	"sig-description"
	"sig-name Foo"
	"sig-string-info Foo"
	"no sig-comment"
	"exit"
	"engine string-tcp"
	"regex-string 10\.1\.1\.100"
	"service-ports 0-65535"
	"direction from-service"
	"exit"
	"exit"
	"exit"
    }

foreach ip $ip_list {
    if {$ip != ""} {

        # Connect
        spawn ssh $user@$ip

		expect "?assword*"
		send "$password\r"

        set prompt "(IPS....#)"

        foreach command $cmd_list {
            if {$command != ""} {
                expect -re $prompt {
			send "$command\r"
			sleep 5
                }
            }
        }

		expect -re "Apply Changes" {
      		send -- "yes\r"
		}

        expect -re $prompt {
            send -- "exit\r"
        }

        close
    }
}

send_user "\nDone.\n"

 

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

question about "sleep" command in expect script

I wrote some expect script to telnet to some device to execute some commands.Firstly,I can't get full result some time,then I try to add some "sleep" command in it.Fortunately it works. My idea about it is that it uses sleep command to wait the result to be displayed.Am I right or correct the... (4 Replies)
Discussion started by: robbiezr
4 Replies

3. Shell Programming and Scripting

Expect/telnet/testing tacacs on a cisco

At times I find the need to test that the tacacs port 49 is open. The code below works but is painfully slow because I have to wait on the timeouts. Examples of possible responds router1#telnet 10.11.20.14 49 Trying 206.112.204.140, 49 ... Open route1#telnet 10.11.19.14 49 Trying... (1 Reply)
Discussion started by: popeye
1 Replies

4. Shell Programming and Scripting

Alternate to SLEEP for EXPECT within BASH script?

Fairly new to the System Admin world, and this is my first post here, hoping to get some clarification. I am using a BASH script to automate some Logfile Archiving (into .tars). The actual logfiles are accessed through an SSH, so I have used the following EXPECT sub-script within my main BASH... (8 Replies)
Discussion started by: Goatfarmer03
8 Replies

5. Shell Programming and Scripting

Bash script failed with expect on cisco routers

Hi all, I use a bash script which use expect to connect throught ssh and run command on a cisco router. The ssh connection with expect work fine, but the first command on the cisco router failed, I try to run the command in error by hand and it work fine... :( the first part of the script... (2 Replies)
Discussion started by: bedomon
2 Replies

6. Programming

Calling expect script inside another expect

Hi, Am very new to expect scripting.. Can You please suggest me how to call an expect script inside another expect script.. I tried with spawn /usr/bin/ksh send "expect main.exp\r" expect $root_prompt and spawn /usr/bin/ksh send "main.exp\r" expect $root_prompt Both... (1 Reply)
Discussion started by: Priya Amaresh
1 Replies

7. Shell Programming and Scripting

Expect script to show cisco configs

I know there are better ways to do this. I prefer snmp. I do not have the proper perl modules loaded on the platorm. Snmp isnt loaded on the platform. Telnet is not an option. I need to write an expect script to pull cisco equipment configs. The following code is executed once I gain... (0 Replies)
Discussion started by: popeye
0 Replies

8. Programming

Calling another expect script inside an expect script

I have an expect script called remote that I want to call from inside my expect script called sudoers.push, here is the code that is causing me issues: set REMOTE "/root/scripts/remote" ... log_user 1 send_user "Executing remote script as $user...\n" send_user "Command to execute is: $REMOTE... (1 Reply)
Discussion started by: brettski
1 Replies

9. 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

10. Shell Programming and Scripting

Cisco, 2 ssh logins for expect /bash

HI all i need to connect to about 900 cisco routers and switch to do some configs changes. the issue i am having is that half the devices have one set of username and password and the other half have another username and password. From expect or bash script i can ssh into a device and make... (0 Replies)
Discussion started by: quintin
0 Replies
DISLOCATE(1)                                                  General Commands Manual                                                 DISLOCATE(1)

NAME
Dislocate - disconnect and reconnect processes SYNOPSIS
dislocate [ program args... ] INTRODUCTION
Dislocate allows processes to be disconnected and reconnected to the terminal. Possible uses: o You can disconnect a process from a terminal at work and reconnect from home, to continue working. o After having your line be dropped due to noise, you can get back to your process without having to restart it from scratch. o If you have a problem that you would like to show someone, you can set up the scenario at your own terminal, disconnect, walk down the hall, and reconnect on another terminal. o If you are in the middle of a great game (or whatever) that does not allow you to save, and someone else kicks you off the ter- minal, you can disconnect, and reconnect later. USAGE
When run with no arguments, Dislocate tells you about your disconnected processes and lets you reconnect to one. Otherwise, Dislocate runs the named program along with any arguments. By default, ^] is an escape that lets you talk to Dislocate itself. At that point, you can disconnect (by pressing ^D) or suspend Dislo- cate (by pressing ^Z). Any Tcl or Expect command is also acceptable at this point. For example, to insert the contents of a the file /etc/motd as if you had typed it, say: send -i $out [exec cat /etc/motd] To send the numbers 1 to 100 in response to the prompt "next #", say: for {set i 0} {$i<100} {incr i} { expect -i $in "next #" send -i $out "$i " } Scripts can also be prepared and sourced in so that you don't have to type them on the spot. Dislocate is actually just a simple Expect script. Feel free to make it do what you want it to do or just use Expect directly, without going through Dislocate. Dislocate understands a few special arguments. These should appear before any program name. Each should be sep- arated by whitespace. If the arguments themselves takes arguments, these should also be separated by whitespace. The -escape flag sets the escape to whatever follows. The default escape is ^]. CAVEATS
This program was written by the author as an exercise to show that communicating with disconnected processes is easy. There are many fea- tures that could be added, but that is not the intent of this program. SEE ALSO
Tcl(3), libexpect(3) "Exploring Expect: A Tcl-Based Toolkit for Automating Interactive Programs" by Don Libes, O'Reilly and Associates, January 1995. AUTHOR
Don Libes, National Institute of Standards and Technology 7 October 1993 DISLOCATE(1)
All times are GMT -4. The time now is 08:36 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy