CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Win32 Programming Help:
GetComputerName

 



beefkirky
newbie

Jun 27, 2001, 7:03 PM

Post #1 of 15 (10484 views)
GetComputerName Can't Post

I'm trying to make a cgi script that can access and record the Computer Name of the computer that visits a particular website.

I'm writing the script on a Win2K laptop, and the people accessing the page will be doing so from Win2K as well. The webpage and scripts are hosted on some Unix flavour though. I am pretty sure the Perl version on the Unix is 5. I'm not able to easily add any other modules though.

Does anyone have any ideas how I can access the computer name?

Thanks for any help.



mhx
Enthusiast

Jun 27, 2001, 10:17 PM

Post #2 of 15 (10479 views)
Re: GetComputerName [In reply to] Can't Post

Hi,

when writing a CGI script, you get the IP address of the one that is accessing your script from $ENV{REMOTE_ADDR}. You can then use the gethostbyaddr function to turn the IP into a hostname.

Code
#!/bin/perl -w 
use Socket;
use strict;

# In a CGI script, you can get the IP address
# of the caller from $ENV{REMOTE_ADDR}

my $ip = "216.239.33.100";
my $name = gethostbyaddr( inet_aton($ip), AF_INET ) || 'unknown';

print "Remote Host: $name ($ip)\n";

This will work fine for both Windows and Unix, but only if there isn't a proxy or firewall or ISP in between. I don't know if there's another possibility to get the 'real' name of the computer. I there is another way, please let me know!

-- Marcus



localfilmmaker
stranger

Feb 10, 2002, 10:50 PM

Post #3 of 15 (10452 views)
Re: [mhx] GetComputerName [In reply to] Can't Post

Or you could use the environment variable $ENV{REMOTE_HOST} instead of doing the name lookup. The variable may not be set on all systems, though. It depends on the web server which ENV vars get set. So you should make a quick script to print out all the ENV vars on the system to see which ones the web server is setting. My advice is if $ENV{REMOTE_HOST} is there, then use it.

Example cgi script to print out the ENV vars:

Code
#!/usr/bin/perl -w 
use strict;
print "Content-type: text/html\n\n";
print "<h1>ENV Vars:</h1>\n";
foreach (sort keys %ENV) {
print "$_ = $ENV{$_}<br>\n";
}

-localfilmmaker
Spencer Christensen


Paul
Enthusiast

Feb 11, 2002, 8:52 AM

Post #4 of 15 (10435 views)
Re: [localfilmmaker] GetComputerName [In reply to] Can't Post


Code
#!/usr/bin/perl -w  
use strict;
print "Content-type: text/html\n\n";
print "<h1>ENV Vars:</h1>\n";
foreach (sort keys %ENV) {
print "$_ = $ENV{$_}<br>\n";
}


How about:

print join("<BR>", map { "$_ = $ENV{$_}" } keys %ENV);

Cool


(This post was edited by WiredON.net on Feb 11, 2002, 8:53 AM)


Jean
User


Feb 17, 2002, 6:04 AM

Post #5 of 15 (10422 views)
Re: [RedRum] GetComputerName [In reply to] Can't Post

TIMTOWTDI: SmileSmileSmile

print "$_ = $ENV{$_}\n" foreach(sort keys %ENV);


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.


mhx
Enthusiast

Feb 17, 2002, 9:33 AM

Post #6 of 15 (10419 views)
Re: [Jean] GetComputerName [In reply to] Can't Post

Or: WinkTongueCool

[perl]
print "$_ = $ENV{$_}\n" for sort keys %ENV;
[/perl]

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



Paul
Enthusiast

Feb 17, 2002, 10:21 AM

Post #7 of 15 (10416 views)
Re: [mhx] GetComputerName [In reply to] Can't Post

Mine's shorter Tongue

print map { "$_ = $ENV{$_}\n" } keys %ENV;

35 chars.

print "$_ = $ENV{$_}\n" for sort keys %ENV;

36 chars

Laugh

but I guess you win for sorting too :)


(This post was edited by RedRum on Feb 17, 2002, 10:22 AM)


Jean
User


Feb 18, 2002, 12:09 AM

Post #8 of 15 (10410 views)
Re: [RedRum] GetComputerName [In reply to] Can't Post

Actually the only thing I meant was TIMTOWTDI ;-)

But nevertheless thank you for your kind words Cool

Another reason was, probably, readability for beginners.

When writing code I'm trying not to use the $_ variable (unlike in the example I've posted here Blush)




Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.


Paul
Enthusiast

Feb 18, 2002, 1:41 AM

Post #9 of 15 (10407 views)
Re: [Jean] GetComputerName [In reply to] Can't Post

Hi,

Sorry, that post was directed to mhx


Jean
User


Feb 18, 2002, 3:05 AM

Post #10 of 15 (10404 views)
Re: [RedRum] GetComputerName [In reply to] Can't Post

I see Wink ...

I thought you've just adapted my version a little (if you'll look at my 1st post here, you'll see that we're talking about very similar solutions here - my solution is longer because I'm not a "golfer" and that wasn't my intention to write the shortest possible code Smile).


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.


Paul
Enthusiast

Feb 18, 2002, 3:10 AM

Post #11 of 15 (10401 views)
Re: [Jean] GetComputerName [In reply to] Can't Post

>>
thought you've just adapted my version a little (if you'll look at my 1st post here, you'll see that we're talking about very similar solutions here
<<

They look very different to me :)


mhx
Enthusiast

Feb 18, 2002, 3:20 AM

Post #12 of 15 (10399 views)
Re: [Jean] GetComputerName [In reply to] Can't Post

My intention also wasn't to write it in the shortest possible way. If I had been golfing, I think I would have posted something else. (As there's so many redundant whitespace in my code... Crazy)

My intention was to show that foreach and for can be interchanged. This is hard if you come from C (as I do) but if you're a "native" Perl speaker this is easy and saves you lots of typing. Another thing that I really like about Perl is that you can leave away parentheses in so many places and the fact that this can really improve readability. Again, this is a little hard for C programmers...

-- mhx

P.S.: Even shorter:


Code
print Dumper \%ENV;


Assuming you have the invaluable Data::Dumper module already loaded. Smile

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



Jasmine
Administrator

Feb 18, 2002, 9:52 AM

Post #13 of 15 (10392 views)
Re: [mhx] GetComputerName [In reply to] Can't Post


In Reply To
Assuming you have the invaluable Data::Dumper module already loaded. Smile


As of 5.6.1, Data::Dumper is part of the standard distribution Smile


mhx
Enthusiast

Feb 18, 2002, 10:14 AM

Post #14 of 15 (10390 views)
Re: [Jasmine] GetComputerName [In reply to] Can't Post


In Reply To
As of 5.6.1, Data::Dumper is part of the standard distribution Smile


It is also in 5.6.0, but the version's slightly broken for large unsigned integers.
With "already loaded" I meant "use Data::Dumper;" is already in the script Wink

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



Jean
User


Feb 19, 2002, 2:51 AM

Post #15 of 15 (10384 views)
Re: [RedRum] GetComputerName [In reply to] Can't Post


In Reply To
They look very different to me :)

This only shows that I'm again missing some point :-(


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives