Quicktip: grep and curl on Windows

I was recently asked by a Linux Engineer to take a script he had developed on RedHat that did a grep for a string, then returned the results into an array and splitted them using awk at each space. It put that result in a variable, then did a curl to to post it to a PHP page. It would be much easier if I had the original code to paste in, but I don’t so you’ll have to trust my word on it.

They wanted to take that script and make it work on a Windows box. Of course, it’s never that easy, even with Powershell, which is where I went to do the development of it.

First let’s break this script down into it’s base components:

  • We have a log file called results.log that we need to mine for a string called “Registration succeeded: /<clientname>”
  • Clientname is the string it registered with.
  • Unfortunately there’s no grep on Windows, so we need to do a findstr and returns the results to a variable:
$client_name=findstr /C:"Registration succeeded: /" "C:\results.log"
  • In our case that result returned an array, because there were multiple lines in the log file that had that string. We want the last one returned, so we use this nifty function.
  • We also need to split the string at the spaces so that we can just get the <clientname> returned. This isn’t really good code, but works for this very specific instance.
$clientname=$client_name[-1]| %{ $_.split(' ')[8]; }
  • We then need to declare our variables for the url and postdata

$url = "http://1.1.1.1/build/client.php"
$postdata = "clientname="+$clientname+"&ostype=windows"

  • This was actually the most difficult part of the script, partly I think due to the PHP file I was posting to. You have to create a new NET object and add headers and configure your encoding. Then you can post to the file

$wc = new-object system.net.WebClient
$wc.Headers.Add("Content-Type","application/x-www-form-urlencoded")
[byte[]]$byteArray = [System.Text.Encoding]::ASCII.GetBytes($postData)
$webpage = $wc.UploadData($url,"POST",$byteArray);

\

  • Then you can store the results in a variable in case you need to use them elsewhere

$response = [System.Text.Encoding]::ASCII.GetString($webpage)

Putting it all together:

$client_name=findstr /C:"Registration succeeded: /" "C:\results.log"<
$clientname=$client_name[-1]| %{ $_.split(' ')[8]; }

$url = "http://1.1.1.1/build/client.php"
$postdata = "clientname="+$clientname+"&ostype=windows"

$wc = new-object system.net.WebClient
$wc.Headers.Add("Content-Type","application/x-www-form-urlencoded")
[byte[]]$byteArray = [System.Text.Encoding]::ASCII.GetBytes($postData)
$webpage = $wc.UploadData($url,"POST",$byteArray);
$response = [System.Text.Encoding]::ASCII.GetString($webpage)

There you go!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s