| |||
Help with a cron job Posted by apsawant (apsawant), 26 May 2005 Hi ,I am new to Expect. I would like to write a script that would ssh into a server, do a few cvs checkouts from a particular directory on that server. This has to be automated every 5 minutes. Thus, I started with an exercise whereby I would telnet to the server every 5 and if I am disconnected would reconnect and do something every 5 seconds (lets say, print 'ls -l' in the log file). This is my code whereby I pass in the hostname and the password as arguments to the script: -------------------------------------------------------- #!/sw/local/bin/expect -f # set up all the variables global prompt set login [exec whoami] set hostname [lindex $argv 0] set password [lindex $argv 1] log_user 1 log_file -noappend test.log set prompt "*>*" send_user "BEGIN telnet!\n" for {} {1} {} { sleep 5 spawn telnet $hostname expect "login: " send "$login\r" expect "password: " send "$password\r" set timeout -1 expect eof { continue; } } send_user "END telnet!\n" # End log log_file close ---------------------------------------------------------- The for loop in this code is part of a snippet I found on this forum. This forum has been very helpful. Result: On running this script, it prompts me for the password, but I just press enter (as I pass the password as an argument), and it telnets into the server fine, but nothing happens after 5 seconds (not sure if my script handles this scenario). If somebody can guide me as to what changes need to be made to the for loop wherein the user is connected to the telnet session, then print the time, and disconnect immediately, then reconnect again after 5 seconds, and continue this process as a cron job, that would immensely help me to build up to my final task. Sorry for being so verbose. Thanks, Amit. Posted by admin (Graham Ellis), 26 May 2005 Hello.To have your expect continue after 5 seconds if it doesn't get a response, you need to set timeout 5 before you run your expect command, then include "timeout" as one of the possible expect conditions rather like you included eof. You might also like to look up expect_before and expect_after if you're wanting to catch the timeout in this way on multiple expects. We've got an example of Expect using a timeout in our examples section. As far as a cronjob goes - do you mean you want tyo fire up a new process every five minutes? Yes, you would use cron for that. If you want to have a single process that sleeps for five minutes when it's gone then wakes up again, you could use a wait in your Tcl or possibly a SIGALRM. Let me know whether you're looking to fire up a fresh job or keep one running, and I can post some further pointers when I know which Posted by apsawant (apsawant), 26 May 2005 Hi Graham,Thanks for your quick response. I want to keep the same process running, and have it wake up and check if the telnet session is still running, if not then in this case fire up a new telnet session to reconnect. I will try out your suggestions and let you know. Thanks, Amit. This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
| |||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |