Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

ive wanted to check whether or not my other servers are showing the SSH & TELNET VERSIONS upon each session requests. ive already made my code on SSH first because it goes the same for TELNET too.(u just need to substitue a few things)
#!/usr/bin/perl -w use IO::Socket; $remote = IO::Socket::INET->new( Proto => "tcp", PeerAddr => "210.212.226.195", PeerPort => "ssh(22)", ) || die "cannot connect"; $remote->autoflush(1); while (defined ($line = <$remote>)) { print STDOUT $line; } $remote->exit(0); print "Hello World!";
the thing is... the socket process doesnt terminate prematurely and at the same time go to the next process. it just hangs there. got any ideas?

Replies are listed 'Best First'.
Re: how to terminate an IO:Socket Process prematurely?
by Aristotle (Chancellor) on Jun 02, 2002 at 18:17 UTC

    Obviously because your while loop never terminates. After reading the first line of input, the $line = <$remote> blocks, waiting for more input that never comes.

    Three notes:
    1. You should use strict;
    2. I can't find any mention of an exit() method in the IO::Socket module documentation and it isn't strictly necessary either since IO::Socket will clean up after you at program termination (as do all well-behaved modules).
    3. Your autoflush() has no effect since you're not writing anything onto the socket that might get buffered in the first place.

    Update: I think what you want to do is more like this:
    #!/usr/bin/perl -w use strict; use IO::Socket; my $remote = IO::Socket::INET->new( Proto => "tcp", PeerAddr => "210.212.226.195", PeerPort => "ssh(22)", ) || die "cannot connect"; sysread($remote, my $line, 4*1024) or die "couldn't read any data"; # +one single attempt to read 4k of data print "$line\n";
    ____________
    Makeshifts last the longest.
Re: how to terminate an IO:Socket Process prematurely?
by kodo (Hermit) on Jun 03, 2002 at 07:13 UTC
    close($remote); should do the job, but then $remote is gone completly of course.

    giant
      its me again, CHARLIE. anyway, i found a workaround in my problem. i found the command SHUTDOWN. heres my code:
      #!/usr/bin/perl -w use IO::Socket; $host="202.46.24.25"; $port="21"; #$flags=100; while ($port<24) { $remote = IO::Socket::INET -> new ( Proto => "tcp", PeerAddr => $host, PeerPort => $port ) ; if ($remote) { # $remote->recv($line, $flags) or die "Can't recv: $!\n"; # sysread($remote, $line2, 4*1024) or die "couldn't read any data"; + # print $remote "test foo \n\n\n"; # print $remote "\^\]\[\n\n\n"; shutdown($remote, 1); # can be 0, 1, 2 @line = <$remote>; close $remote; print "$host:$port=>\tActive\t@line\n"; # print LOG "$host:$port=>\tActive\n"; # push @active_ports, $port; } else { print "$host:$port=>\tInactive\n"; # print LOG "$host:$port=>\tInactive\n"; } $port=$port+1; }
      heres the sample output:
      202.46.24.25:21=> Active 220 cronus.wima.ac.id FTP server (Vers +ion wu-2.6.1-20) ready. 221 You could at least say goodbye. 202.46.24.25:22=> Active SSH-1.99-OpenSSH_2.9p2 202.46.24.25:23=> Active ÿýÿý ÿý#ÿý'
      the thing is... its already working. but my friends box when queried on telnet isnt supposed to display ÿýÿý ÿý#ÿý'. i dunno why that thing comes out on telnet queries. my friend's box should display the following:
      Trying 202.46.24.25... Connected to 202.46.24.25. Escape character is '^]'. Red Hat Linux release 7.2 (Enigma) Kernel 2.4.7-10 on an i686
      so got any ideas? please.... no NET::Telnet ideas! tnx for the responses. ive already done my part donating a working code, hope it will be useful for some persons, but i really need to make that TELNET response captured properly. tnx again!