merlyn@iwarp.intel.com (Randal L. Schwartz) (04/23/91)
Here's another goodie I wired up while play-testing my chat2.pl script (published last week, I believe). It handles telnet option processing (in a trivial way), allowing you to talk directly to a telnet port without invoking 'telnet'. Look at the 'talk/listen' commands to see what it is doing. Turn $trace on to see what the interaction looks like. #! /bin/sh # This is a shell archive. Remove anything before this line, then unpack # it by saving it into a file and typing "sh file". To overwrite existing # files, type "sh file -c". You can also feed this as standard input via # unshar, or by typing "sh <file", e.g.. If this archive is complete, you # will see the following message at the end: # "End of shell archive." # Contents: weather # Wrapped by merlyn@iwarpse on Mon Apr 22 16:14:23 1991 PATH=/bin:/usr/bin:/usr/ucb ; export PATH if test -f 'weather' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'weather'\" else echo shar: Extracting \"'weather'\" \(1467 characters\) sed "s/^X//" >'weather' <<'END_OF_FILE' X#!/usr/bin/perl X X$trace = 0; X X$where = "pdx"; X X$| = 1; Xrequire './chat2.pl'; X X&chat'open_port('hermes.merit.edu', 23) || die "open: $!"; XMAIN: { X X$_ = &listen(10); Xlast MAIN unless /which host/i; X&talk("um-weather\r"); X$_ = &listen(5); Xlast MAIN unless /c.*change scrolling/i; X&talk("c\r"); X$_ = &listen(5); Xlast MAIN unless /4.*unlimited/i; X&talk("4\r"); X$_ = &listen(5); Xlast MAIN unless /1.*city/i; X&talk("1\r"); X$_ = &listen(5); Xlast MAIN unless /1.*selected city/i; X&talk("1\r"); X$_ = &listen(5); Xlast MAIN unless /3-letter city code/i; X&talk("$where\r"); X$_ = &listen(5); # the weather is in here somewhere X $weather = $_; X $weather =~ s/\r//g; X $weather =~ s/\s*Select an option(.|\n)+$//; Xlast MAIN unless /x.*exit program/i; X&talk("x\r"); X$_ = &listen(5); X Xprint "$where weather:\n$weather\n"; X} X&chat'close(); Xexit(0); X X Xsub talk { X local($text) = @_; X print "{$text}" if $trace; X &chat'print($text); X} X Xsub listen { X local($secs) = @_; X local($return,$tmp) = ""; X while (length($tmp = &telnet_read($secs))) { X print $tmp if $trace; X $return .= $tmp; X } X $return; X} X Xsub telnet_read { X local($secs) = @_; X &chat'expect($secs, X '^\377[\375\376](.|\n)', X q#&chat'print ("\377\374".$1); redo LOOP#, X # WON'T do these do/don't requests X '^\377[\373\374](.|\n)', 'redo LOOP', X # ignore these will/won't changes X '^\377\377', '"\377"', X # escaping the IAC X '^\377(.|\n)', 'redo LOOP', X # ignoring these X '^[^\377]+', '$&' X # return these X ); X} END_OF_FILE if test 1467 -ne `wc -c <'weather'`; then echo shar: \"'weather'\" unpacked with wrong size! fi chmod +x 'weather' # end of 'weather' fi echo shar: End of shell archive. exit 0 print "Just another Perl hacker," -- /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\ | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn | \=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/