Jump to content

Sorting a file


trp

Recommended Posts

Hi guys,

I am using Windows Server 2003, Apache and PHP5.

 

I have created a text file using 'nmap' the file looks like this:

Host: 192.168.0.1 ()	Status: Up
Host: 192.168.0.2 ()	Status: Down
Host: 192.168.0.3 ()	Status: Down

 

I am wanting to record ONLY the IP's that have the status of "Up" in a MySQL database.

 

If anyone can help me it would be really great!

 

Thanks,

 

TRP

Link to comment
https://forums.phpfreaks.com/topic/198529-sorting-a-file/
Share on other sites

I'd suggest oen step at a time, lets get this file into an array, one we can go through:

 

use file() it returns an array of lines.

then it is a very simple substr comparison: substr($line,-1) == "p" ...

 

then if thats true, add it to a new array $newarray[] = $line;

 

now you can do some advance stuff with the lines you wanted..

 

-cb-

Link to comment
https://forums.phpfreaks.com/topic/198529-sorting-a-file/#findComment-1041775
Share on other sites

Assuming you saved output from nmap to nmap.txt .. you can use below code to get the ip into $ip variable and then store it in mysql db, if you want.

 

<?php
$lines = file('nmap.txt');
foreach($lines as $line)
{
	if(strpos($line,"Up") !== false)
	{
		preg_match('/Host: (.*?) ()/',$line,$ip);
		$ip = $ip[1];
		echo $ip;
	}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/198529-sorting-a-file/#findComment-1041840
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.