New topic: 

Parsing shell output with RegEx/NthField?

<http://forums.realsoftware.com/viewtopic.php?t=47450>

         Page 1 of 1
   [ 2 posts ]                 Previous topic | Next topic          Author  
Message        p0wn3d          Post subject: Parsing shell output with 
RegEx/NthField?Posted: Sat Mar 30, 2013 8:25 am                                 
Joined: Sun Oct 28, 2012 4:54 am
Posts: 171
Location: Herts, UK                Hi All

I am using the following shell code to do an MX record lookup

dim sh as new shell
sh.execute("nslookup -querytype=mx bbc.co.uk")
dim s as string = sh.Result


OUTPUT AS FOLLOWS:

Non-authoritative answer: Server:  api.home
Address:  192.168.1.254

bbc.co.uk MX preference = 20, mail exchanger = cluster1a.eu.messagelabs.com
bbc.co.uk MX preference = 10, mail exchanger = cluster1.eu.messagelabs.com


I was just wondering if anyone can suggest a quick and slick way of parsing 
just the MX hostname (cluster1a.eu.messagelabs.com) from the output?

Thanks and happy easter      
_________________
Real Studio 2012 R2
SysInfo
BackTrack Linux/BackBox Linux/Debian Lenny/Windows 7/Windows 8/OpenWRT/OpenBSD  
                             Top                harriew          Post subject: 
Re: Parsing shell output with RegEx/NthField?Posted: Sat Mar 30, 2013 11:16 am  
                       
Joined: Fri Dec 01, 2006 3:09 pm
Posts: 640
Location: Tennessee, USA                I am not a RegEx guru so here is a way 
to do it without the RegEx.  The desired result ends up in the variable result. 
This code was displaying the original shell output in a multi-line label field 
and the final result in another label field as I was testing the code. You can 
massage it to get what you want in your code. You should probably test the 
result variable upon exiting the loop. If it is empty then cluster1a was not 
found.
Dim sh as New Shell
Dim s As String
dim shellOutput() As String
dim result As String
Dim i, num, pos As int32

sh.execute("nslookup -querytype=mx bbc.co.uk")
s = sh.Result
shellOutput = split(s, EndOfLine.UNIX)  // split lines into string array
num = UBound(shellOutput)  // last index for array
for i = 0 to num  // walk through array
  pos = instr(shellOutput(i), "cluster1a")  // cluster1a in this line?
  if pos > 0 then  // yes
  result = mid(shellOutput(i), pos)  //  get desired text
  exit for i  // stop searching, we have the line
  end if
next i

lblResults.Text = result
lblCmdOut.Text = s

I was running this on Mac. On windows I do not know if endofline.unix is the 
proper endofline string to use for splitting the output from the shell since it 
looks like you are not running on a Mac.   
                             Top             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 2 posts ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to