Expect Script Error Handling


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expect Script Error Handling
# 1  
Old 02-14-2012
Expect Script Error Handling

Good Day Everyone,

I was hoping to get a little insight into an expect script that I've written.

Basically we have this expect script to perform an sftp upload, key authentication is not an option, and sftp is the only method supported by our vendor, thus the need for this.

I want to be able to catch timeouts and log an error code for scripting purposes.

I need to:
  • Log into sftp site
  • Begin Transfer
  • Detect that upload has hit 100%
  • If upload does not hit 100% within a certain time, I want to log an error code for scripting purposes.

Here is what I have so far:

Code:
  set timeout 10
  spawn sftp user@xxx.xxx.xxx.xxx
  match_max 100000
  expect "password: "
  send -- "PASSWORD\n"
  expect "sftp> "
  send -- "lcd /data/upload\n"
  expect "sftp> "
  send -- "put file_yyyymmdd\n"
  expect "*100%*" {send "quit\n" } \
  timeout { exit 2 }
  expect "sftp> "
  send -- "quit\n"
  expect eof

I believe the code is working up to the timeout, below is the log from the expect session:

Code:
  spawn sftp user@xxx.xxx.xxx.xxx
  user@xxx.xxx.xxx.xxx's password: 
  Connected to xxx.xxx.xxx.xxx.
  sftp> lcd /data/upload/
  sftp> put file_yyyymmdd
  Uploading file_yyyymmdd to /home/user/file_yyyymmdd
  file_yyyymmdd                                 0%    0     0.0KB/s   --:-- ETA
  file_yyyymmdd                               100% 5835     5.7KB/s   00:00    
  quit
  sftp> quit

It appears the "quit" is sent twice? Which I guess makes sense b/c it sees the 100% and immediately sends the "quit" before the prompt is ready.

What changes should I make to get my expected results?

Any help would be much appreciated.


On another note, I'd like to expand this expect script, or create another that detects when the DES key has changed, accept it (send yes), and continue.

However I would like to know when this happens, either with an error code, or writing something to a log file...

I've tried the following but it is not working as expected:

Code:
set timeout 10
spawn sftp user@xxx.xxx.xxx.xxx
match_max 100000
expect "Are you sure you want to continue connecting (yes/no)? " {send "yes\r" } \\
timeout { exit 0 }
sleep 2
exit 3
expect eof

The thought was to use the exit codes to determine if the key had to be updated, however it always comes back as exit code 3 (key changed) and didn't send a "yes" when the key actually did change.


Again any help would be greatly appreciated.

Thanks,
# 2  
Old 02-14-2012
If you used sftp with keys, you wouldn't need to install or use a third-party brute-forcing utility to forcefeed plaintext passwords into ssh in an insecure manner for you. "interactive password authentication" means "password typed by a human being in realtime authentication" and nothing else will do. It's so difficult for a reason -- ssh is designed to prevent it, because it's almost impossible to keep unencoded passwords secure, and because allowing them would be just the avenue hackers need to bruteforce your own system or use your system as a platform to bruteforce others.

Google 'passwordless ssh' to find the proper way to do this with keys, there's hundreds of tutorials all over the internet.
# 3  
Old 02-14-2012
I realize this, and do this with some of our other scripts, however as I mentioned this sftp site is hosted by a third party and ssh keys are not an option.


Quote:
Originally Posted by Corona688
If you used sftp with keys, you wouldn't need to install or use a third-party brute-forcing utility to forcefeed plaintext passwords into ssh in an insecure manner for you. "interactive password authentication" means "password typed by a human being in realtime authentication" and nothing else will do.

Google 'passwordless ssh' to find the proper way to do this with keys, there's hundreds of tutorials all over the internet.
# 4  
Old 02-17-2012
I'm not even questioning how to send the password, log in, etc... My question is about error handling with expect. The other parts of the script are already working.

I'm interested in expects ability to see a prompt and send certain text back as a response, and when a prompt is received that is different than what is expected how to handle that.


Quote:
Originally Posted by Corona688
If you used sftp with keys, you wouldn't need to install or use a third-party brute-forcing utility to forcefeed plaintext passwords into ssh in an insecure manner for you. "interactive password authentication" means "password typed by a human being in realtime authentication" and nothing else will do. It's so difficult for a reason -- ssh is designed to prevent it, because it's almost impossible to keep unencoded passwords secure, and because allowing them would be just the avenue hackers need to bruteforce your own system or use your system as a platform to bruteforce others.

Google 'passwordless ssh' to find the proper way to do this with keys, there's hundreds of tutorials all over the internet.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

EXPECT Bash Script Error Control

Hello Geeks once more, Thanks for all the help you have been rendering.. I have a script that depends on the output of an expect statement but sometimes the main script misbehaves which I believe is a result of SSH communication error, how can I apply an error control to know whether the... (2 Replies)
Discussion started by: infinitydon
2 Replies

2. Shell Programming and Scripting

Script FTP maintain error handling

Hi, I have ftp script like below How to insert an error handling, If the transfer failed then send mail to me. Actually, I just need the script to send an email if the FTP failed. How to put the email script within FTP script? Thank You Edy (5 Replies)
Discussion started by: edydsuranta
5 Replies

3. Shell Programming and Scripting

error situation handling when starting app from a script

Hi, in my bash script I start 3rd party app that runs quickly normally and returns some data that I use later for processing. However if there are problems the app doesn't return anything and just hangs... then my script also hangs of course, awaiting for the app to comeback. Was wondering how to... (3 Replies)
Discussion started by: TomSu
3 Replies

4. Shell Programming and Scripting

Help me add Error Handling to my script

Hi all, I have some sections of a script that I am trying to add error handling to. Basically if it returns any error, just continue. This is for a deployment script that i am writing to assist in the deployment of scripts out to other systems. At the top of my KSH script i added this... (5 Replies)
Discussion started by: nitrobass24
5 Replies

5. Shell Programming and Scripting

Help with Error Handling on Script

Hi, I need your guys help again. I run a script which check for some process status in a loop. when i check the process some of the process could throw an error, how can i check that inside my script. Thanks, RR (3 Replies)
Discussion started by: rrb2009
3 Replies

6. Shell Programming and Scripting

Handling SQL prompt through Perl expect module

Hi All, I have a doubt whether expect module in perl will work in SQL prompt or its applicable only for shell prompt ? Thanks, Arun V (2 Replies)
Discussion started by: arun_maffy
2 Replies

7. Shell Programming and Scripting

Issue with Error handling,not able to continue the script further

Hi, I am trying to write a script to cleanup files in a log directory .. cd log find Datk** -mtime +7 -exec rm -f {} \; 2> /dev/null Have used the above to clean up files in log directory more then 7 days older. The file can be something like ( auto-generate by some processes and... (2 Replies)
Discussion started by: nss280
2 Replies

8. Shell Programming and Scripting

Expect Script Error invalid spawn id (6)

Hi everybody, We are trying to connect to a server and to a router using an .expect script that automatically authenticate the username and password needed. The error is that we cannot connect to the server using ssh AND we cannot connect to the router using either telnet or go commands from the... (1 Reply)
Discussion started by: omoyne
1 Replies

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

10. Shell Programming and Scripting

Expect Script - error using sed -

Guys, I'm testing a simple script that connects to a server, does a few checks and sends the results to a file. code: #!/bin/ksh expect <<-EOF | sed 's/^M//' >> file spawn ssh user@server expect "password:" { send "password \n" } expect "$" send "df -k /dev/md/dsk/d10 |... (4 Replies)
Discussion started by: tony3101
4 Replies
Login or Register to Ask a Question