Try modifying the following.  It checks every 15 seconds to see if a couple
of files have been opened for more than 90 seconds by the same sessionid and
closes the file.  You can take the part out checking the day of the week.
That was when we backed to server up. 

use Win32::Lanman;
use Win32;

$cnt = 1;
$lockcnt = 0;
$hmlockcnt = 0;

while ($cnt == 1)
{
  if(Win32::Lanman::NetFileEnum('', '', '', [EMAIL PROTECTED]))
  {
          $time = localtime(time);
                print "$time\n";
                if(index($time,"Sat") ge 0)
                {
                        if(index($time,"23:00") ge 0)
                        {
                                sleep(86400); 
                        }
                }
    foreach $info (@infos)
    {
          undef $fname;
                        $fname = ${$info}{pathname};
                if ($fname eq "D:\\APPS\\HM\\NW\\NODE.INF")
                        {
                          $fileid = ${$info}{id};
                                if(Win32::Lanman::NetSessionEnum('', "",
"${$info}{username}", [EMAIL PROTECTED]))
        {
          foreach $session (@sessions)
                                {
                                                print "cname:
${$session}{cname}\n";
                                }
                                }
                                if(Win32::Lanman::NetFileGetInfo('',
$fileid, \%info))
                {
                @keys = keys(%info);
                        foreach $key(@keys)
                        {
            print "$key: $info{$key}\n";
                                  }
                                        if ($info{permissions} == 35 or
$info{permissions} == 1)
                                        {
                                          if ($fileid == $closefileid)
                                          {
                                            print "$fileid is suspect\n";
                                                        $lockcnt++;
                                          }
                                          else
                                          { 
                                            $closefileid = $fileid;
                                                        $lockcnt = 0;
                                          }
                                        }
                                        if ($lockcnt > 5)
                                        {
        
if(!Win32::Lanman::NetFileClose('', $fileid))
                                                {
                                print "Sorry, something went wrong; error:
";
                                # get the error code
                                print Win32::Lanman::GetLastError();
                                exit 1;
                                                }
                                        }
                          }
                  }
                        if ($fname eq "D:\\APPS\\HM\\DB\\HMLOCKS.DAT")
                        {
                          $hmfileid = ${$info}{id};
                                if(Win32::Lanman::NetSessionEnum('', "",
"${$info}{username}", [EMAIL PROTECTED]))
        {
          foreach $session (@sessions)
                                {
                                                print "cname:
${$session}{cname}\n";
                                }
                                }
                                if(Win32::Lanman::NetFileGetInfo('',
$hmfileid, \%info))
                {
                @keys = keys(%info);
                        foreach $key(@keys)
                        {
            print "$key: $info{$key}\n";
                                  }
                                        if ($info{permissions} == 35 or
$info{permissions} == 1)
                                        {
                                          if ($hmfileid == $hmclosefileid)
                                          {
                                            print "$hmfileid is suspect\n";
                                                        $hmlockcnt++;
                                          }
                                          else
                                          { 
                                            $hmclosefileid = $hmfileid;
                                                        $hmlockcnt = 0;
                                          }
                                        }
                                        if ($hmlockcnt > 5)
                                        {
                                                $fileid = $hmfileid;
        
if(!Win32::Lanman::NetFileClose('', $fileid))
                                                {
                                print "Sorry, something went wrong; error:
";
                                # get the error code
                                print Win32::Lanman::GetLastError();
                                exit 1;
                                                }
                                        }
                          }
                  }
                }
        }
        print
"---------------------------------------------------------------------------
-----";
        sleep(15);
} 

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Gould,
Kevin
Sent: Thursday, February 05, 2004 9:43 AM
To: Dave Roth; Thomas Berk; Perl-Win32-Admin-Request (E-mail)
Subject: RE: Closing open file sessions on a server

It's not Perl, but you can find open files using the utilities

Handle - from Sysinternals.com
Or 
Oh - from the Windows resource kits
 

-----Original Message-----
From: Dave Roth [mailto:[EMAIL PROTECTED] 
Sent: Thursday, February 05, 2004 12:34 AM
To: Thomas Berk; Perl-Win32-Admin-Request (E-mail)
Subject: RE: Closing open file sessions on a server

Both of the attached scripts are from my second book, "Win32 Perl
Programming: Administrators Handbook"
(http://www.roth.net/books/handbook/). The first one, openfiles.pl, will
display all files that have been opened *remotely*. Unfortunately there
is no easy way to discover all open file handles locally (nor would you
want to since there would literally be hundreds).
The other script (closefile.pl) will force closed a given file that has
been opened remotely.
For both files, pass in "-h" for help.

dave

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of
Thomas Berk
Sent: Wednesday, February 04, 2004 10:29 AM
To: 'Perl-Win32-Admin-Request (E-mail)'
Subject: Closing open file sessions on a server

Does anybody have an example of how to identify and close open file
locks.
I have an application that's locking a db file on a Windows 2000 server.
Users are inadvertently leaving themselves logged in over night
preventing the file from being backed up.

I realize that there are risks in clearing these sessions from the
server side, but I think the risk of missed backups at least warrants
some testing (on something other than the live application db).

I've thought of using "net file" and parsing the output to produce "net
file" commands to close the sessions.  Does anybody have a better idea?

Thomas

_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs




_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to