Jump to content

Preg_grep find a partial match in a array of strings then compare to the original and display the output


curmudgeonly-grump

Recommended Posts

Hi I am trying to search an array that comes from a power shell script, the array of strings that returns is ever changing as it receives variables from the script. Therefore I need to use preg_grep to search for the word "not" I am also using another array which inverts this I then need to compare these new two arrays to the original and separate the results $working and not working into a table. I would be most grateful for any advice/solutions. The preg_grep aren't displaying anything. 

<?php

//phpinfo();

ini_set('display_errors', 'On');

error_reporting(0);//E_ALL

echo '<html>

<style>

body

{

font-family:Calibri,Helvetica,sans-serif;

font-size:100%;

}

</style>

 

<head>

<link rel="stylesheet" type="text/css" href="style.css">

 

<script type="text/javascript">

function uploadJS(){

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };

var textAreaValue=document.getElementById(\'ServerList\').value;

var trimmedTextAreaValue=textAreaValue.trim();

if(trimmedTextAreaValue!="") {

document.myForm.submit();

} else{

alert("Server List Text Area is Empty");

}

};

var input=document.getElementById(\'fileSelect\').value;

if(input!="") {

document.myForm.submit();

} else{

alert("You have not selected a file, please select one to proceed.");

}

 

    var handleFileSelect = function(e) {

        var files = e.target.files;

        if(files.length === 1) {

        document.forms.myForm.filecsv.value = files[0].name;

        }

    }

                };

               

</script>

</head>';

 

#Upload Code

$target = "D:\Web\Upload/";

  $target = $target . basename( $_FILES['uploaded']['name']) ;

  $ok=1;

  if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))

  {

  echo "The file ". basename( $_FILES['uploadedfile']['name']). "

  has been uploaded";

  }

  else {

 

  }

 

  if ($uploaded_size > 150000)

  { echo "Your file is too large.<br>";

  $ok=0;

  }

 

  if ($uploaded_type =="text/php")

  {

  echo "No PHP files<br>";

  $ok=0;

  }

if (!($uploaded_type=="text/csv")) { echo "<br>";

 $ok=0;

}

 

 $target = "upload/";  $target = $target . basename( $_FILES['uploaded']['name']) ;  $ok=1;   //This is our size condition

if ($uploaded_size > 350000)

{

echo "Your file is too large.<br>";

$ok=0;

}

 //This is our limit file type condition

if ($uploaded_type =="text/php")

 {  echo "No PHP files<br>";

$ok=0;

}

 //Here we check that $ok was not set to 0 by an error

if ($ok==0)

{

 }

//If everything is ok we try to upload it

 else  {

 if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))

{

 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";

}  else

 }

 }

 

#Start of scripts to check Servers in textbox

  if(isset($_POST['ServerList']))

                {

               

                //$arry=explode( "\r\n", $_POST['ServerList'] );

                $txttrim = trim($_POST['ServerList']);

                //$textAr = explode("\n", $text);

                //$textAr1 = array_filter($text, 'trim'); // remove any extra \r characters left behind

                $txtarea = explode("\n",$txttrim);

                $txtarea = array_filter($txtarea,'trim');

               

               

                foreach ($txtarea as $line => $servername)

                {             

                                //$line => $servername;

                                ##check if querying itself....

                }             

               

 

 

 

  echo '<pre>';

    $target="D:/Web/Upload/"; $target= $target . basename( $_FILES['uploaded']['name']) ;

    $csv = $target;

                //$filename= $_FILES['uploaded']['name'];

                $output=shell_exec("powershell -Command D:/Web/scripts/PHPfwrules.ps1 $csv < NUL");

                echo '<h6>';

                print_r($output);

                $nomatch=preg_grep('/^not/i',$output);

                echo $nomatch;

                $match=preg_grep("/not/",$output,PREG_GREP_INVERT);

                echo $match;

                if ($working1= array_intersect($output,$match))

                                {

                echo"<link rel=stylesheet type=text/css href=style.css>

                <table class=results>

                                <tr>

                                <th>Server Name</th>

                                <th>Working</th>

                                </tr>

                                <tr>

                                <td>".$servername."</td>

                                <td>".$working1."</td>

                                </tr></table>

                                ";

}

else if ($notworking1= array_intersect($output,$nomatch))

{

                echo"<link rel=stylesheet type=text/css href=style.css>

                <table class=results>

                                <tr>

                                <th>Server Name</th>

                                <th>Not Working</th>

                                </tr>

                                <tr>

                                <td>".$servername."</td>

                                <td>".$notworking1."</td>

                                </tr></table>

";

}

}

                               

                                echo' <link rel=stylesheet href=dhtmlwindow.css type=text/css />

                                                <link rel=stylesheet type=text/css href=style.css>

                                                <script src=js/dhtmlwindow.js></script>

                                                <hr />

                                                <table class=results>

                                                <tr>

                                                <th>Server Name</th>

                                                <th>Working</th>

                                                <th>  Not Working</th>

                                                </tr>

                                                <tr>

                                                <td>'.$servername.'</td>

                                                <td>'.$working1.'</td>

            <td>'.$notworking1.'</td>

 

                                               

                                                <td></td>

                                                ';             

 

echo '</pre>';

 

echo '

<h3>Firewall Implementation </h3>

<h4>Please enter the server below, you can only select one server at a time. </h4>

<!--The form-->

<form action="fw2.php" method="post" name="myForm" id="myForm" enctype="multipart/form-data">

<textarea name=ServerList id=ServerList>

</textarea>

<h5>Please select a CSV file.</h5>

    <input name="uploaded" type="file" /><br />

                <input type="submit" value="Submit" />

<br>

</html> '

;

?>

 

 
Here is the power shell script:

#### Set Parameter for the input filename ####

Param(

  [Parameter(

#  Mandatory = $true,

  ParameterSetName = '',

  ValueFromPipeline = $true)]

  [array]$Filename

  )

 

 

#### Check if files already exist ####

if (test-path c:\ec\company\ports\working.txt)

{

Remove-item c:\etc\company\ports\working.txt

}

if (test-path c:\etc\company\ports\NOTworking.txt)

{

Remove-item c:\etc\company\ports\NOTworking.txt

}

#### Create Directory if it does not exist ####

if ((test-path c:\etc\company\ports) -eq $false)

{

New-Item -ItemType directory -Path C:\etc\company\Ports

}

 

#### Output filenames ####

#### Declaring the failure variable as an array ####

$failure = @()

$computer = gc env:computername

$outputfileworking = "C:\etc\company\ports\working.txt";

$outputfileNOTworking = "C:\etc\company\ports\NOTworking.txt";

#### Output servername to the output file ####

echo "ServerName:$computer" | out-file -filepath $outputfileworking

 

$path = "$Filename"

$csv = Import-csv -path $path

ForEach($line in $csv)

{

    $destination = $line.destination

    $protocol = $line.protocol

    $port = $line.port

 

    $result = ./portqry.exe -n $destination -e $port -p $protocol

    if ($result -like "*: LISTENING*")

    {

    Echo "$destination is reachable on port $port using $protocol"

    }

    else

    {

   Echo "$destination is not reachable on port $port using $protocol"

    }

 

}

if ($failure)

{

echo "ServerName:$computer" | out-file -filepath $outputfileNOTworking

echo $failure | out-file -filepath $outputfileNOTworking -append

}

Edited by curmudgeonly-grump
Link to comment
Share on other sites

Essentially this is the code that I am having issue with the rest is just for further reference. Output displays correctly and with the intended result coming from powershell thischanges based on the inputted CSV file . So it will display a mixture of "$destination is reachable on port $port using $protocol" and "$destination is not reachable on port $port using $protocol". So the keyword I am trying to search for with preg_grep is "not" and another preg_grep variable with "not" but inverted. Any reasons why the code below doesn't work? Thanks.

 


 

  echo '<pre>';

    $target="D:/Web/Upload/"; $target= $target . basename( $_FILES['uploaded']['name']) ;

    $csv = $target;

                //$filename= $_FILES['uploaded']['name'];

                $output=shell_exec("powershell -Command D:/Web/scripts/PHPfwrules.ps1 $csv < NUL");

                echo '<h6>';

                print_r($output);

                $nomatch=preg_grep('/^not/i',$output);

                echo $nomatch;

                $match=preg_grep("/not/",$output,PREG_GREP_INVERT);

                echo $match;

                if ($working1= array_intersect($output,$match))

                                {

                echo"<link rel=stylesheet type=text/css href=style.css>

                <table class=results>

                                <tr>

                                <th>Server Name</th>

                                <th>Working</th>

                                </tr>

                                <tr>

                                <td>".$servername."</td>

                                <td>".$working1."</td>

                                </tr></table>

                                ";

}

else if ($notworking1= array_intersect($output,$nomatch))

{

                echo"<link rel=stylesheet type=text/css href=style.css>

                <table class=results>

                                <tr>

                                <th>Server Name</th>

                                <th>Not Working</th>

                                </tr>

                                <tr>

                                <td>".$servername."</td>

                                <td>".$notworking1."</td>

                                </tr></table>

";

}

}

                               

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.