well i'm new of php and i have this problem....
<?php
$RANGE = 192.168.1.1/254;
for in $RANGE
do
count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if ! ping -c $COUNT $myhost; then
# 100% failed
fi
fi
done
echo "Host : $myHost is down (ping failed) at $(date)" | mail -s "$SUBJECT" $EMAILID
the idea is :
1: ping the Range
2: mount an eventually server (if is alive)
3: send me an email
4: put all host alive in db
anyone can help me?!
tnx in advance
You can check hosts and ports of hosts with fsockopen function
$hosts = array(/* array of hosts list */)
foreach ($hosts as $host) {
$hostname = $host;
$port = 80;
$timeout = 3;
$fp = fsockopen ($hostname, $port ,$errno ,$errstr, $timeout);
if($fp) {
// Port is alive
// Mount, send an email, insert to db
} else {
// Port is dead. Reason : $errstr
}
}
Guess you could try something like this. Be warned though, I have not tested this code.
$mainpart = "192.168.1.";
$errors = array();
foreach(range(1, 254) as $ip) {
$adr = $main . $ip;
$msg = exec("ping {$adr} blablabla");
if($msg == "bad error") {
$errors[$adr] = $msg;
}
}
Might need some sort of timeout for each loop. At the end you can loop through the errors-array to handle each error given. If possible I would consider using fsockopen to check on ports instead of pingcommands like Osaman recommends.
Related
I am trying to force a php script (currently on XAMPP, soon on a dedicated server) to use a certain proxy for outgoing traffic.
On this site i found the following solution for it :
stream_context_set_default(['http'=>['proxy'=>'ip:port']]);
how do i verify that my script is actually using that proxy?
thanks in advance
You can use fsockopen for this, where you can specify a timeout.
A simple proxy list checker. You can check a list ip:port if that port is opened on that IP.
<?php
$fisier = file_get_contents('proxy_list.txt'); // Read the file with the proxy list
$linii = explode("\n", $fisier); // Get each proxy
$fisier = fopen("bune.txt", "a"); // Here we will write the good ones
for($i = 0; $i < count($linii) - 1; $i++) test($linii[$i]); // Test each proxy
function test($proxy)
{
global $fisier;
$splited = explode(':',$proxy); // Separate IP and port
if($con = #fsockopen($splited[0], $splited[1], $eroare, $eroare_str, 3))
{
fwrite($fisier, $proxy . "\n"); // Check if we can connect to that IP and port
print $proxy . '<br>'; // Show the proxy
fclose($con); // Close the socket handle
}
}
fclose($fisier); // Close the file
?>
you may also want to use set_time_limit so that you can run your script for longer.
Code taken from: http://www.php.net/manual/en/function.fsockopen.php#95605
Just for 1 proxy :
function test()
{
$fisier = fopen("bune.txt", "a"); // Here we will write the good ones
if($con = #fsockopen($IP, $port, $eroare, $eroare_str, 3))
{
fwrite($fisier, $proxy . "\n"); // Check if we can connect to that IP and port
print $proxy . '<br>'; // Show the proxy
fclose($con); // Close the socket handle
}
}
Finding the local area network IP and MAC address using PHP code.
Below code is working on Windows. But didn't work on Raspberry Pi. Please help me.
$ipadd = gethostbyname(trim(`hostname`));
echo $ipadd."<br>";
$add = explode(".",$ipadd);
array_pop($add);
$addre = implode(".",$add);
$address = $addre.".";
$i = 1;
while($i < 254)
{
pingAddress($address.$i,$ipadd);
$i++;
}
function pingAddress($ip,$dip)
{
$pingresult = exec("ping -n 1 -w 1 $ip", $outcome, $status);
if (0 == $status)
{
if ($ip != $dip)
{
$cmd = "arp -a " . $ip;
$macadd = exec($cmd);
$str = $macadd;
$mac = explode(" ",$str);
echo $ip." - ".$mac[11]."<br>";
}
else
{
$string = exec('getmac');
$mac = substr($string, 0, 17);
echo $ip." - ".$mac."<br>";
}
}
else
{
}
}
Your Pi is probably running *nix and ping has a slightly different set of flags.
I assume your using -n to say just 1 ping, on *nix it's -c. You could miss this parameter out and just use the -w to limit the pings. So code is...
$pingresult = exec("ping -w 1 $ip", $outcome, $status);
On Raspberry Pi zero W and PHP7, i use (for onboard wifi adaptator) :
//get mac address
$mac=file_get_contents("/sys/class/net/wlan0/address");
echo "mac=".$mac;
I have a simple PHP script setup to check the status of my servers. It uses a standard ping command, run via exec().
On Windows, the script works fine both when a server is online and
when down.
On Linux, the script works when the server is online, but hangs when
the server is down. Timeout seems to have no effect on the latter.
Pinging with the same command via console works fine and times out correctly.
What's the cause, and how would this be fixed?
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$exec_string = 'ping -n 1 -i 255 -w 2 ' . $host;
} else {
$exec_string = 'ping -n -c 1 -t 255 -w 2 ' . $host;
}
exec($exec_string, $output, $return);
I would avoid pinging directly using an exec(). I use this script, found here, you can also setup port and timeout:
function ping($host, $port = 80, $timeout = 6) {
$fsock = fsockopen($host, $port, $errno, $errstr, $timeout);
if (!$fsock) {
return false;
} else {
return true;
}
}
$host = 'www.example.com';
if(ping($host)) {
echo "HOST UP";
} else {
echo "HOST DOWN";
}
I'm unsure why, but switching from suPHP to fastCGI (both with suEXEC enabled) seemed to resolve the issue and the ping properly times out as expected.
If anyone has an explanation for this, I would love to know, in either comment or answer format.
Here is the issue. I'm working on a tracking script with a China tracking unit (not important). The two files I have is as follows:
7778.php
#!/usr/bin/php -q
<?php
error_reporting(0);
set_time_limit(0);
$address = 'SERVER IP';
$port = 7778;
$q_count = 1;
if (($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0)
{
echo "socket_create() failed, reason: " . socket_strerror($master) . "\n";
}
socket_set_option($master, SOL_SOCKET,SO_REUSEADDR, 1);
if (($ret = socket_bind($master, $address, $port)) < 0)
{
echo "socket_bind() failed, reason: " . socket_strerror($ret) . "\n";
}
if (($ret = socket_listen($master, SOMAXCONN)) < 0)
{
echo "socket_listen() failed, reason: " . socket_strerror($ret) . "\n";
}
$read_sockets = array($master);
//---- Create Persistent Loop to continuously handle incoming socket messages ---------------------
while (true)
{
$changed_sockets = $read_sockets;
$num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL);
foreach($changed_sockets as $socket)
{
if ($socket == $master)
{
if (($client = socket_accept($master)) < 0)
{
echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
continue;
}
else
{
array_push($read_sockets, $client);
print "[".date('Y-m-d H:i:s')."] ".$client." CONNECTED "."(".count($read_sockets)."/".SOMAXCONN.")\r\n";
}
}
else
{
$bytes = #socket_recv($socket, $buffer, 2048, 0);
if ($bytes == 0)
{
$index = array_search($socket, $read_sockets);
unset($read_sockets[$index]);
if (socket_close($client))
{
print "Connection closed\r\n";
}
}
else
{
print $buffer."\r\n";
}
print "All Done\r\n";
print "---------------------------------------------------------------------------------------------------------\r\n";
$index = array_search($socket, $read_sockets);
unset($read_sockets[$index]);
if (socket_close($client))
{
print "Connection closed\r\n";
}
}
}
}
?>
and then
port_start.sh with code:
#!/bin/sh
cd /home/path/to/script/
if netstat -tulpn | grep 7778 > /dev/null
then
echo 7778 - Online
else
./7778.php
fi
Now what I would do to test the script is ssh onto my server, navigate to the file directory and type "./7778.php". This will then start my php file, which will open port 7778, my unit will then connect, and my script will dump the data/buffer on the screen. Even if I start the script from the .sh file by typing the following "./port_start.sh" everything works perfect. I can also use Putty and open a RAW connection to my server and the port, and it works. No how it must work is as follows. The unit will make a connection, the script will accept it and receive the data, and then close the connection. My problem occurs when I open the port by running the .sh file from my cronjob with the following code "*/1 * * * * /home/path/to/file/port_start.sh > /dev/null". It will open it, and I can verify that its open, but nothing can connect to it. Why would that be?
When working from cronjob, computer cannot locate the command program. If the program inside /home/root/bin directory, you should call it like/home/root/bin/netstat ... you can locate the progrma by running "locate netstat" and "locate grep". This may solve your problem.
Thank you so much Isa for your response. In my case this was not the problem. Everything opens and works as it should. But I have a little script to check if the port is open. My problem happened there. When I ran the script, it would keep the port open but would kill my script. That's why I could see that the port was still open but nothing would be happening.
The script I use to check the port is as follows if someone ever sits with the same problem:
$errno = "";
$errstr = "";
$fp = #fsockopen($domain, $port, $errno, $errstr, 2);
$status = "";
if (!$fp) {
$status = "<img src='red.gif' alt='Status: Down, Domain: $domain, Port: $port ($errstr)'>\n";
}
else
{
$status = "<img src='green.gif' alt='Status: Up, Domain: $domain, Port: $port'>\n";
}
#fclose($fp);
But again Isa your response made sense but didn't apply to me
I have a list of proxies, need to use them in a php script I am writing.
How can I test that the proxy will work before I use it? is that possible? at the moment if the proxy doesn't work the script just dies after 30 seconds. Is there a quicker way to identify whether it will work or not?
perhaps create a socket connection? send something to it that will reveal whether the proxy is open?
thanks
you can use fsockopen for this, where you can specify a timeout.
A simple proxy list checker. You can check a list ip:port if that port is opened on that IP.
<?php
$fisier = file_get_contents('proxy_list.txt'); // Read the file with the proxy list
$linii = explode("\n", $fisier); // Get each proxy
$fisier = fopen("bune.txt", "a"); // Here we will write the good ones
for($i = 0; $i < count($linii) - 1; $i++) test($linii[$i]); // Test each proxy
function test($proxy)
{
global $fisier;
$splited = explode(':',$proxy); // Separate IP and port
if($con = #fsockopen($splited[0], $splited[1], $eroare, $eroare_str, 3))
{
fwrite($fisier, $proxy . "\n"); // Check if we can connect to that IP and port
print $proxy . '<br>'; // Show the proxy
fclose($con); // Close the socket handle
}
}
fclose($fisier); // Close the file
?>
you may also want to use set_time_limit so that you can run your script for longer.
Code taken from: http://www.php.net/manual/en/function.fsockopen.php#95605
You can use this function
function check($proxy=null)
{
$proxy= explode(':', $proxy);
$host = $proxy[0];
$port = $proxy[1];
$waitTimeoutInSeconds = 10;
$bool=false;
if($fp = #fsockopen($host,$port,$errCode,$errStr,$waitTimeoutInSeconds)){
$bool=true;
}
fclose($fp);
return $bool;
}