Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pull an environment variable from a client

Status
Not open for further replies.

nix45

MIS
Nov 21, 2002
478
US
I have a web server running Apache and I was wondering if I can pull a local environment variable from the connecting client machine (Windows). I want to pull this from the client --> $ENV{username}. Is this possible somehow? The username will then be entered into a CGI script that searches for something. Right now, the users have to enter they're username each time but I'd like for this to be automated.

btw, not sure if I should have posted this in the CGI forum instead.

Thanks,
Chris
 
I don't think username is an environment variable that Apache can read, but there's always one way to find out:

Code:
use CGI qw/:standard/;

print header;

print qq(<html><body><pre>\n);

foreach $key (keys(%ENV))
{
    print qq($key: $ENV{$key}\n);
}

print qq(</pre></body></html>\n);

Let me know the results (as I don't have access to an Apache web server that I can check this against) and we'll go from there. Also, what OS is your web server using? Win2000, WinXP, Linux, Unix, etc.

- Rieekan
 
I ran the script and it prints a bunch of information like this...

SCRIPT_NAME: /cgi-bin/usertest.pl
SERVER_NAME: quest
SERVER_ADMIN: root@foo.org
HTTP_ACCEPT_ENCODING: deflate, gzip, x-gzip, identity, *;q=0
HTTP_CONNECTION: Keep-Alive, TE
REQUEST_METHOD: GET

That was just a small sample of what it printed. This server in particular is running on Winblows unfortunately, but I do have some running on Linux that I will use this on too if I get it working.

Thanks,
Chris
 
It's grabbing your information as well as some server side environment variables. See if you can see your username as one of the variables. If not, run this via the command line replacing <IPADDR> with your IP Address and let me know the results (just in case I have to tweak my program).

Code:
nbtstat -a <IPADDR>

- Rieekan
 
It only grabs server information, nothing client side except for my IP address and browser name and version. I'll print the full output if you want to see it.

Why do you want to see my NetBIOS information? Here it is anyway...

F:\>nbtstat -a 192.0.1.60

Local Area Connection:
Node IpAddress: [192.0.1.60] Scope Id: []

NetBIOS Remote Machine Name Table

Name Type Status
---------------------------------------------
INFO197 <00> UNIQUE Registered
DOMAIN <00> GROUP Registered
INFO197 <20> UNIQUE Registered

MAC Address = 00-07-E9-E5-22-94



Chris
 
I didn't expect the Environment variables would be able to capture the information, and based on your nbtstat info, it doesn't look like you're logging into a domain controller either, so using that function is out.

As of now, I'm not sure what to do to get that information into your scripts. I use this handy little function, but I've never had to worry about users being outside of the Windows NT/2000/XP OS and logging into a domain controller. You can give it a try if you'd like, but based on the information above, it's not going to work. One thing you'll need to watch for is that the <03> type is used for both the Computer name and the User ID logged into the machine. If you use specific naming conventions for your computers, you're all set. (I.E. - ours are all named NWxxxxxx and our usernames are last name plus first initial and number so I know there won't be an NW combination for the username)

Code:
sub get_user
{
    my $user = "Guest";

    my @output = `c:/windows/system32/nbtstat.exe -A $ENV{'REMOTE_ADDR'}`;

    foreach my $line (@output)
    {
        my @info = split / /, $line;
        undef my @line;
        foreach my $ele (@info)
        {
            if ($ele ne "")
            {
                push @line, $ele;
            }
        }
        if ($line[1] =~ m/<03>/)
        {
            if ($line[0] =~ /^DOMAIN/i)
            {
                next;
            }
            else
            {
                $user = $line[0];
            }
        }
    }
    return ($user);
}

my $user_info = get_user();

- Rieekan
 
I am logging into a domain, it says that above with the "DOMAIN <00> GROUP Registered" line.

How do you pull a username from the output of nbtstat? I ran it from another computer in the domain against my machine but it doesn't show anything like that.

Thanks,
Chris
 
I thought of a possible solution that is similar to yours. The only problem with it is that our web server isn't part of our domain. What I was thinking is that we do something like this...

Code:
$remote = $ENV{REMOTE_ADDR};
@qwinsta = "C:\\windows\\system32\\qwinsta /SERVER:$remote";
foreach (@qwinsta) {
   do some regexps to extract the username;
}

qwinsta is a built-in Windows command.

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top