You can probably follow the advice above and get timeouts
to do what you need. If not, try the Expect
module. There are more Expect examples here.
You may find that Expect does a lot of other things
that your program needs for dealing with a telnet
session.