news.utdallas.edu!wupost!howland.reston.ans.net!usc!enterpoop.mit.edu!senator-bedfellow.mit.edu!athena.mit.edu!mkgray Mon Mar 15 14:45:49 CST 1993
Article: 1591 of comp.lang.perl
Xref: feenix.metronet.com comp.lang.perl:1591
Path: feenix.metronet.com!news.utdallas.edu!wupost!howland.reston.ans.net!usc!enterpoop.mit.edu!senator-bedfellow.mit.edu!athena.mit.edu!mkgray
From: mkgray@athena.mit.edu (Matthew K Gray)
Newsgroups: comp.lang.perl
#Subject: Re: Getting perl to do a telnet
Date: 15 Mar 1993 18:49:38 GMT
Organization: Massachusetts Institute of Technology
Lines: 110
Distribution: world
Message-ID: <1o2j42INN8cf@senator-bedfellow.MIT.EDU>
References: <C3wI4q.DAB@cantua.canterbury.ac.nz>
NNTP-Posting-Host: podge.mit.edu
Keywords: chat2, chat, telnet, geography

The easiest way to do this is probably with the chat2.pl package.
This is available all over the place by ftp.
(eg, gatekeeper.dec.com:/contrib/src/crl/perl/src/lib/chat2.pl)

Here is a sample of using chat2.pl that retrieves info from the geographical
name server:

---cut here---cut here---cut here---
#!perl

if($#ARGV < 0) {&usage();}
sub usage {
	print("Usage:\n");
	print("\tgeo place name\n");
	exit(0);
}


require 'chat2.pl';


sub listen {
        local($secs) = @_;
        local($return,$tmp) = "";
        while (length($tmp = &chat'expect($secs, '(.|\n)+', '$&'))) {
                print $tmp if $trace;
                $return .= $tmp;
        }
	$return;
}
# 0 <city name>
# 1 <county FIPS code> <county name>
# 2 <state/province abbreviation> <state/province name>
# 3 <nation abbreviation> <nation name>
# A <telephone area code>
# E <elevation in feet above mean sea level>
# F <feature code> <feature name>
# L <latitude DD MM SS X> <longitude DDD MM SS X>
# P <1980 census population>
# R <remark>
# T <time zone>
# Z <postal ("ZIP") code>


$request = join(' ', @ARGV);
&chat'open_port('martini.eecs.umich.edu', 3000) || die "open: $!";
$_=&listen(2);
while(!/Geographic/){
$_=&listen(5);
}
print("Geographic NameServer Query:  \"$request\"\n");
&chat'print("$request\n");
$_=&listen(3);
split(/\n/);
foreach $dataline (@_){
	@els = split(/[ ]+/, $dataline);
	$foo =shift(@els);
	if($foo=~/0/){
		$rest = join(' ',@els);
		chop($rest);
                print("\nLocation Name:  $rest\n");
                }
        if($foo=~/1/){
		$bar = shift(@els);
                $rest = join(' ', @els);
		chop($rest);
                print("County:         $rest ($bar)\n");
		}
	if($foo=~/2/){
		$bar = shift(@els);
		$rest = join(' ', @els);
		chop($rest);
		print("State/Province: $rest ($bar)\n");
	}
	if($foo=~/3/){
		$bar = shift(@els);
		$rest = join(' ', @els);
		chop($rest);
		print("Country:        $rest ($bar)\n");
	}
	if($foo=~/A/){
		$rest = join(' ', @els);
		chop($rest);
		print("Area Code:      $rest\n");
	}
	if($foo=~/E/){
		$rest = join(' ', @els);
		chop($rest);
		print("Elevation:      $rest\n");
	}
	if($foo=~/L/){
		print("Latitude:       $els[0] $els[1]' $els[2]\" $els[3]\n");
		print("Longitude:      $els[4] $els[5]' $els[6]\" $els[7]\n");
	}
	if($foo=~/P/){
		print("Population:     $els[0]\n");
	}
	if($foo=~/T/){
		print("Time Zone:      $els[0]\n");
	}
	if($foo=~/Z/){
		print("Zip Code:       $els[0]\n");
	}
	if($foo=~/R/){
		$rest = join(' ', @els);
		print("Comment:	       $rest\n");
	}

}
&chat'print("quit\n");


