0

My company is renting lot's of IP's. And our clients are mostly mailers. After IP lease, sometimes they make IP's unusable, because IP's gets blocked at certain providers, like Yahoo, Gmail, AOL etc.

Currently, we make a simple check this by binding the IP and running telnet command from our CentOS systems like the this:

telnet -b $IP smtp.gmail.com 25

And get the output, if it is 220 for OK, or something else that would indicate that IP is not dirty.

What I am looking for is a PHP script, or first the idea how this can be done. Basically, we would bind IP's, and PHP will check the output of the connection and write to a csv file for example.

I've done lots of php projects in the past, but nothing similar. Any idea from where to start? Thanks for suggestions.

EDIT

Due my investigations, I am able to do something like this,

    $eol= system("nc  -4 -w 2 -s $Binded_IP smtp.gmail.com 25", $output );
var_dump($output); 

and get the output like:

-bash-4.2$ php script.php 

220 smtp.gmail.com ESMTP 13sm2795405wml.25 - gsmtp

I am trying with netcat instead telnet. This is the meaning of the arguments, to those who aren't familiar with netcat:

-4 to bind only IPv4
-s Source IP to bind to
-w 2  Timeout

But the problem is I can not find a way to close that connection, it only hangs out, and remain open. If I find a way to close that connection, I could store $output in the file, filter SMTP status code, and continue with second IP and check it.

fugitive
  • 357
  • 2
  • 8
  • 26
  • Can't you just run this via PHPs exec()? Or, probably better, just do a bash-script that runs through IPs in a text-file or something? – junkfoodjunkie Apr 11 '17 at 13:34
  • @junkfoodjunkie yes, I considered bash as well, but got stucked how to terminate and get a output of existing connection – fugitive Apr 11 '17 at 13:36
  • this script here should help http://stackoverflow.com/questions/3675897/how-to-check-by-php-if-my-script-is-connecting-to-smtp-server – unixmiah Apr 11 '17 at 14:48

1 Answers1

0

You can use the following script to check if there is a connection to the server.

$f = fsockopen('smtp smtp.gmail.com', 25) ;
if ($f !== false) {
    $res = fread($f, 1024) ;
    if (strlen($res) > 0 && strpos($res, '220') === 0) {
        echo "Success!" ;
    }
    else {
        echo "Error: " . $res ;
    }
}
fclose($f) ;
unixmiah
  • 3,081
  • 1
  • 12
  • 26
  • yes, but it will send a request through main ip, not binded one. F.e. main IP of the server is `192.168.1.1` and I want to test `10.0.0.0/24` subnet. So I will bind `10.0.0.0/24 to my interface and then I will use those IP's to do requests. Ofc, those are private IP's, I gave just the example. – fugitive Apr 11 '17 at 15:09
  • try this, i thought this would be useful http://demo.phpipam.net/login/ https://github.com/phpipam/phpipam – unixmiah Apr 11 '17 at 15:29