Skip to main content
Topic: General Coding Discussion? (Read 6679 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

General Coding Discussion?

I don't find any General Coding Discussion board which can be used to discuss this topic, so I opened it here instead.

I am running a test server using Ubuntu Thrusty & ISPConfig 3 for ElkArte Malaysia site but facing one problem i.e. IP address problem since I am using a dynamic IP instead of static one.

I have configured ddclient to update the IP in zoneedit so that it will be kept updated from time to time, if there is an IP change. However, ISPConfig has its own database where each domain's IP is kept.

I need a script to update this database with the current IP for each domain there is any change to it. I guess some php script with cron job can do this. What I will do is study ddclient scripts first and see whether they can make changes to the ISPConfig database at the same time it updates zoneedit if the IP change.

If anybody can throw any suggestions or codes, they will be highly appreciated.

Thank you in advance.

Re: General Coding Discussion?

Reply #1

Can't help out on this one as I have not done server configs with dynamic IP addressing.  I  would like to hear about the solution you come up with.

Re: General Coding Discussion?

Reply #2

Should be a fairly easy thing to do. Just run a cron every so often to check if the IP is changed and update it. Biggest issue is probably finding out where ISPConfig stores the info.  Using a dynamic IP for a website has to suck. Considering how long it takes DNS to update sometimes.
Success is not the result of spontaneous combustion, you must set yourself on fire!

Re: General Coding Discussion?

Reply #3

How do you run ddclient when the IP changes?
Bugs creator.
Features destroyer.
Template killer.

Re: General Coding Discussion?

Reply #4

@ahrasis  are you using your PC to run the server ? How is it connected ?

Re: General Coding Discussion?

Reply #5

It is a pc server with 1TB storage and 8GB ram. Setting up ddclient is easy but the setting must be right though. Just apt-get ddclient and edit /etc/ddclient.conf accordingly. If anybody need a working configuration for ubuntu thrusty 14.04, post a request here.

I believe @IchBin is right. Cron job is how to resolve this. But definitely with a code to access ispconfig database which luckily I know what and how.

I need to create a proper working php script and access that database, add it to ispconfig folder, check the current ip every 5 minutes, find and replace that ip table if the ip has changed. In other words, ddclient handles ip change to zoneedit while this php script handles ip change to ispconfig database.

A sync from ispconfig cp may be necessary (or may be possible with the same php script), so email should be sent to admin to remind this. Plus, zoneedit may some time need checking too.
Last Edit: April 25, 2015, 01:53:20 am by ahrasis

Re: General Coding Discussion?

Reply #6

Have you checked noip ?

Re: General Coding Discussion?

Reply #7

I have used noip before but I prefer zoneedit for now. If settings are correct, ddclient would check your public ip every ten minutes and update it at zoneedit if there is any ip change. This is my working configuration for ddclient:
Code: [Select]
daemon=600
pid=/var/run/ddclient.pid
ssl=yes
protocol=zoneedit1
use=web, web=dnspark
server=dynamic.zoneedit.com
login=loginid
password='password'
domain.tld

You can follow the steps in here to install and test run ddclient on your server.

Edited: Zoneedit requires minimum of ten minutes now.i
Last Edit: December 03, 2015, 07:31:30 pm by ahrasis

Re: General Coding Discussion?

Reply #8

I have been thinking and formulating this php code named IP Updater to update stored ip into current public if there is any ip changes. Cron job has to be set for it to work accordingly.

I am moving this code for a test but am not sure whether it will work. This code will be shared later on in HowToForge. If you have any feedback on this, please feel free to do so.
Last Edit: April 27, 2015, 12:35:44 am by ahrasis

Re: General Coding Discussion?

Reply #9

I thought I have uploaded the php file but as you can see, I forgot. Lol.

The ip_updater.php (attached) is still not working. I troubleshooted it with 'everything I know'[1] but can't get it to work. Right now the file ownership is ispconfig:ispconfig.[2] No errors but the ispconfig  database tables are not updated.

Code: [Select]
// Get access by using ispconfig default
require_once '/usr/local/ispconfig/interface/lib/config.inc.php';
require_once '/usr/local/ispconfig/interface/lib/app.inc.php';
// Create connection
$ip_updater = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_password'], $conf['db_database']);
// Check connection
if (!$ip_updater) {
    die("Connection failed: " . mysql_connect_error());
}
$db_selection = mysql_select_db('dbispconfig', $ip_updater);
if (!$db_selection) {
    die ('Can\'t use selected database : ' . mysql_error());
}
// Current server public ip
$public_ip = file_get_contents('http://phihag.de/ip/'); // Can use different ip checker
// Server stored ip
$select_ip = "SELECT ip_address FROM server_ip WHERE sys_userid =1";
$query_ip = mysql_query($select_ip);
$fetched_ip = mysql_fetch_assoc($query_ip);
$stored_ip = $fetched_ip['ip_address'];
// Update stored ip if there is changes
if ($public_ip != $stored_ip) {
        $update_ip = "UPDATE [icode]dns_rr[/icode] SET [icode]data[/icode] = replace([icode]data[/icode], '.$stored_ip.', '.$public_ip.')";
        $update_ip2 = "UPDATE [icode]sys_datalog[/icode] SET [icode]data[/icode] = replace([icode]data[/icode], '.$stored_ip.', '.$public_ip.')";
        $update_ip3 = "UPDATE [icode]server_ip[/icode] SET [icode]ip_address[/icode] = replace([icode]ip_address[/icode], '.$stored_ip.', '.$public_ip.')";
}
mysql_close($ip_updater);

I need to find and replace but I am guessing that the last UPDATE code is somehow wrong.

Can anybody see this code and give me some feedbacks?
I surely don't know much.
I can chown it to root if needed but is this necessary?

Re: General Coding Discussion?

Reply #10

What I can see in the code you posted is that none of the UPDATE queries are actually run, you assign the string to the variable, but then you don't do anything with that. ;)
Bugs creator.
Features destroyer.
Template killer.

Re: General Coding Discussion?

Reply #11

Thanks for the feedback @emanuele . So, I should run mysql_query for update_ip 1-3? Let me see... I will add the following code right after the update and see its output.

Code: [Select]
$run_update_ip = mysql_query($update_ip);
$run_update_ip2 = mysql_query($update_ip2);
$run_update_ip3 = mysql_query($update_ip3);

Anyway, can I simplify any of the above code?

Re: General Coding Discussion?

Reply #12

It may even just be the query without assigning it to another variable:
Code: [Select]
mysql_query($update_ip);
mysql_query($update_ip2);
mysql_query($update_ip3);
unless you want to verify everything went smooth.

There isn't much to simplify, the only one I can think of it:
Code: [Select]
$fetched_ip = mysql_fetch_assoc($query_ip);
$stored_ip = $fetched_ip['ip_address'];
to something like:
Code: [Select]
list ($stored_ip) = mysql_fetch_row($query_ip);
but that's all.

I would, instead, add some more complexity, for example a check to verify that $public_ip is actually an IP and not some random error, for example with:
Code: [Select]
if (filter_var($public_ip), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
{
    // do your stuff here
}
see:
http://php.net/manual/en/function.filter-var.php
http://php.net/manual/en/filter.filters.validate.php
Bugs creator.
Features destroyer.
Template killer.

Re: General Coding Discussion?

Reply #13

I have updated the code and simplify it as much as I can.[1] It seems like running mysql_query for the updates is not working at all.[2]

I reproduce the current updated code:
Code: [Select]
<?php
// Get access by using ispconfig default
require_once '/usr/local/ispconfig/interface/lib/config.inc.php';
// I think the following is only needed if this is run from control panel
// require_once '/usr/local/ispconfig/interface/lib/app.inc.php';
// Create and check connection to proceed
$ip_updater = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_password']);
if (!$ip_updater) {
echo "Connection failed! \r\n"; die(mysql_connect_error());
} else {
// Connection is fine. Select the database
$db_selection = mysql_select_db($conf['db_database'], $ip_updater);
if (!$db_selection) {
echo "Can\'t use selected database! \r\n"; die(mysql_error());
} else {
// Databse is selected. Get public ip. Use others if this not working.
$public_ip = file_get_contents('http://phihag.de/ip/');
if (!filter_var($public_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === true) {
echo "Filtering failed!";  die (mysql_error());
} else {
// IPv4 is true. Extract stored ip.
echo "Comparing public IP " . $public_ip . " with ";
$select_ip = "SELECT ip_address FROM server_ip WHERE sys_userid =1";
$query_ip = mysql_query($select_ip);
list ($stored_ip) = mysql_fetch_row($query_ip);
echo "stored IP " . $stored_ip . ". \r\n";
// Update stored ip only if there is a change
if ($public_ip == $stored_ip) {
echo "Same IP address. No changes is made." . "\r\n"; die (mysql_error());
} else {
echo "Different IP address. Attempting updates..." . "\r\n";
$update_ip = mysql_query("UPDATE [icode]dns_rr[/icode] SET [icode]data[/icode] = replace([icode]data[/icode], '.$stored_ip.', '.$public_ip.')");
$update_ip2 = mysql_query("UPDATE [icode]sys_datalog[/icode] SET [icode]data[/icode] = replace([icode]data[/icode], '.$stored_ip.', '.$public_ip.')");
$update_ip3 = mysql_query("UPDATE [icode]server_ip[/icode] SET [icode]ip_address[/icode] = replace([icode]ip_address[/icode], '.$stored_ip.', '.$public_ip.')");
}
// Check the updates mysql_query
if(!$update_ip3) {
echo "Public ip should now be the same with stored ip. :( \r\n"; die (mysql_error());
} else {
// Double check stored ip as update mysql_query always returns successful :(
$select_new_ip = "SELECT ip_address FROM server_ip WHERE sys_userid =1";
$query_new_ip = mysql_query($select_new_ip);
$fetched_new_ip = mysql_fetch_assoc($query_new_ip);
$new_stored_ip = $fetched_new_ip['ip_address'];
echo "Stored ip is ";
if ($new_stored_ip = $stored_ip) {
echo "still " . $new_stored_ip . ". Updates failed! \r\n"; die (mysql_error());
} else {
echo "now " . $new_stored_ip . ". Updates are successful! Thanks God." . "\r\n";
}
}
}
}
}
// Close connection
mysql_close($ip_updater);
echo "Closing connection..." . "\r\n";
?>

I already checked the database and its user has update privilege to update from localhost. What else could be wrong?[3]
Or as much as I know. Lol.
Sighed...
I was wondering whether ispconfig itself does not allow any updates to its database. Hmmm...

Re: General Coding Discussion?

Reply #14

Why do you use the "." around $stored_ip and $public_ip in the update queries?
Since you are not doing concatenation I would expect:
Code: [Select]
"UPDATE [icode]dns_rr[/icode] SET [icode]data[/icode] = replace([icode]data[/icode], '$stored_ip', '$public_ip')"
Bugs creator.
Features destroyer.
Template killer.