Last active
December 18, 2018 05:24
-
-
Save abz89/6e8a145e8d90bc044ee47971cff1d17f to your computer and use it in GitHub Desktop.
IP Configurator Helper written by simple PHP for 'mini device' such as: raspberry pi, cubieboard etc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Abz; | |
| /** | |
| * IP Configurator Helper written by simple PHP | |
| * for 'mini device' such as: raspberry pi, cubieboard etc | |
| * | |
| * USAGES: | |
| * ======= | |
| * 1) Set IP to DHCP: | |
| * $ipconfig = new IpConfig(); | |
| * $ipconfig->dhcp()->commit(); | |
| * | |
| * 2) Set IP to static: | |
| * $ipconfig = new IpConfig(); | |
| * $ipconfig->dhcp(false)->setIp('192.168.1.10', '255.255.255.0', '192.168.1.1')->commit(); | |
| * | |
| * 3) Set DNS to Public DNS: | |
| * $ipconfig = new IpConfig(); | |
| * $ipconfig->setDns('8.8.8.8', '8.8.4.4')->commit(); | |
| * | |
| * Note: | |
| * - this is possible to combine set IP with set DNS | |
| */ | |
| class IpConfig | |
| { | |
| /** | |
| * Default *NIX network configuration files | |
| */ | |
| const IPCONFIG_LOCATION = '/etc/network/interfaces.d/eth0'; | |
| /** | |
| * Default *NIX dns configuration files | |
| */ | |
| const DNSCONFIG_LOCATION = '/etc/resolv.conf'; | |
| /** | |
| * Weather ip address is DHCP or static | |
| * | |
| * @var bool | |
| */ | |
| private $is_dhcp; | |
| /** | |
| * Ip address for static IP configuration | |
| * | |
| * @var string | |
| */ | |
| private $ip_address; | |
| /** | |
| * Subnet of network | |
| * | |
| * @var string | |
| */ | |
| private $netmask; | |
| /** | |
| * Default gateway ip address | |
| * | |
| * @var string | |
| */ | |
| private $gateway; | |
| /** | |
| * Array of DNS Servers | |
| * | |
| * @var array | |
| */ | |
| private $dns_config; | |
| /** | |
| * Buffer of IP configuration text file | |
| * | |
| * @var [type] | |
| */ | |
| private $text_ip; | |
| /** | |
| * Buffer of DNS configuration text file | |
| * | |
| * @var [type] | |
| */ | |
| private $text_dns; | |
| /** | |
| * Construct do nothing | |
| */ | |
| public function __construct() | |
| { | |
| } | |
| /** | |
| * Set IP configuration mode, default is DHCP | |
| * | |
| * @param boolean $is_dhcp | |
| * @return object | |
| */ | |
| public function dhcp($is_dhcp = true) | |
| { | |
| $this->is_dhcp = ($is_dhcp===true) ? true : false; | |
| return $this; | |
| } | |
| /** | |
| * Set IP address configuration | |
| * | |
| * @param string $ip_address string of ip address | |
| * @param string $netmask string of netmask address | |
| * @param string $gateway string of gateway ip address | |
| * @return object | |
| */ | |
| public function setIp($ip_address, $netmask, $gateway) | |
| { | |
| if (!$this->is_dhcp) { | |
| $this->ip_address = $ip_address; | |
| $this->netmask = $netmask; | |
| $this->gateway = $gateway; | |
| } | |
| return $this; | |
| } | |
| /** | |
| * Set DNS address with single/multiple arguments of string | |
| * | |
| * @param array $dns_config string of dns server ip address, accept multiple | |
| * @return object | |
| */ | |
| public function setDns($dns_config) | |
| { | |
| $args = func_get_args(); | |
| $this->dns_config = $args; | |
| return $this; | |
| } | |
| /** | |
| * Parse IP configuration file content | |
| * | |
| * @return void | |
| */ | |
| private function parseIp() | |
| { | |
| if ($this->is_dhcp) { | |
| return <<<TEXT | |
| auto eth0 | |
| iface eth0 inet dhcp | |
| TEXT; | |
| } elseif ($this->ip_address && $this->netmask && $this->gateway) { | |
| return <<<TEXT | |
| auto eth0 | |
| iface eth0 inet static | |
| address $this->ip_address | |
| netmask $this->netmask | |
| gateway $this->gateway | |
| TEXT; | |
| } | |
| } | |
| /** | |
| * Parse DNS configuration file content | |
| * | |
| * @return void | |
| */ | |
| private function parseDns() | |
| { | |
| $text = ''; | |
| foreach ($this->dns_config as $dns) { | |
| $text .= "nameserver ${dns}\n"; | |
| } | |
| return $text; | |
| } | |
| /** | |
| * Check configuration that will be written | |
| * for debug purpose | |
| * | |
| * @return object | |
| */ | |
| public function show() | |
| { | |
| $parse_ip = $this->parseIp(); | |
| $parse_dns = $this->parseDns(); | |
| if ($parse_ip) { | |
| echo 'will be write this to ' . self::IPCONFIG_LOCATION . ":\n\n"; | |
| echo $parse_ip . "\n\n"; | |
| } | |
| if ($parse_dns) { | |
| echo 'will be write this to ' . self::DNSCONFIG_LOCATION . ":\n\n"; | |
| echo $parse_dns . "\n\n"; | |
| } | |
| return $this; | |
| } | |
| /** | |
| * Write configuration to file | |
| * | |
| * @param [type] $file location of configuration file | |
| * @param [type] $string string of configuration | |
| * @return bool | |
| */ | |
| private function put($file, $string) | |
| { | |
| return (file_put_contents($file, $string)===true) ? true : false; | |
| } | |
| /** | |
| * Commit by writing configuration | |
| * and restart network interface | |
| * | |
| * @return void | |
| */ | |
| public function commit() | |
| { | |
| $parse_ip = $this->parseIp(); | |
| $parse_dns = $this->parseDns(); | |
| if ($parse_ip) { | |
| $this->put(self::IPCONFIG_LOCATION, $parse_ip); | |
| exec('/usr/bin/sudo /sbin/ifdown eth0'); | |
| exec('/usr/bin/sudo /sbin/ifup eth0'); | |
| } | |
| if ($parse_dns) { | |
| $this->put(self::DNSCONFIG_LOCATION, $parse_dns); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment