Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Remote Win32 User

by nimdokk (Vicar)
on Nov 14, 2003 at 14:06 UTC ( #307047=perlquestion: print w/replies, xml ) Need Help??

nimdokk has asked for the wisdom of the Perl Monks concerning the following question:

Greetings,
I am trying to run a script that checks for the presence or absence of a particular file on a remote Windows 2000 server. When I test the script as a domain user that has a local account on the remote server, I can test for the file just fine. However, the script needs to be run by a service account that does not have direct access to the machine. However, for the script to run, it needs to run under the domain service account user. Is there a way within the script to "login" to the remote Windows server as a user with local access to the machine for the purposes of checking for a file? I'm trying to find the simplest solution to our little problem that I can. Also, the remote machine does not (currently) have Perl installed but the machine where the script would reside does. Am I just spitting into a windstorm or is there something I might try?

update: I have tried using NT's rsh but we are also going through a firewall and that won't work.


"Ex libris un peut de tout"

Replies are listed 'Best First'.
Re: Remote Win32 User
by AcidHawk (Vicar) on Nov 14, 2003 at 15:11 UTC

    I think you are looking for Win32::NetResource

    Code Snippet:

    my $localdrive = "x:/"; my $RemoteName = "\\\\RemoteSrv\\RemoteShare"; %NetResource = ( LocalName => "$localdrive", RemoteName => "$RemoteName"); my $User = "User"; my $Password = "Password"; if ( Win32::NetResource::AddConnection( \%NetResource, $Pa +ssword, $User, 0) ) { print "Connection Successful!\n"; } else { print "Connection Failed: $^E \n"; }
    you can noe check on $NetResource{LocalName} for the file you want.

    You are effectively mapping a drive to a remote share with specified credentials.

    Update: You can put this code into a win32 service which starts as any user and connect with the local user from the remote machine. (You might need to put the machine name that you are connecting to in front of the remote username ie. remotemachine\remoteuser..)

    Update2: You will need to have NetBIOS (I think) access through the firewall.. basically if you can map a drive from the machine with perl to the remote machine, you can use the above code..;)

    -----
    Of all the things I've lost in my life, its my mind I miss the most.
      good advice.

      but on my system the slash in $localdrive must not appear. After removing it the connection could be established properly.

      greetings, tos

      That might just do the trick. I'll take a look at that particular module and see if it is the answer. Thanks.


      "Ex libris un peut de tout"
Re: Remote Win32 User
by meetraz (Hermit) on Nov 14, 2003 at 19:19 UTC
    The best way to do this is using Win32::Lanman and connecting to the IPC$ share of the server. The IPC$ share let's you establish a security token with the remote server, without limiting yourself to a particular share. Then you can test for the existence of that file using UNC paths.

    An example:

    use strict; use Win32::Lanman; my $result = Win32::Lanman::NetUseAdd({ remote => "\\\\yourserver\\ipc\$", password => "yourpass", username => "youruser", domain => "yourdomain", asg_type => &USE_IPC }); if (! $result) { print "Sorry, something went wrong; error: "; print Win32::Lanman::GetLastError(); exit 1; } #test for file existence if (-e "\\\\yourserver\\c\$\\winnt\\system32\\myfile.dll") { print "File exists\n"; } else { print "File does not exist\n"; }
Re: Remote Win32 User
by inman (Curate) on Nov 14, 2003 at 15:05 UTC
    By default, a service will run as the system account which will not have access to the network share. You should be able to change the service properties to run the service under a different account. (Log On tab on the properties page in the services MMC console.)

    The other thing to look for is the drive letter trap. You as a logged in user can map remote shares to drive letters but the service won't know anything about it. Use UNC paths instead. e.g.

    use \\server\sharename\path\to\file instead of x:\path\to\file

    Try it and let me know if it works!

    inman

Re: Remote Win32 User
by jsprat (Curate) on Nov 14, 2003 at 16:49 UTC
    Can you run it as a "scheduled task" instead of a service? If so, you can specify the domain/user for the job. The user and password will be cached and used to authenticate when the job is run.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://307047]
Approved by nefertari
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2020-08-11 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (58 votes). Check out past polls.

    Notices?