Search code examples
phplinuxunixcommand-line-interfacepacket

Ping from Dynamic Language without using 'ping'


I'm on a Linux system where I am not allowed to use the 'ping' application (ping: icmp open socket: Operation not permitted). However, the script that I am writing (PHP, but I can use an exec() call to any script/program if needed) needs to determine if a host is 'alive'. How can I go about this without using 'ping'?


Solution

  • If ping can't do it, you can't do it in a different language. Here is an analogy that may help you understand why. Let's say there is a file on the file system and you want to its contents. You run cat filename and it says cat: filename: Permission denied. Do you think Perl (or any other language) will fair better than C did here? Let's try:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    
    die "usage: $0 filename" unless @ARGV == 1;
    my $filename = shift;
    
    open my $fh, "<", $filename
        or die "could not open $filename: $!\n";
    
    print while <$fh>;
    

    When run against the file it says could not open filename: Permission denied. No matter what language you try to use, you are going to get Operation not permitted.

    That said, there are other methods of determining if a machine is alive. If there is a server that is known to always be running on the machine, you could try to connect to it. Note that you don't need to finish the connection (e.g. log in), just the fact that you can successfully initiate the connection is enough to know that box is up.