Connecting to Asterisk Manager Interface using php Http Connection - php

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.

Related

How to verify specified proxy is used in php script?

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
}
}

PHP checking remote server port and count connections

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.

PHP TCP socket connection Limit - Windows server

I have a weird issue and I can't seem to find a solution or anything closer to the issue I am having ,
Here is the thing , I have a scoket script run via php on command line, it accepts connection and reads data in json format from mobile app clients and sends appropriate response in json.
Everything works fine except the number of connection does not go above 256 connection.
I would like to know why is that, and how can I solve it ? I have been It on so many days, but no luck!
Here is the script snippet
<?php
date_default_timezone_set("UTC");
$server = stream_socket_server("tcp://192.168.1.77:25003", $errno, $errorMessage);
if (!$server) {
die("$errstr ($errno)");
}
echo "Server started..";
echo "\r\n";
$client_socks = array();
while (true) {
//prepare readable sockets
$read_socks = $client_socks;
$read_socks[] = $server;
//start reading and use a large timeout
if (!stream_select ($read_socks, $write, $except, 10000)) {
die('something went wrong while selecting');
}
//new client
if (in_array($server, $read_socks)) {
$new_client = stream_socket_accept($server);
if ($new_client) {
//print remote client information, ip and port number
echo 'Connection accepted from ' . stream_socket_get_name($new_client, true);
echo "\r\n";
$client_socks[] = $new_client;
echo "Now there are total ". count($client_socks) . " clients";
echo "\r\n";
}
// echo stream_socket_get_name($new_client, true);
//delete the server socket from the read sockets
unset($read_socks[array_search($server, $read_socks)]);
}
$data = '';
$res = '';
//message from existing client
foreach($read_socks as $sock) {
stream_set_timeout($sock, 1000);
while($resp = fread($sock, 25000)) {
$data .= $resp;
if (strpos($data, "\n") !== false) {
break;
}
}
$info = stream_get_meta_data($sock);
if ($info['timed_out']) {
unset($client_socks[array_search($sock, $client_socks)]);
#fclose($sock);
echo 'Connection timed out!';
continue;
}
$client = stream_socket_get_name($sock, true);
if (!$data) {
unset($client_socks[array_search($sock, $client_socks)]);
#fclose($sock);
echo "$client got disconnected";
echo "\r\n";
continue;
}
//send the message back to client
$decode = json_decode($data);
$encode = json_encode($res);
fwrite($sock,$encode."\n");
}
}
P.S.: What I did is, extensive search on the topic, and went over article like these,
http://smallvoid.com/article/winnt-tcpip-max-limit.html and two dozens others.
I have a windows 7 running this thing + wamp 2.5 which runs php 5.5.12
It's nothing to do with your code, it's a "feature" of MS Windows to make you buy the server edition (or upgrade to a different OS). Functionally there's no difference between the server and desktop editions of the NT kernel (some different optimization tweaks) its just a means of ensuring you are complying with the terms of the licence.

CronJob BackUp: run every day, week, month

I did some google search for this but all I can find is separate things and the cPanel's interface is a little bit confusing to be honest.
Basically, I have this script to create files+mysql backup:
<?php
$auth = base64_encode(":");
$domain = "";
$theme = "";
$secure = false;
$ftp = false;
$ftpserver = "";
$ftpusername = "";
$ftppassword = "";
$ftpport = "21";
$ftpdirectory = "/";
if ($secure) {
$url = "ssl://" . $domain;
$port = 2083;
} else {
$url = $domain;
$port = 2082;
}
$socket = fsockopen($url, $port);
if (!$socket) {
exit("Failed to open socket connection.");
}
if ($ftp) {
$params = "dest=ftp&server=$ftpserver&user=$ftpusername&pass=$ftppassword&port=$ftpport&rdir=$ftpdirectory&submit=Generate Backup";
} else {
$params = "submit=Generate Backup";
}
fputs($socket, "POST /frontend/" . $theme . "/backup/dofullbackup.html?" . $params . " HTTP/1.0\r\n");
fputs($socket, "Host: $domain\r\n");
fputs($socket, "Authorization: Basic $auth\r\n");
fputs($socket, "Connection: Close\r\n");
fputs($socket, "\r\n");
while (!feof($socket)) {
$response = fgets($socket, 4096);
echo $response;
}
fclose($socket);
?>
I would like to run that PHP file so it backups up my stuff exactly like the cPanel would on any regular shared hosting plan: Daily, Weekly and Monthly. I want to do this because of my website got screwed up because the hosting company backup was not working properly.
Do I need to create separate cron jobs for daily, weekly, monthly or I can do all of them at once?
If I input a command in the Command field, is it enough to get it working or I need to setup the other input fields?
What's the correct command to run my backups as explained above?
Thanks!
In that interface, you have a few different options for the command,
php -q /path/to/your/script.php
would work.
If you add #!/usr/bin/php -q to the top of your PHP script, then you can just call it directly from cron:
/path/to/your/script.php
The general answers to your questions (not having to do with that web interface) are covered here:
How to execute a php script every day
This might also be helpful, a random but pretty decent reference on crontab syntax:
http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/

Help to test if a proxy works using php

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;
}

Categories