I have made a PHP to check an open port locally and count how many connections that is connect and print it. It works great locally but once i want to check remotely (my web server is on diff server) it just failed to get the result. The remote server is server 2012 with port 80 close. Below is the code.
$handle = fopen("Includes/online.txt", "r");
$contents = fread($handle, filesize("Includes/online.txt"));
fclose($handle);
$contents = explode("\n", $contents);
//print_r($contents);
if (!$contents || (time() - $contents[1]) > $ocs) {
$command = "netstat -p TCP -n";
exec($command,$status);
//$status = PsExecute($command);
//print_r($status);
$status = array_slice($status, 5);
$connections = array();
foreach ($status as $stat => $s) {
$c = preg_split('/ /', $s, -1, PREG_SPLIT_NO_EMPTY);
if ($c[3] == 'ESTABLISHED') {
$ipp = preg_split('/:/', $c[1], -1, PREG_SPLIT_NO_EMPTY);
$ripp = preg_split('/:/', $c[2], -1, PREG_SPLIT_NO_EMPTY);
for ($i=0; $i < 6; $i++ ){
if ($ipp[1] == $config['server'][$i]['port']) {
$connections[] = array('type'=>$c[0],'ip'=>$ipp[0],'port'=>$ipp[1],'rip'=>$ripp[0],'rport'=>$ripp[1],'con'=>$c[3]);
}
}
}
}
$con = (!count($connections) ? 0 : count($connections)) ."\n". time();
if ($fp = fopen('Includes/online.txt', 'w')) {
fwrite ($fp, $con);
}
$con = array('online' => count($connections));
} else {
$con = array('online' => $contents[0]);
}
//print_r($status);
Below is connection info.
$config['server'] = array(array('ip'=>'ipxxx',
'port'=>10201),array('ip'=>'ipxxx',
'port'=>10202),array('ip'=>'ipxxx',
'port'=>10203),array('ip'=>'ipxxx',
'port'=>10204),array('ip'=>'ipxxx',
'port'=>10205),array('ip'=>'ipxxx', 'port'=>10208));
$ocs = "20";//in seconds it checks
The purpose of the code is to check an open port remotely at difference server (server 2012) and count the total connections and print it. I have failed to do that remotely. Is there any port that I need to open beside SQL server port for PHP successfully connect to the server? MSSQL is connected and I don't have any problems with that.
To me it's unclear what you are asking for.
To check active TCP/IP connections on SQL-Server you can use there stored procedure sp_who2 or this query:
select * from sys.sysprocesses where net_library like 'TCP/IP%'
SQL-Server has default TCP/IP port 1433. The TCP/IP protocol must be enabled on server side.
Related
I got the data from machine connected through COM port but some of machine its not working(gave null value instead of weight)
Explanation how i read the weighing data through php
Load php_dio.dll extension on xampp server
2.Using portName , baudRate ,dataBit,stopBit i am able to get data
The code below shows how i got the weighing data
public static function getWeight($portName = '', $baudRate = '', $dataBit = '', $stopBit = '')
{
$bbSerialPort;
$bbSerialPort = dio_open($portName, O_RDWR); //open port with read and write permission
//we're on windows configure com port from command line
shell_exec("mode {$portName} baud={$baudRate} data={$dataBit} stop={$stopBit} parity=n xon=on");
$runForSeconds = new DateInterval("PT1S");
$endTime = (new DateTime())->add($runForSeconds);
$abc = '';
//loop for 1 seconds
while (new DateTime() < $endTime) {
$data = dio_read($bbSerialPort, 512); //this is a blocking call
if ($data) {
preg_match('/(:\d+)/', $data, $abc);
if (count($abc)) {
$abc = $abc[0];
if ($abc) break;
}
}
}
dio_close($bbSerialPort);
return $abc;
}
ON some of machine its working and gave exact output but some of machine its not working. Is there any missing on my code or the configuration of machine connected through
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
}
}
I am trying to connect to my Asterisk Manager interface using Http Connection. This is not working for me. Anyone can help for me?
I need to login to Asteric manager interface using php. PHP file should be in another server.
manager.conf
[general]
enabled = yes
webenabled = yes
port = 5038
bindaddr = 0.0.0.0
displayconnects=no ;only effects 1.6+
[sameera]
secret = 123123
deny=0.0.0.0/0.0.0.0
permit=192.168.100.122/255.255.255.0
read = system,call,log,verbose,command,agent,user,config,command,dtmf,reporting,cdr,dialplan,originate
write = system,call,log,verbose,command,agent,user,config,command,dtmf,reporting,cdr,dialplan,originate
writetimeout = 5000
192.168.100.122 is my server ip address
login.php
<?php
$timeout = 3;
$socket = fsockopen("192.168.100.122",'12321',$errno,$errstr,$timeout);
fputs($socket,"Action: Login\r\n");
fputs($socket,"Username: sameera\r\n");
fputs($socket,"Secret: 123123\r\n\r\n");
$line="";
$response="";
while($line != "\r\n"){
$line = fgetss($socket,128);
$response .= $line;
}
echo $response;
?>
Your port number is wrong, change 12321 to 5039
$socket = fsockopen("192.168.100.122",'5039',$errno,$errstr,$timeout);
<?php
$timeout = 3;
$socket = fsockopen("127.0.0.1",'5038',$errno,$errstr,$timeout);
fputs($socket,"Action: Login\r\n");
fputs($socket,"Username: julio\r\n");
fputs($socket,"Secret: 12345\r\n\r\n");
$line="";
$response="";
while($line != "\r\n"){
$line = fgetss($socket,128);
$response .= $line;
}
echo $response;
?>
Use phpagi/ami library,NOT re-create it again
Do debug of your code and ensure your firewall allow connect.
I am learning socket programming in PHP and so I am trying a simple echo-chat server.
I wrote a server and it works. I can connect two netcats to it and when I write in the one netcat, I recive it at the other. Now, I want to implement what NC does in PHP
I want to use stream_select to see if I have data on STDIN or on the socket to either send the message from STDIN to the server or reading the incoming message from the server.
Unfortunately the example at the php manual doesn't give me any clue how to do that.
I tried to simply $line = fgets(STDIN) and socket_write($socket, $line) but it doesnt work. So I started to go down and just want stream_select to act up when the user typed the message.
$read = array(STDIN);
$write = NULL;
$exept = NULL;
while(1){
if(stream_select($read, $write, $exept, 0) > 0)
echo 'read';
}
Gives
PHP Warning: stream_select(): No stream arrays were passed in
/home/user/client.php on line 18
But when I var_dump($read) it tells me, that it is an array with a stream.
array(1) {
[0]=>
resource(1) of type (stream)
}
How do I get stream_select to work?
PS: In Python I can do something like
r,w,e = select.select([sys.stdin, sock.fd], [],[])
for input in r:
if input == sys.stdin:
#having input on stdin, we can read it now
if input == sock.fd
#there is input on socket, lets read it
I need the same in PHP
I found a solution. It seems to work, when I use:
$stdin = fopen('php://stdin', 'r');
$read = array($sock, $stdin);
$write = NULL;
$exept = NULL;
Instead of just STDIN. Despite php.net says, STDIN is already open and saves using
$stdin = fopen('php://stdin', 'r');
It seems not, if you want to pass it into stream_select.
Also, the socket to the server should be created with $sock = fsockopen($host); instead of using socket_create on the client side... gotta love this language and it's reasonability and clear manual...
Here a working example of a client that connects to an echo server using select.
<?php
$ip = '127.0.0.1';
$port = 1234;
$sock = fsockopen($ip, $port, $errno) or die(
"(EE) Couldn't connect to $ip:$port ".socket_strerror($errno)."\n");
if($sock)
$connected = TRUE;
$stdin = fopen('php://stdin', 'r'); //open STDIN for reading
while($connected){ //continuous loop monitoring the input streams
$read = array($sock, $stdin);
$write = NULL;
$exept = NULL;
if (stream_select($read, $write, $exept, 0) > 0){
//something happened on our monitors. let's see what it is
foreach ($read as $input => $fd){
if ($fd == $stdin){ //was it on STDIN?
$line = fgets($stdin); //then read the line and send it to socket
fwrite($sock, $line);
} else { //else was the socket itself, we got something from server
$line = fgets($sock); //lets read it
echo $line;
}
}
}
}
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;
}