$ ping -ncl perlmonks.org
ping: bad number of packets to transmit.
$ ping -ncl www.perlmonks.org
ping: bad number of packets to transmit.
$
I found the c option to be relevant to what I'm shooting for now. I don't mean to be dismissive about the others, but I hardly understood them. In man ping, these are desribed as follows:
-n Numeric output only. No attempt will be made to lookup
+symbolic
names for host addresses.
-c count
Stop after sending count ECHO_REQUEST packets. With
+deadline
option, ping waits for count ECHO_REPLY packets, until t
+he time‐
out expires.
-l preload
If preload is specified, ping sends that many packets n
+ot wait‐
ing for reply. Only the super-user may select preload m
+ore than
3.
I went with different options to see how it would dovetail with forked processes. I'll have more about ping at the end of this post, but I wanted to post the functions that would house it first, with the logic driven by an initial sftp->mkdir method call. First, I ssh to my site and put a plain file where I know the script is going to want to ask for a new directory:
(uiserver):u61210220:~/pmimage$ pwd >1.pitty2
(uiserver):u61210220:~/pmimage$ cat 1.pitty2
/kunden/homepages/9/d349337426/htdocs/pmimage
(uiserver):u61210220:~/pmimage$
Then I run my normal html template with the new functions. Output then source:
Put file to server(y/n)?: y
server dir is perlmonks
parameter array is perlmonks Net::SFTP::Foreign=HASH(0x55825b47bcc8) C
+ouldn't create remote directory: Failure
error is Couldn't create remote directory: Failure
sub createDir {
use 5.011;
use Net::SFTP::Foreign;
my ( $dirName, $sftp ) = @_;
my $success = $sftp->mkdir($dirName)
or handleDirCreateError( @_, $sftp->error );
return $success;
}
sub handleDirCreateError {
use 5.011;
use Net::SFTP::Foreign;
use Net::SFTP::Foreign::Constants qw(:error);
my ( $dirName, $sftp, $error ) = @_;
say "parameter array is @_";
say "error is $error";
return $error;
}
The error's description is less than complete.
I'm trying to get the functionality described in https://metacpan.org/pod/Net::SFTP::Foreign::Constants but seem to be missing something, as my error message looks a lot different than the ones in that module. One hopes to catch salva's eye, but it's improbable this deep in my own shit.
Q1) How do I employ the constants in that module for this case?
I've folded together elementary forking and pinging, and I really don't get what I've unearthed. This is the source:
$ cat 6.fork.pl
#!/usr/bin/perl -w
use 5.011;
my $start = time;
if (fork() == 0) {
# arm the alarm clock
alarm(10);
# create a child process that sleeps
system("ping www.google.com >5.fork.txt");
exit(0);
}
say "relevant processes with pstree command:";
system("pstree -Apal $$");
while((my $pid = wait()) != -1) {
say "$pid terminated"
}
system("cat 5.fork.txt");
say time-$start, " ? <-ten seconds elapsed from forked process"
__END__
$
This was the first trial:
This was as I expected it. The icmp_seq= went from 1 to 10. I expected the same output if run again and was wrong:
The icmp_seq= values go up to 152, and there is twice as much output. The third trial had these values all over the map:
Q2) What does icmp_seq=608 mean in this context?
Q3) With 30 seconds of pinging google, how much data was sent? How much of this type of activity can a person do without it being interpreted as an attack?
What really threw me for a loop is when I walked away from my laptop, came back later to it and find:
$ ping: sendmsg: Network is unreachable
ping: sendmsg: Network is unreachable
ping: sendmsg: Network is unreachable
...
They descended like ghosts, long after the programs were supposedly done executing, and twice.
After a couple of days of making mistakes, this seems like a good ping command to test whether one has internet connectivity:
$ ping -c 3 www.google.com
PING www.google.com(sea15s12-in-x04.1e100.net (2607:f8b0:400a:809::200
+4)) 56 data bytes
64 bytes from sea15s12-in-x04.1e100.net (2607:f8b0:400a:809::2004): ic
+mp_seq=1 ttl=54 time=17.0 ms
64 bytes from sea15s12-in-x04.1e100.net (2607:f8b0:400a:809::2004): ic
+mp_seq=2 ttl=54 time=21.1 ms
64 bytes from sea15s12-in-x04.1e100.net (2607:f8b0:400a:809::2004): ic
+mp_seq=3 ttl=54 time=19.0 ms
--- www.google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 17.096/19.092/21.126/1.649 ms
Q4) What could you regex for to decide that this were a success?
Finally, I didn't want to post before taking a whirl with hippo's suggestion of Net::Ping. Here's the source:
#!/usr/bin/perl -w
use 5.011;
use Net::Ping;
my $start = time;
if ( fork() == 0 ) {
# arm the alarm clock
alarm(5);
my @sites = qw/www.google.com www.merrillpjensen.com www.perlmonks.o
+rg/;
for my $host (@sites) {
my $p = Net::Ping->new();
print "$host is alive.\n" if $p->ping($host);
$p->close();
}
exit(0);
}
say "relevant processes with pstree command:";
system("pstree -Apal $$");
while ( ( my $pid = wait() ) != -1 ) {
say "$pid terminated";
}
#system("cat 7.fork.txt");
say time - $start, " ? seconds elapsed from forked process"
__END__
The unimpressive output is:
relevant processes with pstree command:
7.fork.pl,27579 -w ./7.fork.pl
|-7.fork.pl,27580 -w ./7.fork.pl
`-pstree,27581 -Apal 27579
27580 terminated
5 ? seconds elapsed from forked process
The intent is to loop over a few sites and see if pinging is successful. I wonder what makes a site pingable like google, and how we might handle a ping sent to our own sites.
Anyways, long post, arrrggh...
Thank you for your comments, |