Sponsored Content
Top Forums Shell Programming and Scripting Expect Scripting Loop Argument Desperately Needed! Post 302658057 by dwightlaidler on Monday 18th of June 2012 04:26:03 PM
Old 06-18-2012
Expect Scripting Loop Argument Desperately Needed!

I am trying to create an Expect script that does the following:

1) Telnets to an IP address and logs in with user ID and Password

2) Issue a CLI command to the server that will output data of which I am particularly interested in a DS1 clock 'Slips' value. I want to be able to keep issuing the CLI command..........wait say 5 minutes...then issue the command again...wait 5 minutes....issue it again........and if the clock 'Slips' value is increasing, I want to input another CLI command to change the clock source which prevents the equipment from losing clocking and having to be reboot the server to restore call processing.
3) this script will be run randomly using crontab.

I am obviously using Expect/TCL off a Linux box. My current script is able to Telnet to the server and login successfully, issue the CLI commands and change the clock source accordingly. The crontab also works.
The issue is parsing this SLIPS value and putting this value in a loop to see if it is in fact incrementing over the 3 or 4 CLI command queries and if so, I need to initialize the clock source. I have no clue as to how to make this loop command syntax using Expect scripting.

So again, once the CLI is isssued the first time, there will be a SLIPS value within the output, that needs to be saved, .....5 minutes later....the same command is issued again to see if the SLIPS has increased.......5 minutes later....same command issued again....and if SLIPS has increased 3 times in a row after every inputted CLI command...then I need to issue the CLI command to switch the DS1 clock source.
Again, this script will be ran again using crontab.....likely every hour.

Here is my script contents to date:

#!/usr/bin/expect
spawn telnet 127.16.10.99/r
expect "Connected to 127.16.10.99.\r"
expect "Login:"
send "tester\r"
expect "password:\r"
send "esso123"
expect "Spirent#"
send "st t1 stat sl6di2"
# the above command outputs the following info on terminal

DS1 Stats:


Slot SL2/DI8(PRI)
StatusLayer1 Active
StatusLayer2 Active
FramingErrors 6
PathCodeViolations 0
LineCodeViolations 22439
Slips 2
DchTxFrames 18260
DchTxOctets 72765
DchTxFrameUnderrun 0
DchRxFrames 17816
DchRxOctets 72765
DchRxLenViolations 0
DchRxAbortedFrames 0

#what I need to do is take note of the 'Slips' value - currently is shows value of 2

# so I need to create a loop statement that stores this value and then sleeps for 5 minutes.......then issues the CLI command to the system again and checks Slips value and check if it has increased - if it does - sleep 5 minutes and then issue the CLI command again......if it has increased again.....then I will need to switch the clock source. Note - if the Slips value is not increasing after the CLI command, then i need to say within Loop statement to exit this procedure.

Any help would be greatly appreciated.....searching Google for specific examples pertaining to this scenario has been fruitless. I have the Expect manual by OReilly....and it hasn't been useful to me regarding this looping function needed.

Thank you for taking the time.

Last edited by dwightlaidler; 06-21-2012 at 10:58 AM.. Reason: more data and scripting example included
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help needed desperately......

Hi guys, I want to execute an SQL script based on a condition.... I am writing a sample requirement... num = {some value} if(num == 0) then cat>> test.sql <<FIN select * from table_a FIN else cat>> test.sql << FIN select * from table_b FIN fi This is what i... (3 Replies)
Discussion started by: Payal Sharma
3 Replies

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

3. Shell Programming and Scripting

Help needed in expect scripting.

Hi, I am new to expect scripting and need your help in the following task. I want an expect script to take the commands from an input file and then execute the commands and give the command execution output in an otput file. The command execution part is normal and I did it via script by... (2 Replies)
Discussion started by: Sooryaa
2 Replies

4. Shell Programming and Scripting

Help needed desperately

Hello Folks, i have written a script to nullify the log files in my dev environment but the log files are not getting nullified.Please find below my script and also thank you all in advance for your help. find /app -size +100000 -xdev -exec du -k {} \; 2>/dev/null | sort -nr |ls -1dF ... (2 Replies)
Discussion started by: Rayzone
2 Replies

5. Shell Programming and Scripting

help needed with script that gets a filename as argument (with an if/else statement)

Hi, I am trying to create a script just to study BASH scripting, but I have some problems. I need to create a script that gets a filename as an argument. The script should behave as follows: • If the given file name already exists, inform the user and quit. • If the given file name does... (2 Replies)
Discussion started by: I-1
2 Replies

6. Shell Programming and Scripting

Help needed with expect...(If telnet fails)

Hello I'm working using expect to automate telnet commands...i want to check if telnet fails and in this case...certain message should appear or certain word should be out in a file). Thses are the messages that apeear if telnet fails: Trying 172.16.3.115... telnet: Unable to connect... (0 Replies)
Discussion started by: Hossam_Nox
0 Replies

7. Shell Programming and Scripting

Expect - Comparison of expect value and loop selection

Hello All, I am trying to automate an installation process using expect and sh script. My problem is that during the installation process the expected value can change according to the situation. For Example if this is a first time installation then at step 3 I'll get "Do you want to accept... (0 Replies)
Discussion started by: alokrm
0 Replies

8. UNIX for Dummies Questions & Answers

help with shell scripting argument

#!/bin/bash echo "enter a file or directory name" read name if then echo " argument is file " ls -l $name | awk '{print $1,}' elif echo " argument is a directory" ls -l $name | awk '{print $1}' fi what i am trying to do. get input... (3 Replies)
Discussion started by: iluvsushi
3 Replies

9. UNIX for Dummies Questions & Answers

UNIX advice needed desperately :/

Firstly I do need to state that I am turning here as a last resort. Im in my first year of a Computer Networking and technical support course, and as a first year I must complete a Introductory Unix/Linux class. I have been doing well in the course so far however I have been stumped on the final... (1 Reply)
Discussion started by: setaylor5
1 Replies

10. Shell Programming and Scripting

Expect help needed .. can i do this ?

hi all, i need to run expect and get the output into variables so my bash script can make decisions this is what i get currently Please wait while gathering statistics... 1: 0x60bf83c8 2: 0x60bf8d58 3: 0x60bf8d80 * 4: 0x60bf93a0 * 5: 0x62b0b4e0 6: 0x605938a0 7:... (13 Replies)
Discussion started by: ExpectHelpNeede
13 Replies
Net::CLI::Interact::Manual::Tutorial(3pm)		User Contributed Perl Documentation		 Net::CLI::Interact::Manual::Tutorial(3pm)

NAME
Net::CLI::Interact::Manual::Tutorial - Guide for new users Introduction Automating command line interface (CLI) interactions is not a new idea, but can be tricky to implement. Net::CLI::Interact aims to provide a simple and manageable interface to CLI interactions, supporting: o SSH, Telnet and Serial-Line connections o Unix and Windows support o Reusable device command phrasebooks The module exists to support developers of applications and libraries which must interact with a command line interface. The SYNOPSIS section of Net::CLI::Interact has an overview of the commands demonstrated in this document. Getting Started Like many other Perl modules, you need to load the library and then create a new Net::CLI::Interact instance (which is $s in the example, below): use Net::CLI::Interact; my $s = Net::CLI::Interact->new({ transport => 'Serial', personality => 'cisco', }); Your application can have multiple independent instances (that is, connect to different devices at the same time); simply repeat the above example more times for variables other than $s. Note that at the time you create the instance, as in the example above, the library does not connect to the device. That comes later. There were two options provided to the "new" call, above, both of which are required for all new instances. Let's look at them in turn: transport How do you want to connect to your CLI? The current choices are Telnet, SSH and a Serial line (that is, a console cable). In this option you need to tell the library which underlying transport is to be used. Some of the transports have additional options that are either required, or optional. For example, the Telnet and SSH transports both need to know which post name or IP address should be contacted. You pass this in another option to "new", like so: my $s = Net::CLI::Interact->new({ transport => 'Telnet', connect_options => { host => 'my.server.example.com' }, personality => 'cisco', }); See the manual page of the transport module for the option details. personality What language does the connected device speak? For instance one common format is Cisco's IOS, which is widely cloned on other vendor equipment CLIs. In this option you need to pass the name of a personality that's used to load a Phrasebook. A phrasebook is simply a library, or dictionary, of pre-configured phrases you can use on the CLI. This makes life simple, because Net::CLI::Interact then can automate some of the more difficult tasks. For example, if you issue a command and the output is "paged" so you hit Space or Return to see the next page, the phrasebook can tell Net::CLI::Interact how to slurp all these pages into one body of output before returning it to you. See the Phrasebook user guide for a list of the possible values for the "personality" option. Connecting This is done automatically for you the first time you send a command to the device, so skip this step and move on! Sending Commands But first, Prompts The idea of sending a command is, usually, to see some output. The most important part of this process is knowing when the output has all been sent, otherwise the library would sit forever, waiting to gather more text! Between each command sent, the connected device prints a CLI Prompt. This prompt is where you type commands, and it's what tells us that all the output has been sent from our last command. Prompts are loaded in the phrasebook, and given friendly names. If your personality's phrasebook is sufficiently mature, then the prompts might be fully automated, and just like the Connecting step above, you can skip doing anything manually. Consult the Phrasebook user guide for details. However if you need to set it manually, do the following: $s->set_prompt('friendly_name'); Sometimes you might not know what state the CLI is in; typically this applies to Serial lines. In that case you can ask to find the matching prompt: $s->find_prompt('wake_up'); The option "wake_up" asks to "hit the Return key", to make the device spit out its prompt. That is only done if needed, and you can remove "wake_up" to skip this step. Literal Commands There's not a lot to it. Remember that with a mature personality loaded, you were probably able to skip the previous prompt step and go straight to: my $output = $s->cmd('show ip interfaces brief'); Here you will get all the output from the command together in one variable, $output. If you prefer an array where each item is one line of output, simply use @output instead in the above example. Macros Life gets more complicated when your command has things like confirmation steps (e.g. reboot), other prompts (e.g. extended ping), etc. For these situations we have Macros in the phrasebook. A macro is simply a sequence of commands we could issue using "$s->cmd()", bundled together and given a friendly name. Macros are also smart enough either to handle simple confirmation steps themselves, or to allow you to pass in parameters. Some examples probably help: # saves config, accepting the default "startup-config" when prompted $s->macro('write_mem'); # logs in, passing a username and password at the prompts $s->macro('to_user_exec', { params => ['my_username', 'my_password'], }); # simply a parameterized command $s->macro('show_interfaces_x', { params => ['GigabitEthernet 3/4'], }); Slurping Output As mentioned above, output at the CLI is often "paged" with the user hitting Space or Return to show the next page. Most macros can deal with this automatically if well implemented. If the Phrasebook user guide says your personality has a named default Continuation for handling paged output, then set it like so: $s->set_default_continuation('friendly_name'); Disconnecting This is nothing more fancy than issuing the appropriate CLI commands to close the network connection. In the case of the Serial line transport you can usually only log out, and not fully disconnect. Simply end your application and the library will tidy things up as best it can. perl v5.14.2 2012-06-12 Net::CLI::Interact::Manual::Tutorial(3pm)
All times are GMT -4. The time now is 12:46 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy