1

is there a way to query a server for its OS type in Perl? For example, if I knew that a remote server was running Windows, I might send it a winver from my local machine and get the output to determine which version of Windows it's running. Yet, is there a way to be even more abstract and simply ask "what are you?"

Since CPAN is huge, I was wondering if there were a module that encapsulated this sort of functionality.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
joslinm
  • 7,845
  • 6
  • 49
  • 72
  • 1
    "server" is a wholly ambiguous term. So ambiguous that nobody can answer the question. Information that allows the question to be answered would be stating what protocols this "server" can respond to. – tadmc Sep 09 '11 at 18:05

2 Answers2

3

If you can get command-line access on the remove server, then you should be able to use %ENV:

jmaney> perl -e 'print "$ENV{OSTYPE}\n";'
linux

Edit: It looks as though the key in Windows (or, at least on Windows 7 on my laptop) is OS. So, unfortunately, the exact solution via %ENV is OS-dependent... You could, however, check to see which of $ENV{OS} or $ENV{OSTYPE} is defined (and if they're both defined, then canonically pick which one you want to use), and proceed accordingly.

  • the op is looking to find the OS of a remote server. Your statement works on the 'local' machine (or assuming a login etc) ... and no, the dv is not mine.. – lexu Sep 09 '11 at 16:18
  • And **if OP can get command-line access on the remote server** (as I indicated in the first sentence of my answer), then OP has a shot at figuring out the OS of the remote server. –  Sep 09 '11 at 16:19
  • If you can get command-line access on the remote server there's absolutely no sense in writing a Perl program to run on that server, because you can try things like `uname`. – CanSpice Sep 09 '11 at 16:35
  • 1
    Jack, this is a good answer, thanks. Sorry to confuse.. but I actually *can* get command-line access.. I know of uname and what not, but primarily wanted to see it accomplished via a Perl script from the local machine. The main question should have just focused on wondering if a CSPAN module could query a remote server, given an IP address, and get information about it. Thanks again though. – joslinm Sep 09 '11 at 17:44
2

There is no foolproof way to do this, but the HTTP Server header -- which the server isn't required to send -- often contains the OS. For example, it may look like this (from Wikipedia):

    Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)

The Perl CGI module has an http function that gets the HTTP headers. You could use it like this:

my $server = $q->http('Server');
# Test $server for Windows, *nix, etc
# My Perl experience is minimal and I haven't used it in
# a while, so I'm not going to give an example here, but
# someone can feel free to edit one in.

CPAN probably has a module to do the testing on the Server header for you.

Community
  • 1
  • 1
Peter C
  • 6,219
  • 1
  • 25
  • 37
  • Which unfortunately I'm not able to assume. Regardless, thanks for the answer :) – joslinm Sep 09 '11 at 17:39
  • @Wooble: You're right. Being a web developer, that's what I think of when I think of "server". Since he can get command-line access it doesn't really matter though. – Peter C Sep 09 '11 at 21:06