#121940 - 2004-06-29 11:46 AM
parse file
|
jechilt
Starting to like KiXtart
Registered: 2000-12-01
Posts: 102
Loc: Denver Colorado
|
Greetings,
I have been through the forum and have seen much of the information in the UDF. Unfortunately, my brain is just not successfully connecting the dots. I need to be able to do the following:
1. Get permissions to file 2. Determine if permissions are within specs - users permission must be "No Access" - username permission must be "No Access" 3. If permission is not "No Access", the username, machinename will be copied to a text file on the domain server for sysadmin to take action.
We have to be able to determine permissions on a file. No problem there...this is shell we use for the permission check: Code:
Function Tail($file,optional $lines, optional $skip) dim $fso, $f,$fs,$i,$all if not $lines $lines = 1 endif if not $skip $skip = 0 endif $fso = CreateObject("Scripting.FileSystemObject") $f = $fso.GetFile($file) if @error exit(2) endif $fs = $f.OpenAsTextStream(1) if $skip $fs.Skip($skip) endif $all = split($fs.Read($f.size-$skip),@crlf) if $lines = -1 redim preserve $all[ubound($all)-1] $tail = $all exit(0) endif dim $out[$lines] for $i = ubound($all)-$lines to ubound($all) $out[$i-(ubound($all)-$lines)] = $all[$i] next redim preserve $out[ubound($out)-1] $tail=$out Exit(0) endfunction $file = "c:\perm.txt" ; Start the process //////////////////////// SHELL "CMD.EXE /C perms /i users c:\winnt\system32\drivers\usbstor.sys > c:\perm.txt" for each $line in tail($file,-1,42) shell "cmd.exe /c echo users $line >> c:\perm1.txt" next SHELL "CMD.EXE /C perms /i %username% c:\winnt\system32\drivers\usbstor.sys > c:\perm.txt" for each $line in tail($file,-1,42) shell "cmd.exe /c echo %username% $line >> c:\perm1.txt" next del "c:\perm.txt"
; ///////////////////////
The output file looks like this: Code:
users perms: R-X---- jechilt perms: #RWXDPOA
I am thinking that the TRIM and SPLIT would allow the check of the permission after the ':' by assigning the permission as a variable and running a loop on it. My problem is my lack of experience to make this happen. Any suggestion or pointers in the right direction would be most kind.
Kind Regards...
_________________________
John LM Contractor One of the 2 dads
|
|
Top
|
|
|
|
#121941 - 2004-06-29 01:06 PM
Re: parse file
|
New Mexico Mark
Hey THIS is FUN
  
Registered: 2002-01-03
Posts: 223
Loc: Columbia, SC
|
You mean something like:
Code:
$File='c:\perml.txt' $RC=Open(1,$File) $Line=ReadLine(1) WHILE $Line <> '' AND @ERRORLEVEL=0 $aPerms=Split($Line,':') $sPerms=$aPerms[1] IF Perms($sPerms) 'Found perms' ? ELSE 'No perms found' ? ENDIF $Line=ReadLine(1) LOOP $RC=Close(1)
FUNCTION Perms($sPerms) DIM $i $sPerms=Trim($sPerms) FOR $i = 1 TO Len($sPerms) ' Check permissions one-by-one however you want IF SubStr($sPerms,$i,1) <> '-' $Perms=1 ENDIF NEXT ENDFUNCTION
|
|
Top
|
|
|
|
#121942 - 2004-06-29 01:44 PM
Re: parse file
|
jechilt
Starting to like KiXtart
Registered: 2000-12-01
Posts: 102
Loc: Denver Colorado
|
definitely looks condensed.
I get the following error and not sure why?
Code:
ERROR : missing ENDFUNCTION for [perms]!
EDIT: ok....syntax error on the check permission statement. changed to ; and now code runs but now no display...
Edited by jechilt (2004-06-29 01:59 PM)
_________________________
John LM Contractor One of the 2 dads
|
|
Top
|
|
|
|
#121943 - 2004-06-29 02:23 PM
Re: parse file
|
jechilt
Starting to like KiXtart
Registered: 2000-12-01
Posts: 102
Loc: Denver Colorado
|
so far so good....but now wonder how hard it would be to take this a step further. For each line, I should extract the username (user or jechilt) so we can then pass the username with the permission to the log file if the permission is out of spec.
NewMexicoMark...thanks for the help so far!!!
kind regards...
_________________________
John LM Contractor One of the 2 dads
|
|
Top
|
|
|
|
#121944 - 2004-06-29 02:50 PM
Re: parse file
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Looks like there is a missing information -
' Check permissions one-by-one however you want
Should it not be - Code:
' Check permissions one-by-one however you want'?
Thanks,
Kent
|
|
Top
|
|
|
|
#121946 - 2004-06-29 09:07 PM
Re: parse file
|
jechilt
Starting to like KiXtart
Registered: 2000-12-01
Posts: 102
Loc: Denver Colorado
|
thanks for the replies so far...
could i possibly get a little more help on parsing the file?
checking the permissions is pefect, but if the permission is not set to "no access" (which I have modified in my own script in the function), the username and permission needs to be passed to a text file on the server. My real stumbling block is how to get the username and permission passed to a text file in one smooth process.
_________________________
John LM Contractor One of the 2 dads
|
|
Top
|
|
|
|
#121948 - 2004-06-30 05:57 AM
Re: parse file
|
jechilt
Starting to like KiXtart
Registered: 2000-12-01
Posts: 102
Loc: Denver Colorado
|
hi....
i did get the format i wanted for the output: Code:
WHILE $Line <> '' AND @ERRORLEVEL=0 $aPerms=Split($Line,':') $sPerms=$aPerms[1] ;//get the username// $data=split($line,'') IF Perms($sPerms) ? $data[0] + ' has $sPerms permissions assigned' ELSE ? 'No perms found' ENDIF $Line=ReadLine(1) LOOP $RC=Close(1) I will use your suggestion to output to the datafile.
I was told this morning that there is an added requirement to the query (when is there not???)  We need to be able to determine if the USB device is enabled or disabled. I was able to use a WMI query that I found in the post that shows the USB devices (i.e. controllers), but I need to know if there is an easy way to flag the device if it is enabled which we then pass off to the datafile...
_________________________
John LM Contractor One of the 2 dads
|
|
Top
|
|
|
|
#121951 - 2004-07-02 12:09 PM
Re: parse file
|
jechilt
Starting to like KiXtart
Registered: 2000-12-01
Posts: 102
Loc: Denver Colorado
|
Greetings all,
I would like to thank you for your time and help. Below, you can see the script I ended up with. I am sure it can be much better but as a novice user, this was the result of compiling your suggestions, script inputs, UDF resources and a bit of head banging on the desk... Now, I have been asked to dump the data into a EXCEL spreadsheet...so back to the forum to search and hopefully it wont be too painful. Again, thanks for the input...here is the code we are using in the logon script: Code:
break on $file='c:\@wksta.ini' $tmpFile='c:\tmpFile.txt' $targetDrive='P:\COMSEC\dataCollection' $targetFile='$targetDrive\USBresults.ini' $failedFile='$targetDrive\USBfailed.txt' ;$targetFile='c:\USBresults.ini' ;$failedFile='c:\USBfailed.txt' $RC=Open(1,$File) $rdLine=ReadLine(7) $perms1='No Access' $perms2='-------'
? 'starting usbData.kix'
;################################################ ;## Function to Tail the file (unix style) Function Tail($file,optional $lines, optional $skip) dim $fso,$f,$fs,$i,$all if not $lines $lines = 1 endif if not $skip $skip = 0 endif $fso = CreateObject("Scripting.FileSystemObject") $f = $fso.GetFile($file) if @error exit(2) endif $fs = $f.OpenAsTextStream(1) if $skip $fs.Skip($skip) endif $all = split($fs.Read($f.size-$skip),@crlf) if $lines = -1 redim preserve $all[ubound($all)-1] $tail = $all exit(0) endif dim $out[$lines] for $i = ubound($all)-$lines to ubound($all) $out[$i-(ubound($all)-$lines)] = $all[$i] next redim preserve $out[ubound($out)-1] $tail=$out Exit(0) EndFunction
;################################################ ;## Function to read the data in the file Function ReadFile($file) Dim $lf, $f, $_, $t $lf=chr(10) $f=freefilehandle $_=open($f,$file) if @error exit @error endif do $t=$t+$lf+readline($f) until @error $_=close($f) $ReadFile=split(substr($t,2),$lf) EndFunction
;################################################ ;## Function to merge datafiles to the server Function MergeIni($SourceIni,$TargetIni,$Log) Dim $Section,$AllSections Dim $Key,$AllKeys,$Value,$_ ; Check if SourceIni exists.. If Exist($SourceIni)=1 ; Read all sections from Source ini file into AllSections array.. $AllSections=Split(ReadProfileString($SourceIni,"",""),Chr(10)) ; Read section subkeys.. For Each $Section In $AllSections ; Section not empty.. If $Section<>"" ; Local parameters.. $AllKeys=Split(ReadProfileString($SourceIni,$Section,""),Chr(10)) ; Read section subkeys.. For Each $Key In $AllKeys ; Key not empty.. If $Key<>"" $Value=ReadProfileString($SourceIni,$Section,$Key) $_=WriteProfileString($TargetIni,$Section,$Key,$Value) If $Log<>"" Shell "%ComSpec% /c echo - write $Key=$Value in [$Section]>>$Log" EndIf EndIf Next EndIf Next ; Merge succesfull.. Exit(1) Else ; Source INI file error.. Exit(0) EndIf EndFunction
;################################################ ; START THE SCRIPT
; Open file and set header $nul = open(1, $file, 5) $nul = writeline(1, "[@wksta]" + @crlf) $nul = close(1)
; Get the permissions for the USBSTOR.SYS file and strip it for the datafile SHELL "CMD.EXE /C perms /i users c:\winnt\system32\drivers\usbstor.sys > $tmpFile" for each $line in tail($tmpFile,-1,42) $aPerms=Split($line,':') $sPerms=$aPerms[1] $sPerms=trim("$sPerms") if $sPerms <> $perms1 writeprofilestring($file,@wksta,'users',$sPerms) else if $sPerms <> $perms2 writeprofilestring($file,@wksta,'users',$sPerms) endif endif next del $tmpFile
SHELL "CMD.EXE /C perms /i @userid c:\winnt\system32\drivers\usbstor.sys > $tmpFile" for each $line in tail($tmpFile,-1,42) $bPerms=Split($line,':') $tPerms=$bPerms[1] $tPerms=trim("$tPerms") if $tPerms <> $perms1 writeprofilestring($file,@wksta,@userid,$tPerms) else if $tPerms <> $perms2 writeprofilestring($file,@wksta,@userid,$tPerms) endif endif next del $tmpFile
SHELL "CMD.EXE /C perms /i administrators c:\winnt\system32\drivers\usbstor.sys > $tmpFile" for each $line in tail($tmpFile,-1,42) $cPerms=Split($line,':') $uPerms=$cPerms[1] $uPerms=trim("$uPerms") if $uPerms <> $perms1 writeprofilestring($file,@wksta,'administrators',$uPerms) else if $uPerms <> $perms2 writeprofilestring($file,@wksta,'administrators',$uPerms) endif endif next del $tmpFile
; Look for the USB device and list it if it is ACTIVE Dim $SO,$strComputer,$objWMIService,$colItems,$objItem $i = 1 $SO=SetOption('Explicit','On') $SO=SetOption('NoVarsInStrings','On') $strComputer = "." $objWMIService = GetObject("winmgmts:\\" + $strComputer + "\root\cimv2") $colItems = $objWMIService.ExecQuery("Select * from Win32_USBController",,48) For Each $objItem in $colItems if $objItem.ConfigManagerErrorCode = "0" $nul = open(1, $file, 5) $nul = writeline(1, "UsbController" + $i + "=" + $objItem.Description + @crlf) $nul = close(1) $i = $i + 1 else $nul = writeprofilestring($file,@wksta,'UsbController','NONE activated') endif Next
; Force a line feed at end of file $nul = open(1, $file, 5) $nul = writeline(1, @crlf) $nul = close(1)
; Merge the files Dim $MergeFile $MergeFile = MergeIni($file,$targetFile,'') If $MergeFile <> 0 $nul = open(8, $failedFile, 5) $nul = writeline(8, @wksta + @crlf) $nul = close(8) else ? 'File Merge Successful' Endif
; Force a line feed at end of targetFile $nul = open(9, $targetFile, 5) $nul = writeline(9, @crlf) $nul = close(9)
; Delete the file from the local workstation del $file
Kind Regards...
_________________________
John LM Contractor One of the 2 dads
|
|
Top
|
|
|
|
#121953 - 2004-07-06 10:34 AM
Re: parse file
|
jechilt
Starting to like KiXtart
Registered: 2000-12-01
Posts: 102
Loc: Denver Colorado
|
thanks....will take a look at dumping into a database.
kind regards///
_________________________
John LM Contractor One of the 2 dads
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 1046 anonymous users online.
|
|
|