Jump to content

curmudgeonly-grump

New Members
  • Posts

    6
  • Joined

  • Last visited

curmudgeonly-grump's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Using $notworking = preg_match_all("/not/i", $output, $match, PREG_SET_ORDER); I am able to return the result "not" in a array. How do I get it to return the whole string this match was contained in or the indexed position of the element.
  2. 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> "; } }
  3. 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 }
  4. Update I made a change to the code so that It uploads the file (which works) then I use $csv= $_FILES["uploaded"]; $output=shell_exec("powershell -executionPolicy Unrestricted D:/Web/scripts/PHPfwrules.ps1".$csv."< NUL"); echo '<pre>'; print_r($output); echo '<h6>'; if ( $output== "destination is reachable on port using protocol") { echo "Destination IS reachable on the port using the protocol"; } else { echo "Destination is NOT reachable on the port using the protocol"; } echo '</h6>'; Now I receive: The term 'D:/Web/scripts/PHPfwrules.ps1Array' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. jcbones said the string would be read "powershell -executionPolicy Unrestricted D:/Web/scripts/phpfwrules.ps1Array< NUL" Am I receiving this error as I am passing it as an array does the file need to be read and the output stored as a string?
  5. Thanks for the advice, sorry for the code indentation new to the forum and new to PHP. The issue is that it does not display $output. Upon asking it to print_r($output); I receive errors like Access to the path 'C:\etc\company\ports\NOTworking.txt' is denied. AND Import-Csv : Cannot bind argument to parameter 'Path' because it is an empty string. Am I to assume it is an empty string as the file was not passed correctly.
  6. Hi I am trying to pass a csv file into a powershell script and then return the output and display it to the user. The file is a firewall checker. Any advice or solutions on where I am going wrong would be most appreciative, Thanks. <?php //phpinfo(); ini_set('display_errors', 'On'); error_reporting(E_ALL);//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 upload(){ 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>'; #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'); //$proxyserver = 'smcdlabdev1'; $proxyserver = 'smd1utweadm0001'; $masterserver = 'smd1utweadm0001'; foreach ($txtarea as $line => $servername) { //$line => $servername; ##check if querying itself.... } if(isset($_FILES["filecsv"])) { $csv= $_FILES["filecsv"]; $output=shell_exec("powershell -executionPolicy Unrestricted D:/Web/scripts/phpfwrules.ps1".$csv."< NUL"); echo '<pre>'; echo '<h6>'; if ( $output== "The destination is reachable on port using the protocol") { echo "Destination IS reachable on port using protocol"; } else { echo "Destination is NOT reachable on port using protocol"; } echo '</h6>'; 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> <h3>Firewall Implementation </h3> <hr /> <table class=results> <tr> <th>Server Name</th> <th>Firewall Acceptance</th> </tr> <tr> <td>'.$servername.'</td> <td>'.$output.'</td> <td></td> '; print_r($output); } } 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"> <textarea name=ServerList id=ServerList> </textarea> <h5>Please select a CSV file.</h5> <input type="file" id="filecsv"> <input type="hidden" name="filecsv"> <br> <button onclick=\'upload()\'> Submit </button </html> ' ; ?> And here is the powershell 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:\etc\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 "The destination is reachable on port using the protocol" | out-file -filepath $outputfileworking -append } else { $failure +="The destination is not reachable on port using the protocol" } } if ($failure) { echo "ServerName:$computer" | out-file -filepath $outputfileNOTworking echo $failure | out-file -filepath $outputfileNOTworking -append }
×
×
  • 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.