Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Expect Scripting

Status
Not open for further replies.

scorney

Technical User
Sep 15, 2003
115
CA
Hello,
First of all I want to say I'm starting to write these kind of script, so really newbie level.
I'm trying to create a script which will telnet into many different boxes (linux & Unix based)and from these device telnet into a sub-component, then run a simple command capturing partially the output and collect it into a text file.
But I've just tried the simple telnet without files which will provide the IP and it stopped at the password:


Here is the script:

WMSCANLAB:/usr/local/bin <114> (root) % vi login.expect
"login.expect" 8 lines, 174 characters
#!/usr/local/bin/expect ^M
spawn telnet 125.121.239.90 ^M
expect "BUSY BOX login:"
send "nodea\n"
expect "Password: "
send "nodeblny\n"
send "cd /pltf\n" ^M
send "cd /cpri\n" ^M


Here is the results:

WMSCANLAB:/usr/local/bin <113> (root) % expect -f login.expect
spawn telnet 125.121.239.90
Trying 125.121.239.90...
Connected to 125.121.239.90.
Escape character is '^]'.

BUSY BOX login: nodea
Password: WMSCANLAB:/usr/local/bin <114> (root) %

It doesn't go further, what is wrong?
Do i have to have Expect in each single box or just whereI initiate the script?

Once I fix this, how can I make the IP as a variable which it will take the info from a text file?

Thanks,
Sylvain
 
you probably need to expect the prompt and then send your command

one question though, do you have to use telnet for this? it'd be much easier to accomplish with ssh if the servers support them as you can run non-interactive sessions through ssh and with keys.. no need for password prompting either.

add something like pdsh on top of that and you've got a great deal of flexibility.
 
Hi exsnafu!
nope I just tried and these devices are not ssh capable, only my main server..
 
autoexpect is a very handy tool provided with expect for generating scripts; it records a sample session and saves it as an expect script which you can then edit (usually involves removing lots of text apart from the prompts) and use on other systems.

Annihilannic.
 
Thanks autoexpect works perfectly for learning.
I've created my script and it work fine.

Now I want to improve it by adding variables and get the variable from a text file.

So far this part was really easy with autoexpect:
send -- "telnet 135.121.239.90\r"
expect -exact "telnet 135.121.239.90\r
Trying 135.121.239.90...\r
Connected to 135.121.239.90.\r
Escape character is '^\]'.\r
\r\r
BUSY BOX login: "

But I want to have now:
send -- "telnet $IP\r"
but how do I match or refer the $IP which will be contained in a text file under the same directory.
ie txt file:
135.121.239.90
135.121.239.91
135.121.239.104
135.121.239.180

The IP address will not increment necessary it will be a list that I would have to populate based on need.

and how to collect the output of a command initiate from these devices to a text file?

Then from this File (output from the initiated command) should I use awk command to collect only a string of 10 digit after the same parameter of the output.

ie.
06-05 11:37:27Z 0x3163ac0 tShell tipReadCpmTxPowerMeters: \r
06-05 11:37:27Z 0x3163ac0 tShell Meter Conversion factor: 0.0000000030\r
06-05 11:37:27Z 0x3163ac0 tShell txId: 0 Meter reading: 2060320, 0x 1f7020 \r
06-05 11:37:27Z 0x3163ac0 tShell Carrier Power -22.1182 dBFS\r
06-05 11:37:27Z 0x3163ac0 tShell txId: 1 Meter reading: 0, 0x 0 \r
06-05 11:37:27Z 0x3163ac0 tShell Carrier Power -Inf dBFS\r
06-05 11:37:27Z 0x3163ac0 tShell txId: 2 Meter reading: 0, 0x 0 \r
06-05 11:37:27Z 0x3163ac0 tShell Carrier Power -Inf dBFS\r
06-05 11:37:29Z 0x3163ac0 tShell gTxDesign: 5800 1/100ths dB\r
value = 51785584 = 0x3162f70\r

I need only the value for Carrier Power -22.1182 dBFS.
the -22.1182 is changing and this is the value I need.
you can see 3 times Carrier power but it's the only one with the - with value which is radiating power. this is the one I need.

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top