Expect Script Error Handling
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have
Expect Script Examples
Meta Discuss the workings and policies of this site About Us bash script error handling Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us
Shell Script Error Handling
Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and powershell script error handling other Un*x-like operating systems. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top error handling in expect? up vote 4 down vote favorite I am just improving my question because I have achieved so far batch script error handling up to this: set username [lindex $argv 0] set password [lindex $argv 1] set hostname [lindex $argv 2] if {[llength $argv] == 0} { send_user "Usage: scriptname username \'password\' hostname\n" exit 1 } send_user "\n#####\n# $hostname\n#####\n" spawn ssh -q -o StrictHostKeyChecking=no $username@$hostname expect { timeout { send_user "\nFailed to get password prompt\n"; exit 1 } eof { send_user "\nSSH failure for $hostname\n"; exit 1 } "Password: " } send "$password\r" expect { timeout { send_user "\nLogin failed. Password incorrect.\n"; exit 1} "{severname:mike} " } send "ls -lrt\n" expect { "{severname:mike} " { send "uname\n" } } expect { "{severname:mike} " } send "exit\r" close I hope I am correct but how do I get the out put of commands logged locally errlogs and success logs where command is a list of commands like ls, ls -lrt moreover when I put this : spawn ssh -q -o StrictHostKeyChecking=no $username@$hostname {ls -lrt;df -h} it logs in and executes ls -lrt;df -h but after that threw errror error with debug option. The connection closes perhaps
O'Reilly Media, Inc. Exploring Expect SPECIAL OFFER: Upgrade this ebook with O’Reilly A Note Regarding Supplemental Files Preface Expect—Why another tool? Tcl—A Little History Acknowledgments We’d Like to
Linux Script Error Handling
Hear From You How To Read This Book Notational Conventions Exercises 1. unix script error handling Intro—What Is Expect? Ouch, Those Programs Are Painful! A Very Brief Overview A First Script—dialback Total Automation Differing Behavior
Sql Script Error Handling
When Running Non-Interactively Partial Automation Dangerous, Unfriendly, Or Otherwise Unlikable User Interfaces Graphical Applications A Little More About Tcl Job Control Background Processes Using Expect With Other Programs Using Expect On http://unix.stackexchange.com/questions/66520/error-handling-in-expect UNIX Using Expect On Other Operating Systems Using Expect In Real Applications Using Expect In Commercial Applications—Legalese Obtaining Expect and the Examples Expect And Tcl Resources Exercises 2. Tcl—Introduction And Overview Everything Is A String Quoting Conventions Expressions Braces—Deferring Evaluation Control Structures More On Expressions Lists More Ways To Manipulate Strings Arrays Indirect References Handling Errors Evaluating Lists As Commands Passing By Reference https://www.safaribooksonline.com/library/view/exploring-expect/9781565920903/ch04s04.html Working With Files File I/O Executing UNIX Commands Environment Variables Handling Unknown Commands Libraries Is There More To Tcl? Exercises 3. Getting Started With Expect The send Command The expect Command Anchoring What Happens When Input Does Not Match Pattern-Action Pairs Example—Timed Reads In The Shell The spawn Command The interact Command Example—Anonymous ftp Exercises 4. Glob Patterns And Other Basics The * Wildcard More Glob Patterns Backslashes Handling Timeout Handling End Of File (eof) Hints On The spawn Command Back To Eof The close Command Programs That Ignore Eof The wait Command Exercises 5. Regular Expressions Regular Expressions—A Quick Start Identifying Regular Expressions And Glob Patterns Using Parentheses To Override Precedence Using Parentheses For Feedback More On The timed–read Script Pattern Matching Strategy Nested Parentheses Always Count Parentheses Even Inside Of Alternatives Example—The Return Value From A Remote Shell Matching Customized Prompts Example—A Smart Remote Login Script What Else Gets Stored In expect_out More On Anchoring Exercises 6. Patterns, Actions, And Limits Matching Anything But Really Complex Patterns Really Simple Patterns Matching One Line And Only One Line Tcl’s string match Command Tcl’s regexp
Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company http://serverfault.com/questions/364221/expect-script-timeouts-and-error-checking Business Learn more about hiring developers or posting ads with us Server Fault Questions Tags Users Badges Unanswered Ask Question _ Server Fault is a question and answer site for system and network administrators. Join them; it only http://www.linuxforums.org/forum/programming-scripting/191102-exit-code-handling-expect-running-bash.html takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Expect Script, timeouts and error checking up vote 0 down script error vote favorite 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 script error handling 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: 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: 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
Today's Posts Advanced Search Find the answer to your Linux question: Entire Site Articles Downloads Forums Linux Hosting Forum GNU Linux Zone Programming / Scripting Exit Code Handling in Expect Running from Bash If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. ** If you are logged in, most ads will not be displayed. ** Linuxforums now supports the Tapatalk app for your mobile device. Results 1 to 3 of 3 Thread: Exit Code Handling in Expect Running from Bash Thread Tools Show Printable Version Email this Page… Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode Enjoy an ad free experience by logging in. Not a member yet? Register. 08-16-2012 #1 Evilmachine View Profile View Forum Posts View Articles Just Joined! Join Date Aug 2012 Posts 1 Exit Code Handling in Expect Running from Bash Hi there, i am sitting on a very Big Expect Script and got some problems now with calling the Expect from within Bash and getting Errror and Exit Codes back to handle the errors. For Example my functiosn look like this Code: function execute_ssh_command () { test_log INFO "Executing SSH Command $3 on Device $2 with a $1 seconds timeout" "execute_ssh_command" expect <<- DONE set timeout $1 system "touch ~/.ssh/known_hosts" system "ssh-keygen -R $2" spawn /bin/bash sleep 3 send_user "Executing command via SSH...\n" send "/usr/bin/ssh -l root $2\n" expect "(yes/no)?" { send "yes\n" } expect "assword:" { after 1000 send "$DUT_PWD\r" expect " #" send "no_shell_timeout\n" } expect " #" send "$3\r" send_user "\n" expect " #"; send_user "Done." exit expect eof DONE } I have to handle the cases that - No string is given back - The expected string is not given back in a certain amount of time - The expected string is given back in a small amount of time I think i have got the problems cause i really dont know how to handle the exit codes in expect itself. Are there even any error handling functions in expect?? Thanks. Regards Sascha from Germany Reply With Quote 08-17-2012 #2 atreyu View Profile View Forum Posts Private Message View Articles Trusted Penguin Join Date May 2011