#!/usr/bin/perl # ncliauto.pl a simple automated client use warnings; use strict; use IO::Socket; my ( $confstr, $host, $port, $kidpid, $handle, $line, @say ); # the config file contains host name (or IP addr) and port number, with a space between # examples: localhost 6100 # myserv.mynet.com 6100 # 123.456.789.1 6100 open( CONF, "<./hsms.conf") or die "conf file: $!\n"; $confstr = ; close( CONF ) or die "closing conf file: $!\n"; chomp $confstr; ( $host, $port ) = split( /\s+/, $confstr ); # This is our demo array of outputs sent to the handler @say = ( 'You are getting sleepy...', '... very sleepy.', 'Your eyes are getting very heavy!', "... it's so hard to hold them open.", "You're so very sleepy now.", 'You just want to go to sleep.', 'Sleep feels so good!', "You're asleep. Sleep!", "You've earned it, just relax and sleep!", "... Sleep!", " Sleep!", " Sleep!", '', '... zzz... zzz... ...zzz ...', 'endit'); # This creates our client socket $handle = IO::Socket::INET->new( Proto => "tcp", PeerAddr => $host, PeerPort => $port ) or die "can't connect to port '$port' on host '$host': $!\n"; # make sure it turns around inputs immediately $handle->autoflush(1); # announce our connection print STDERR "[connected to $host:$port]\n"; # fork a child to handle sending our data to the socket die "can't fork: $!\n" unless defined($kidpid = fork()); if ( $kidpid ) { # The parent handles data coming from the socket server to us while ( defined( $line = <$handle> ) ) { print STDOUT $line; } # ... until the connection is broken kill( "TERM" => $kidpid ); } else { # The child process receives data for us foreach my $item ( @say ) { print $handle $item . "\r\n"; sleep 1; } } exit;