Match until new line

A

anon1m0us

I am running a server utility that allows me to view server shares and
permissions.

The command I am using is:
SHARES=Array.new
SHARES=`srvcheck \\\\testserver.split("\n")

This will change to be a variable.

the output is:

\\testserver1\test
domain\user1 Full Control
Domain\Admin Full Control
Domain\chuser Read
Everyone Read
domain\ysergeb Read

\\testserver2\BACKUPTEST
Everyone Change
BUILTIN\Administrators Read

\\testserver31\DEVQA$
Everyone Full Control

\\testsercver4\trymeout$
BUILTIN\Administrators Full Control



I need to match to a server and a specified share and print out all
the permissions just for that share. The .split("\n") spliuts it on
the newline, but how can I get all the information in between the new
lines.
I need \\testserver2 with all it's permissions and ignore the rest.
 
B

Brian Candler

the output is:

\\testserver1\test
domain\user1 Full Control
Domain\Admin Full Control
Domain\chuser Read
Everyone Read
domain\ysergeb Read

\\testserver2\BACKUPTEST
Everyone Change
BUILTIN\Administrators Read

\\testserver31\DEVQA$
Everyone Full Control

\\testsercver4\trymeout$
BUILTIN\Administrators Full Control



I need to match to a server and a specified share and print out all
the permissions just for that share. The .split("\n") spliuts it on
the newline, but how can I get all the information in between the new
lines.

split("\n\n") will divide the output into groups split by a blank line.
Then you can match on the start of the string to find the one you want.

#sharelist = `srvcheck \\\\testserver`
sharelist = <<EOS
\\\\testserver1\\test
domain\\user1 Full Control
Domain\\Admin Full Control
Domain\\chuser Read
Everyone Read
domain\\ysergeb Read

\\\\testserver2\\BACKUPTEST
Everyone Change
BUILTIN\\Administrators Read

\\\\testserver31\\DEVQA$
Everyone Full Control

\\\\testsercver4\\trymeout$
BUILTIN\\Administrators Full Control
EOS

def find_share(sharelist, match)
re = /^#{Regexp.escape(match)}$/
sharelist.split("\n\n").find { |s| re.match(s) }
end

puts find_share(sharelist, "\\\\testserver2\\BACKUPTEST")
 
P

Phrogz

I am running a server utility that allows me to view server shares and
permissions.

The command I am using is:
SHARES=Array.new
SHARES=`srvcheck \\\\testserver.split("\n")

A small note, but that first line is totally unnecessary. You don't
need to 'declare' a variable to be of a particular type before
assigning it. What you have above is similar to:

foo = 12
foo = 'hello world'

The value in your first assignment (a new, empty array instance) is
totally discarded when you then assign the SHARES constant to a
completely different array.
 
B

Brian Candler

def find_share(sharelist, match)
re = /^#{Regexp.escape(match)}$/
sharelist.split("\n\n").find { |s| re.match(s) }
end

puts find_share(sharelist, "\\\\testserver2\\BACKUPTEST")

Or more cryptically, but much faster as it's a single regexp scan and no
intermediate array is created:

def find_share(sharelist, match)
return $1 if /^#{Regexp.escape(match)}$\n(.*?\n)\n/m =~ sharelist + "\n"
nil
end

puts find_share(sharelist, "\\\\testserver2\\BACKUPTEST")

Note that ^ and $ match start and end of line anywhere within the string,
not just the start and end of the string.

Here find_share() also strips off the sharename, leaving just the users and
their rights, but you can change this by moving the capture parentheses in
the regexp if you wish.

B.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top