-
Notifications
You must be signed in to change notification settings - Fork 1
/
ethernet.php
258 lines (222 loc) · 8.94 KB
/
ethernet.php
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
session_start();
if(!isset($_SESSION['login']) || $_SESSION['login'] != true)
{
header("location: login.php");
exit;
}
$lines = explode("\n", trim(`nmcli dev stat|grep ethernet|sort -n`));
$ethArr = array();
$i = 0;
$enable = "yes";
$IP = $gw = $nm = $dns = "";
foreach($lines as $line)
{
$line = trim($line);
list($eth, $crud) = explode(" ", str_replace(" ", " ", $line), 2);
$i++;
$ethArr[$i]['int'] = $eth;
if(!isset($ethernet))
{
$ethernet = escapeshellarg(trim($ethArr[$i]['int']));
$ethernet2 = substr($ethernet, 1, -1);
}
if(isset($_POST['int']) && $_POST['int'] == $ethArr[$i]['int'])
{
$ethernet = escapeshellarg(trim($ethArr[$i]['int']));
$ethernet2 = substr($ethernet, 1, -1);
}
}
if(isset($_POST['button']))
{
$ethernet = escapeshellarg(trim($_POST['int']));
$ethernet2 = substr($ethernet, 1, -1);
$enable = substr(escapeshellarg(trim($_POST['enable'])), 1, -1);
$IP = substr(escapeshellarg(trim($_POST['IP'])), 1, -1);
$gw = substr(escapeshellarg(trim($_POST['gw'])), 1, -1);
$dns = substr(escapeshellarg(trim($_POST['dns'])), 1, -1);
$nm = substr(escapeshellarg(trim($_POST['nm'])), 1, -1);
$dhcp = substr(escapeshellarg(trim($_POST['dhcp'])), 1, -1);
if(file_exists("/etc/network/interfaces.d/$ethernet2"))
{
$do = `sudo ifdown --force "$ethernet2"`;
$do = `sudo ifconfig "$ethernet2" 0.0.0.0 down`;
}
if($enable == "yes")
{
if($dhcp != "no")
{
$cmd = "echo 'auto $ethernet2\nallow-hotplug $ethernet2\niface $ethernet2 inet dhcp' | sudo tee '/etc/network/interfaces.d/$ethernet2'";
$do = `$cmd`;
if($IP != "")
{
$cmd = "echo 'address $IP' | sudo tee -a '/etc/network/interfaces.d/$ethernet2'";
$do = `$cmd`;
}
if($nm != "")
{
$cmd = "echo 'netmask $nm' | sudo tee -a '/etc/network/interfaces.d/$ethernet2'";
$do = `$cmd`;
}
if($gw != "")
{
$cmd = "echo 'gateway $gw' | sudo tee -a '/etc/network/interfaces.d/$ethernet2'";
$do = `$cmd`;
}
if($dns != "")
{
$cmd = "echo 'dns-nameservers $dns' | sudo tee -a '/etc/network/interfaces.d/$ethernet2'";
$do = `$cmd`;
$cmd = "echo 'no-resolv\nserver=$dns' | sudo tee '/etc/dnsmasq.d/dns.conf'";
$do = `$cmd`;
} else {
$cmd = "sudo rm -f '/etc/dnsmasq.d/dns.conf'";
$do = `$cmd`;
}
$cmd = "echo 'post-up /var/www/html/scripts/WAN.sh up\npre-down /var/www/html/scripts/WAN.sh down' | sudo tee -a '/etc/network/interfaces.d/$ethernet2'";
$do = `$cmd`;
} else {
$cmd = "echo 'auto $ethernet2\nallow-hotplug $ethernet2\niface $ethernet2 inet static' | sudo tee '/etc/network/interfaces.d/$ethernet2'";
$do = `$cmd`;
if($IP != "")
{
$cmd = "echo 'address $IP' | sudo tee -a '/etc/network/interfaces.d/$ethernet2'";
$do = `$cmd`;
}
if($nm != "")
{
$cmd = "echo 'netmask $nm' | sudo tee -a '/etc/network/interfaces.d/$ethernet2'";
$do = `$cmd`;
}
if($gw != "")
{
$cmd = "echo 'gateway $gw' | sudo tee -a '/etc/network/interfaces.d/$ethernet2'";
$do = `$cmd`;
}
if($dns != "")
{
$cmd = "echo 'dns-nameservers $dns' | sudo tee -a '/etc/network/interfaces.d/$ethernet2'";
$do = `$cmd`;
$cmd = "echo 'no-resolv\nserver=$dns' | sudo tee '/etc/dnsmasq.d/dns.conf'";
$do = `$cmd`;
} else {
$cmd = "sudo rm -f '/etc/dnsmasq.d/dns.conf'";
$do = `$cmd`;
}
$cmd = "echo 'post-up /var/www/html/scripts/WAN.sh up\npre-down /var/www/html/scripts/WAN.sh down' | sudo tee -a '/etc/network/interfaces.d/$ethernet2'";
$do = `$cmd`;
}
} else {
$cmd = "echo 'iface $ethernet2 inet manual' | sudo tee '/etc/network/interfaces.d/$ethernet2'";
$do = `$cmd`;
}
$do = `sudo ifconfig $ethernet up`;
$do = `sudo ifup $ethernet`;
$cmd = "sudo /etc/init.d/dnsmasq restart";
$do = `$cmd`;
}
if(isset($_POST['disable']))
{
$do = `sudo ifdown --force $ethernet`;
$do = `sudo ifconfig $ethernet 0.0.0.0 down`;
$do = `sudo rm -f "/etc/network/interfaces.d/$ethernet2"`;
}
if(file_exists("/etc/network/interfaces.d/$ethernet2"))
{
$fp = fopen("/etc/network/interfaces.d/$ethernet2", "r");
while(!feof($fp))
{
$line = trim(fgets($fp, 1024));
if($line === false)
break;
if(strpos($line, "iface ") === 0)
{
$bits = explode(" ", $line, 4);
if($bits['3'] == "dhcp")
$dhcp = "yes";
if($bits['3'] == "static")
$dhcp = "no";
if($bits['3'] == "manual")
$enable = "no";
}
if(strpos($line, "address ") === 0)
list($crud, $IP) = explode(" ", $line, 2);
if(strpos($line, "netmask ") === 0)
list($crud, $nm) = explode(" ", $line, 2);
if(strpos($line, "gateway ") === 0)
list($crud, $gw) = explode(" ", $line, 2);
if(strpos($line, "dns-nameservers ") === 0)
list($crud, $dns) = explode(" ", $line, 2);
}
} else {
$IP = "192.168.100.2";
$nm = "255.255.255.0";
$gw = "192.168.100.1";
$dns = "192.168.100.1";
$dhcp = "yes";
}
if($IP == "")
{
$IP = trim(`ifconfig eth0 | awk '/inet /{ print $2}'`);
$nm = trim(`ifconfig eth0 | awk '/netmask /{ print $4}'`);
$gw = trim(`ip route list dev eth0 | awk ' /^default/ {print $3}'`);
$dns = trim(`grep 'dns-nameservers' '/etc/network/interfaces.d/eth0'|awk -F \ '{print $2}'`);
if($dns == "")
$dns = $gw;
}
$page = 3;
$pageTitle = "Ethernet Settings";
include_once("header.php");
?>
<div id="page-wrapper" >
<div id="page-inner">
<div class="row">
<div class="col-md-12">
<h2><?=$pageTitle?></h2>
</div>
</div>
<hr />
<div class="row" style="padding-left:15px;padding-right:15px;">
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<div style="width:140px;float:left">Interface:</div>
<select name="int" class="form-control" style="width:200px;float:left;margin-left:20px;" onClick="this.form.submit()">
<?php for($i = 1; $i <= count($ethArr); $i++) { ?>
<option value="<?=$ethArr[$i]['int']?>"<?php if($ethArr[$i]['int'] == $ethernet2) { ?> selected<?php } ?>><?=$ethArr[$i]['int']?></option>
<?php } ?>
</select><br style="clear:left;"/>
<div style="width:140px;float:left">Enable Interface:</div>
<div style="width:100px;float:left;padding-left:25px;">
<input style="width:20px;float:left" class="form-control" name="enable" value="yes" type="radio" id="ifup"<?php if($enable != "no") { echo " checked='checked'"; } ?>>
<label style="padding-left:10px;" for="ifup">Yes</label>
</div>
<div style="width:100px;float:left">
<input style="width:20px;float:left" class="form-control" name="enable" value="no" type="radio" id="ifdown"<?php if($enable == "no") { echo " checked='checked'"; } ?>>
<label style="padding-left:10px;" for="ifdown">No</label>
</div>
<br style="clear:left;"/>
<div style="width:140px;float:left">Use DHCP:</div>
<div style="width:100px;float:left;padding-left:25px;">
<input style="width:20px;float:left" class="form-control" name="dhcp" value="yes" type="radio" id="useDHCP"<?php if($dhcp != "no") { echo " checked='checked'"; } ?>>
<label style="padding-left:10px;" for="useDHCP">Yes</label>
</div>
<div style="width:100px;float:left">
<input style="width:20px;float:left" class="form-control" name="dhcp" value="no" type="radio" id="useStatic"<?php if($dhcp == "no") { echo " checked='checked'"; } ?>>
<label style="padding-left:10px;" for="useStatic">No</label>
</div>
<br style="clear:left;"/>
<div style="width:140px;float:left">Gateway IP:</div>
<input type="text" style="width:200px;float:left;margin-left:20px;" class="form-control" name="gw" value="<?=$gw?>" placeholder="Enter Gateway IP" /><br style="clear:left;"/>
<div style="width:140px;float:left">Ethernet IP:</div>
<input type="text" style="width:200px;float:left;margin-left:20px;" class="form-control" name="IP" value="<?=$IP?>" placeholder="Enter Ethernet IP" /><br style="clear:left;"/>
<div style="width:140px;float:left">Netmask:</div>
<input type="text" style="width:200px;float:left;margin-left:20px;" class="form-control" name="nm" value="<?=$nm?>" placeholder="Enter Ethernet Netmask" /><br style="clear:left;"/>
<div style="width:140px;float:left">DNS IP:</div>
<input type="text" style="width:200px;float:left;margin-left:20px;" class="form-control" name="dns" value="<?=$dns?>" placeholder="Enter DNS IP" /><br style="clear:left;"/>
<br style="clear:left;"/>
<input type="submit" class="btn btn-primary" name="button" value="Update and Reload" />
<input type="submit" class="btn btn-primary" name="disable" value="Disable" />
</form>
</div>
</div>
</div>
<?php include_once("footer.php"); ?>