Bssh running Expect


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bssh running Expect
# 1  
Old 04-08-2010
Bssh running Expect

I have a problem with my expect script which I run under a bash script. What it does is take a list of routers and telnets to them and run some commands then to the next router on the list. It works, but the issue I have is if my commands "$cmds" receive a lot of return data, then it gets cut off. I messed around with the timeout but would always cut off data.
Code:
expect << EOF
set timeout 60
log_user 0
spawn telnet ${router}
expect "login: "
send "testusername\r"
expect "Password: "
send "testpass\r"
expect "*#"
send "term len 0\r"
expect "*#"
log_user 1
send "$cmds"  
expect "*$router#"
send "exit\r"
EOF

# 2  
Old 04-08-2010
Can you use SSH instead of telnet? That's far more predictable and easily scripted. It also supports normal error reporting over stderr, while telnet can't differentiate. Using keys you can arrange noninteractive remote logins without the gaping security hole of keeping a plaintext password inside your script, too. So all that expect thrashing would boil down to

Code:
ssh user@site "$commands" > outputfile 2> errlog

or even

Code:
ssh user@site /bin/bash < scriptfile > outputfile 2>errlog

# 3  
Old 04-08-2010
Thanks Corona688, I can ssh to these routers but cannot use keys. So, I still will need the expect Password. Do you have a suggestion on how I would implement that with the ssh statement you provided?
# 4  
Old 04-08-2010
ssh is designed not to accept passwords in that manner. plaintext passwords in script files are security holes big enough to drop a cow through -- and wholly unnecessary when keys can do the same thing securely. Why can't you use keys? All it means is generating one, then adding a line to ~/.ssh/authorized_keys
# 5  
Old 04-08-2010
I think what I need is for each line in my $cmds list, to wait for my expect prompt before the next line is input. Then each line will need to complete before moving to the next line. I read about the while statement but not sure how to implement.

Another theory is that because the speed between server and router the expect keyword is received and exited before the terminal prints the entire output.
# 6  
Old 04-08-2010
Why can't you use keys? You should be able to do so if you can login with ssh. It would allow you to write normal shell scripts for your servers instead of expect scripts that break down all the time and nobody understands. It'd also mean you'd no longer need to embed insecure plaintext passwords inside your script files.
# 7  
Old 04-09-2010
I'm checking with the router manufacture about ssh keys. In the mean time, I tried a coupe of things on my expect script, both failed, trying to expect a prompt after each line in the cmds variable.
Code:
foreach element $cmds {
    send $element
    expect "*#"
}

Code:
for {set i [expr [llength $cmds]] {$i>=0} {incr i} {
    send "$cmds($i)\r"
    expect "*#"
}



---------- Post updated 04-09-10 at 11:57 AM ---------- Previous update was 04-08-10 at 10:09 PM ----------

I got it working pretty good. I put a unique string at the end of my cmds variable and changed the timeout to 240 right before when the commands are sent.
Code:
expect << EOF
set timeout 60
set var "exit    "
log_user 0
spawn telnet ${router}
expect "login: "
send "testusername\r"
expect "Password: "
send "testpass\r"
expect "*#"
send "term len 0\r"
expect "*#"
set timeout 240
log_user 1
send "$cmds"  
expect $var
send "exit\r"
EOF

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 not running from cron. Full Paths used

My cron file. Copied $PATH # Minute Hour Day of Month Month Day of Week Command SHELL=/bin/ksh PATH=/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/perl/lib:/perl/lib/lib/perl5:/perl/scripts:.:/perl/lib/local:/home/popeye:/temp 0... (3 Replies)
Discussion started by: popeye
3 Replies

3. Shell Programming and Scripting

expect script not running in cron "Urgent"

I wrote a script to download the files from sftp location to local. while running the script manually its working fine. when i schedule the same in cron its not working.... :wall::wall: here is the script: #!/bin/bash ... (2 Replies)
Discussion started by: ganga39
2 Replies

4. Shell Programming and Scripting

Passing username and password to a script running inside "expect" script

Hi I'm trying to run a script " abc.sh" which triggers "use.sh" . abc.sh is nothing but a "expect" script which provides username and password automatically to the use.sh script. Please find below the scripts: #abc.sh #!/usr/bin/expect -f exec /root/use.sh expect "*name*" send... (1 Reply)
Discussion started by: baddykam
1 Replies

5. Shell Programming and Scripting

Expect script cronjob running but dying prematurely

I have an Ubuntu machine that I'd like to update automatically. I've written an expect script to run the aptitude package manager and update my packages. Essentially it does: aptitude update && aptitude upgrade while answering "yes" at the appropriate time. It works quite nicely when run... (4 Replies)
Discussion started by: CluelessPerson
4 Replies

6. Shell Programming and Scripting

how to debug an expect script running on a tomcat server

Hello everyone, I have a small expect script as follows; #!/usr/bin/expect -f set force_conservative 0 ;# set to 1 to force conservative mode even if ;# script wasn't run conservatively originally if {$force_conservative} { set send_slow {1 .1} proc send {ignore arg}... (1 Reply)
Discussion started by: randeel
1 Replies

7. Shell Programming and Scripting

How to write an expect script to check if a process is running?

I'm new to expecting and i want to create a script to ssh to a device,check is a process is running and display that the process is running or not.This is what i have so far After executing this script i get an error. #!/usr/bin/expect set timeout -1 set ip "machine ip goes here" set... (5 Replies)
Discussion started by: icchi
5 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. Shell Programming and Scripting

Is there a way to ask expect wait for sometime before running the next send command ?

Hi all, After expect catches the string I specify, is there a way to ask expect wait for sometime before running the next send command ? So my script looks like following, expect "some string" #How to ask expect to wait for a while send "next command" The reason I want to do this is... (0 Replies)
Discussion started by: qiulang
0 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