-
Notifications
You must be signed in to change notification settings - Fork 8
/
hda-change-dns
executable file
·85 lines (75 loc) · 2.61 KB
/
hda-change-dns
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/perl -w
#
# Amahi Home Server
# Copyright (C) 2007-2009 Amahi Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License v3
# (29 June 2007), as published in the COPYING file.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# file COPYING for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Amahi
# team at http://www.amahi.org/ under "Contact Us."
use DBI;
# Globals
my $m_host='localhost';
my $m_database='hda_production';
my $m_user='amahihda';
my $m_pass='AmahiHDARulez';
my $m_dns1='';
my $m_dns2='';
my $dbhM='';
my $sth='';
# Sub Pototypes
sub CollectCommandPromptInfo;
sub ConnectToDatabase;
sub ModifyDatabase;
sub DisconnectFromDatabase;
sub RestartServices;
# Main
CollectCommandPromptInfo;
ConnectToDatabase;
ModifyDatabase;
DisconnectFromDatabase;
RestartServices;
# Get command line arguments
sub CollectCommandPromptInfo
{
### Check that user has supplied correct number of command line args
die "Usage: hda-change-dns <dns1 ip address> <dns2 ip address>\nPass the full IP address of your two new DNS servers\n"
unless @ARGV == 2;
$m_dns1 = $ARGV[0];
$m_dns2 = $ARGV[1];
}
# Connect to the database to prepare for modification
sub ConnectToDatabase
{
$dbhM = DBI->connect("dbi:mysql:database=$m_database;host=$m_host","$m_user","$m_pass")
or die "Can't connect to MySQL process! \nError: $DBI::errstr\n";
}
# Modify the database with the settings we previously calculated.
sub ModifyDatabase
{
$sth = $dbhM->prepare("UPDATE $m_database.settings SET value = ? WHERE settings.name = 'dns' LIMIT 1 ;");
$sth->execute("custom") or die "Couldn't execute statement: " . $sth->errstr;
$sth = $dbhM->prepare("UPDATE $m_database.settings SET value = ? WHERE settings.name = 'dns_ip_1' LIMIT 1 ;");
$sth->execute($m_dns1) or die "Couldn't execute statement: " . $sth->errstr;
$sth = $dbhM->prepare("UPDATE $m_database.settings SET value = ? WHERE settings.name = 'dns_ip_2' LIMIT 1 ;");
$sth->execute($m_dns2) or die "Couldn't execute statement: " . $sth->errstr;
print "Network settings were successfully applied.\n"
}
sub DisconnectFromDatabase
{
$dbhM->disconnect
or warn " Disconnection failed: $DBI::errstr\n";
}
# Restart hda-ctl and other services related to networking
sub RestartServices
{
system("hda-ctl-hup");
}