0

I am logging outgoing traffic in a text file, which consists of ip & port. after copying all logs to text file and further copying its content to php string, I need to show all ips.

Code:

<?php
function get_string_between($string, $start, $end){
    $string = ' ' . $string;
    $ini = strpos($string, $start);
    if ($ini == 0) return '';
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);
}

$fullstring = "[IP]49.44.50.18[/IP][PORT]80[/PORT] [IP]204.79.197.200[/IP][PORT]80[/PORT] [IP]49.44.50.18[/IP][PORT]80[/PORT] [IP]204.79.197.203[/IP][PORT]80[/PORT] [IP]204.79.197.200[/IP][PORT]80[/PORT] [IP]49.44.50.18[/IP][PORT]80[/PORT] [IP]204.79.197.203[/IP][PORT]80[/PORT] [IP]204.79.197.200[/IP][PORT]80[/PORT] [IP]49.44.50.18[/IP][PORT]80[/PORT] [IP]111.221.29.30[/IP][PORT]80[/PORT] [IP]23.99.125.55[/IP][PORT]80[/PORT] [IP]103.243.221.87[/IP][PORT]80[/PORT] [IP]107.21.211.226[/IP][PORT]80[/PORT] [IP]49.44.118.224[/IP][PORT]443[/PORT] [IP]49.44.50.9[/IP][PORT]80[/PORT] [IP]204.79.197.200[/IP][PORT]80[/PORT] [IP]46.137.205.158[/IP][PORT]80[/PORT] [IP]46.137.205.249[/IP][PORT]443[/PORT] [IP]204.79.197.200[/IP][PORT]80[/PORT] [IP]13.107.5.80[/IP][PORT]80[/PORT] [IP]103.243.221.87[/IP][PORT]443[/PORT] [IP]204.79.197.203[/IP][PORT]80[/PORT] [IP]204.79.197.200[/IP][PORT]80[/PORT] [IP]49.44.50.18[/IP][PORT]80[/PORT] [IP]175.41.140.23[/IP][PORT]443[/PORT] [IP]144.2.1.1[/IP][PORT]443[/PORT] [IP]111.221.29.30[/IP][PORT]80[/PORT] [IP]23.99.125.55[/IP][PORT]80[/PORT] [IP]103.243.221.87[/IP][PORT]80[/PORT] [IP]107.21.211.226[/IP][PORT]80[/PORT] [IP]49.44.118.224[/IP][PORT]443[/PORT] [IP]49.44.50.9[/IP][PORT]80[/PORT] [IP]204.79.197.200[/IP][PORT]80[/PORT] [IP]46.137.205.158[/IP][PORT]80[/PORT] [IP]46.137.205.249[/IP][PORT]443[/PORT] [IP]204.79.197.200[/IP][PORT]80[/PORT] [IP]13.107.5.80[/IP][PORT]80[/PORT] [IP]103.243.221.87[/IP][PORT]443[/PORT] [IP]204.79.197.203[/IP][PORT]80[/PORT] [IP]204.79.197.200[/IP][PORT]80[/PORT] [IP]49.44.50.18[/IP][PORT]80[/PORT] [IP]175.41.140.23[/IP][PORT]443[/PORT] [IP]144.2.1.1[/IP][PORT]443[/PORT] [IP]111.221.29.30[/IP][PORT]80[/PORT] [IP]23.99.125.55[/IP][PORT]80[/PORT] [IP]103.243.221.87[/IP][PORT]80[/PORT] [IP]107.21.211.226[/IP][PORT]80[/PORT]";
$parsed = get_string_between($fullstring, '[IP]', '[/IP]');

echo $parsed;
?>

Output:

49.44.50.18

How to show all IP's

Divyam Solanki
  • 461
  • 2
  • 7
  • 25

2 Answers2

1

Try:

preg_match_all('#\[IP\]([^\[]+)\[/IP\]#', $fullstring, $ips);
var_dump($ips[1]);
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
Robert
  • 372
  • 1
  • 4
  • 11
0
The below code may help you
<?php
function get_all_ips($string){

    $AllIP = explode(' ',$string);

    $FilteredIPs = array();

    foreach($AllIP as $IP){

        preg_match('/[0-9\.]+/',$string,$output);

        if(filter_var($output[0],FILTER_VALIDATE_IP)){

            $FilteredIPs[] = $output[0];
        }
    }

    return $FilteredIPs;   
}
$parsed = get_all_ips($fullstring);
var_dump($parsed);
?>