maihabunash
maihabunash

Reputation: 1702

groovy + how to match line from parameter

I run some external command from my groovy script

I print from my groovy script the external command results by this:

 println "stdout: ${proc.in.text}"   )

.

my full groovy script

  path='C:/Program Files/Systems/VPN/vpnclient.exe'


  NAME = "JGSVGVGBGVG"
  PASS = "JHBYGTGFBV"
  USER = "HBTFTNI"


  def command = """ $path connect user $USER pwd $PASS "$NAME"  """
  def proc = command.execute()                 

  proc.waitFor()                               

 // Obtain status and output
 println "return code: ${ proc.exitValue()}"
 println "stderr: ${proc.err.text}"
 println "stdout: ${proc.in.text}" 

groovy script print part the following output:

 .
 .
 .
 Authenticating user.
 Negotiating security policies.
 Securing communication channel.
 Your VPN connection is secure.

Until now everything is OK

Now I want to match the line “Your VPN connection is secure” from results

So I create the following code in groovy and add this code to my original groovy script: ( in order to match the line ) ,

  line= 'Your VPN connection is secure.'
  def matcher = ${proc.in.text}.matcher(line)
  def count = matcher.getCount()
  println "Matches = ${count}"

I get “Exception” , I am very new in groovy I not understand what wrong in my code And what need to fix in order to match the line - Your VPN connection is secure

Please help

Upvotes: 0

Views: 463

Answers (1)

Will
Will

Reputation: 14539

You have a GString dollar syntax which should be between double quotes, but is not needed in this case. You can just use the string matcher operator, =~.

I wrote a small mock (with the proc=[in:[text:) just to make the assertion work, it is not needed in your script:

proc=[in:[text:
  '''
    Authenticating user.
    Negotiating security policies.
    Securing communication channel.
    Your VPN connection is secure.
'''
]]
line= 'Your VPN connection is secure.'
def matcher = proc.in.text =~ line
def count = matcher.getCount()
assert count == 1

Update: you must not use the proc.in.text twice, you need to store its results in a variable and then reread the variable:

path='C:/Program Files/Systems/VPN/vpnclient.exe'

NAME = "JGSVGVGBGVG"
PASS = "JHBYGTGFBV"
USER = "HBTFTNI"

def command = """ $path connect user $USER pwd $PASS "$NAME"  """
def proc = command.execute()                 

proc.waitFor()                               

def output = proc.in.text

// Obtain status and output
println "return code: ${ proc.exitValue()}"
println "stderr: ${proc.err.text}"
println "stdout: ${output}" 

def matcher = (output =~ 'Your VPN connection is secure.')
def count = matcher.count

Upvotes: 2

Related Questions