El siguiente programa comprueba si el servicio SSH esta operativo en un servidor:
pp2@europa:~/src/perl/perltesting$ cat -n sshping.pl 1 #!/usr/bin/perl 2 3 use warnings; 4 use strict; 5 use Socket; 6 my ($remote,$port, $iaddr, $paddr, $proto, $line); 7 8 $remote = $ARGV[0] || die "usage: $0 hostname"; 9 $port = 22 ; # the SSH port 10 $iaddr = inet_aton($remote) || die "no such host: $remote"; 11 $paddr = sockaddr_in($port, $iaddr); 12 $proto = getprotobyname('tcp'); 13 14 socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket() failed: $!"; 15 print "Connecting to port 22...\n"; 16 connect(SOCK, $paddr) || die "connect() failed: $!"; 17 print "Connected.\n"; 18 $line = <SOCK>; 19 print $line; 20 exit 0 if $line =~ /SSH/; 21 exit 1; 22 __END__ 23 24 =head1 NAME 25 Remote host back online test 26 27 =head1 WHERE 28 29 Question: I<Remote host back online test> at PerlMonks. 30 L<http://www.perlmonks.org/?node_id=653059> 31 32 =head1 HELP 33 34 If you want to test whether the SSH service on the remote host is ready to 35 accept logins you can try connecting to TCP port 22 on the remote host and see 36 if you can read the SSH banner from it. I adapted this example from the perlipc 37 documentation: 38 39 With the SSH server on my box running: 40 41 $ perl sshping.pl localhost; echo $? 42 Connecting to port 22... 43 Connected. 44 SSH-1.99-OpenSSH_x.xx Debian-xxx 45 0 46 47 If I stop the SSH server on my box: 48 49 $ perl sshping.pl localhost; echo $? 50 Connecting to port 22... 51 connect() failed: Connection refused at sshping.pl line 14. 52 111 53 54 You might want to have the perl script loop (with a sleep for a few seconds) 55 and retry the connect, or you could do that in an external bash script by 56 checking the exit code... 57 58 $ if perl sshping.pl localhost; then echo up; else echo down; fi 59 Connecting to port 22... 60 connect() failed: Connection refused at sshping.pl line 14. 61 down 62 63 =head1 AUTHOR 64 65 quester 66 L<http://www.perlmonks.org/?node_id=576594>
Escriba una versión paralela del programa anterior que determina que subconjunto de máquinas tiene sus servicios SSH operativos.