Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

Expect monitoring

Posted by neo (The Architect), 2 April 2005
Hi all,  
How do I write a expect script that will telnet to a remote machine and keep on checking if I am disconnected or not?
I want to reconnect as soon as I am disconnected.
How is this done?

Posted by admin (Graham Ellis), 2 April 2005
How about "expect"ing an eof , and if you get one the reconnect?

Posted by neo (The Architect), 3 April 2005
Tried that but after typing an exit on the remote machine I just get no prompt or anything.
Here is the code I used.
Code:
expect -re "$POMPT "
send "ls -l\r"
expect eof {send_user "telnet $HOST"; exit}

The code in the expect eof block is just for a test right now. It will be changed later to make it reconnect later.

Posted by neo (The Architect), 4 April 2005
Does anyone have some example of this. I want to see how to send the commands after eof is received.  
I tried
Code:
expect eof {send "telnet $HOST\r";}


Posted by neo (The Architect), 4 April 2005
Code:
proc login2host {} {
     expect -re ".*login: "
     send "$USERNAME\r"
     expect -re "Password: "
     send "$USERPASS\r"
     expect -re "$POMPT\% "
}

for {} {1} {} {
     sleep 10
     spawn telnet $HOST
     login2host
     set timeout -1
     expect eof {
           continue;
     }
}


Noe if I need to send some commands too while I am connected how do i do this. I mean I need the user to be able to be interact too (at least for a few commands).
How is this done?

Posted by admin (Graham Ellis), 5 April 2005
on 04/04/05 at 14:54:06, The Architect wrote:
[code]Noe if I need to send some commands too while I am connected how do i do this. I mean I need the user to be able to be interact too (at least for a few commands).
How is this done?


If you know want to send the commands automatically and analyse the responses automatically, then use send and expect commands.  If you want to drop to keyboard input and display the response, then you can use the intereact command instead.

Posted by neo (The Architect), 5 April 2005
Got that solved last night when I tried it out. Phew!
@Graham: Yeah I found that out with a little searching (in the man page). I sort of want to use both.

Anyway another doubt now.
I have a list of commands that I want to execute just after login. What kind of a loop can I use for this (some sample code would be nice) . I thought of storing the commands in a array and then usign that. How is this done in code?

Posted by admin (Graham Ellis), 5 April 2005
on 04/05/05 at 12:48:18, The Architect wrote:
Got that solved last night


Well THANK YOU for letting me know before I put time into coming up with an answer  

I appreciate that there are times you'll ask questions but then find out the answers elsewhere, but PLEASE have the decency to come back here and tell us that you've found an answer.  I'm delighted to help people, but I don't have a lot of time and it's a bit of a slap in the face to put effort in for someone to have them say "oh - I got that answered elsewhere ...."

Posted by neo (The Architect), 6 April 2005
on 04/05/05 at 14:52:54, Graham Ellis wrote:
Well THANK YOU for letting me know before I put time into coming up with an answer  

I appreciate that there are times you'll ask questions but then find out the answers elsewhere, but PLEASE have the decency to come back here and tell us that you've found an answer.  I'm delighted to help people, but I don't have a lot of time and it's a bit of a slap in the face to put effort in for someone to have them say "oh - I got that answered elsewhere ...."


Hey Man I really appreciate your help. In fact I also searched through this board and that was helpful.

Do you think I would not post here as soon as I get an answer? I did not have net access at that time (and I am in a place where I don't control that). I'm really sorry if you were offended.

Posted by admin (Graham Ellis), 6 April 2005
I appreciate that not everyone has net access all the time.  Shall we just say that this looks a little unfortunate.   A question's asked, an answer found elsewhere within a very short time, and then other(s) spin their wheels looking for a solution while the original questioner goes off for a good night's sleep.

"Opentalk" is intended for questions that are difficult to get answered elsewhere.  The format's not well suited to researching things where you have a number of active and parallel lines of enquiry all going on at the same time as (inevitably) incidents like this will happen.   I appreciate your enthusiasm to ask, but can I suggest that you limit yourself to questions that you're very unlikely to get solved elsewhere in the very short term and that if you're lucky enough to solve them through another route after all you make a concerted effort to let us know, even if it means specially dialling in to post?  That's a great way to help us help you.



Posted by neo (The Architect), 6 April 2005
Whoops, had to read that twice slowly to get it.
And yes I will try to be more careful about posting things that I may be able to solve sooner or later.
And no I cannot dial in (absolutely prohibited in the wrong hours   )

Posted by neo (The Architect), 21 May 2005
I had written this script that keeps logging into the remote server even after I get disconnected.
It works fine but I've noticed that after a while the process seems to be killed because I see that I am out of the remote and the terminal shows this

Quote:
Killed


Is there anything I have to do so that I am always connected  and this script process is not killed?

Posted by admin (Graham Ellis), 21 May 2005
I suspect that the remote machine has been set up to guard against automated access and recognises it in various ways (e.g. timeouts, allowing for connections for only a certain proportion of the time, etc).   So you're probably going to have to play a game of "cat and mouse" in order to maintain your connection.

I always have a slightly uneasy feeling when someone's writing software to "spoof" a remote system to allow then to remain connected ... is it OK with the remote machine's administrator that you're handing on in this way?   If so, could he/she provide you with an easier form of access that doesn't time out?   There's more than one way to catch a crockodile, you know  

Posted by neo (The Architect), 21 May 2005
No I have absolute access on the remote machine (root). I think my local sys kills the process after some time. Is there any way to verify and guard against this?

Posted by admin (Graham Ellis), 23 May 2005
on 05/21/05 at 17:00:31, The Architect wrote:
No I have absolute access on the remote machine (root). I think my local sys kills the process after some time. Is there any way to verify and guard against this?


That sounds like it's a local operating system issue  

Posted by neo (The Architect), 23 May 2005
Yeah is there anything I can do about this?
If i start this in the background will this be avoided?

Posted by admin (Graham Ellis), 23 May 2005
It's an operating system matter - it depends on what operating system you're using, which command line interpretter ("shell") and what parameters / settings you have.  You'll probably find you can find an answer in the documentation for that operating system, or on chat rooms / forums that are OS specific rather than here, which is language specific.

I would expect (please excuse that pun - it's hard to avoid) that i fyou run your process as a daemon or service you might be in luck - but you do need to ensure that the settings percolate through to your spawned process.

Posted by neo (The Architect), 23 May 2005
Ok thanks. I'll try that.



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.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho