frame

Welcome

Search content for your interest. If you want to get more answers or contribute, click one of the buttons below!

Sign In Register

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Raspberry Pi Network Configuration Tool (network-config)

edited January 2017 in Plex

Screenshot of Raspberry Pi Network Configuration Tool (network-config) menu.

Login as pi user raspberry password (or whatever you made pi user password) then copy and
paste these commands into a remote shell, console or desktop TTY session. Then configure your desired Raspberry Pi network configuration!

sudo wget http://iiot.co.za/downloads/network_menu.sh
sudo mv /home/pi/network_menu.sh /etc/network_menu.sh
sudo chmod +x /etc/network_menu.sh
sudo ln -s /etc/network_menu.sh /home/pi/network-menu
sudo chmod +x /home/pi/network-menu
sudo ./network-menu

The source bash script is below.
`#!/bin/bash
calc_wt_size() {
# NOTE: it's tempting to redirect stderr to /dev/null, so supress error
# output from tput. However in this case, tput detects neither stdout or
# stderr is a tty and so only gives default 80, 24 values
WT_HEIGHT=17
WT_WIDTH=$(tput cols)

if [ -z "$WT_WIDTH" ] || [ "$WT_WIDTH" -lt 60 ]; then
WT_WIDTH=80
fi
if [ "$WT_WIDTH" -gt 178 ]; then
WT_WIDTH=120
fi
WT_MENU_HEIGHT=$(($WT_HEIGHT-7))
}
do_finish() {

exit 0

}
do_exit() {

exit 0

}
do_reboot() {

sudo reboot
exit 0

}
reboot_message(){
whiptail --msgbox "\
Changes sucessfully completed!

Your Raspberry Pi now needs to be rebooted
for the configuration to take effect.

Please select OK to return to the main menu
then select Exit & Reboot to reboot.
\
" 20 70 1
}
install_dnsmasq() {
echo "Checking for installed packages..."
CHECK_DNSMASQ_PACKAGE=$(dpkg -l | awk '{print $2}' | grep dnsmasq)
if [[ "$CHECK_DNSMASQ_PACKAGE" != "" ]]
then
echo $CHECK_DNSMASQ_PACKAGE "packages already installed."
fi
if [[ "$CHECK_DNSMASQ_PACKAGE" = "" ]]
then
echo "Installing dnsmasq packages..."
sudo rm -rf /etc/dnsmasq.*
sudo apt-get -yqq install dnsmasq-base dnsmasq --install-suggests --force-yes
fi
}
install_hostapd() {
echo "Checking for installed packages..."
CHECK_HOSTAPD_PACKAGE=$(dpkg -l | awk '{print $2}' | grep hostapd)
if [[ "$CHECK_HOSTAPD_PACKAGE" != "" ]]
then
echo $CHECK_HOSTAPD_PACKAGE "packages already installed."
fi
if [[ "$CHECK_HOSTAPD_PACKAGE" = "" ]]
then
echo "Installing hostapd packages..."
sudo apt-get -yqq install hostapd --install-suggests --force-yes
fi
}
do_wireless_ap(){
install_dnsmasq
install_hostapd
echo "checking for existing installation..."
EXISTING_AP_INSTALL=$(grep "hostapd" /etc/network/interfaces)
if [[ "$EXISTING_AP_INSTALL" != "" ]]
then
echo "This is not the first time this has been run..."
echo "SSID and Password will only be changed, please reboot your device when completed."
echo -e "Your password must be a minimum of 8 and max 63 characters."
echo -e "Enter your password?"
read APPASS
echo -e "Enter your SSID"
read APSSID
OLD_APSSID='ssid'
#NEW_APSSID='ssid="'"$APSSID"'"'
NEW_APSSID='ssid='"$APSSID"
OLD_APPASS='wpa_passphrase'
#NEW_APPASS='wpa_passphrase="'"$APPASS"'"'
NEW_APPASS='wpa_passphrase='"$APPASS"
echo "New SSID set to:"$NEW_APSSID
echo "New Passphrase set to:"$NEW_APPASS
sudo sed -i '/'"$OLD_APSSID"'/ c\'"$NEW_APSSID"'' /etc/hostapd/hostapd.conf
sudo sed -i '/'"$OLD_APPASS
"'/ c\'"$NEW_APPASS"'' /etc/hostapd/hostapd.conf
sudo systemctl daemon-reload
sudo service hostapd restart
sudo service networking restart
reboot_message
fi
if [[ "$EXISTING_AP_INSTALL" = "" ]]
then
echo "This is the first time this has been run..."
echo "SSID, Password, Wireless IPv4 IP, DHCP, DNS servers"
echo "and host access point daemon will be installed and configured."
echo -e "Your AP SSID password must be a minimum of 8 and max 63 characters."
echo -e "NB!! If you do not make this password long enough host access point services"
echo "will not start!!!!."
echo "Please enter your password?"
read APPASS
echo -e "Please enter your SSID?"
read APSSID
echo -e "Please enter the fixed IPv4 address for your wireless AP."
echo -e "For example 10.0.0.1"
echo -e "Please enter a IP address?"
read IP_ADDRESS
echo -e "Please enter a DHCP start and finish IP address seperated by a comma."
echo -e "For example 10.0.0.2,10.0.0.100"
echo -e "in the example above there will be 98 available IP addresses."
echo -e "Please enter the DHCP IPv4 scope or range?"
read DHCP_RANGE
echo -e "Checking and installing packages..."
install_dnsmasq
install_hostapd
DNSMASQ_CHECK_2=$(ls /etc/init.d/dnsmasq)
if [[ "$DNSMASQ_CHECK_2" = "" ]]
then
sudo apt-get -yqq install dnsmasq
fi
sudo cat > /etc/dnsmasq.conf < /etc/hostapd/hostapd.conf < /etc/network/interfaces <<INTERFACES_EOF
#source-directory /etc/network/interfaces.d

#auto lo
#iface lo inet loopback

#iface eth0 inet manual

#allow-hotplug wlan0
#iface wlan0 inet manual
#wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

#allow-hotplug wlan1
#iface wlan1 inet manual
# wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
#auto wlan0
sudo cat <> /etc/network/interfaces
iface wlan0 inet static
address $IP_ADDRESS
netmask 255.255.255.0
pre-up ifconfig wlan0
hostapd /etc/hostapd/hostapd.conf
EOT
#INTERFACES_EOF
OLD11='iface wlan0 inet manual'
NEW11='#iface wlan0 inet manual'
OLD12='wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf'
NEW12='#wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf'
sudo sed -i 's,'"$OLD11"','"$NEW11"',g' /etc/network/interfaces
sudo sed -i 's,'"$OLD12"','"$NEW12"',g' /etc/network/interfaces
echo "All done!"
reboot_message
fi
}
do_wireless_client(){
echo -e "Enter the SSID you wish to join?"
read C_SSID
echo -e "Enter your password or key?"
read C_SECRET
#cp /etc/network/interfaces /etc/network/interfaces.backup
sudo /usr/sbin/update-rc.d -f hostapd remove
sudo rm -rf /etc/hostapd/hostapd.conf
sudo rm -rf /etc/dnsmasq.*
sudo cat <> /etc/network/interfaces
iface wlan0 inet dhcp
wpa-scan-ssid 1
wpa-ap-scan 1
wpa-key-mgmt WPA-PSK
wpa-proto RSN WPA
wpa-pairwise CCMP TKIP
wpa-group CCMP TKIP
wpa-ssid "$C_SSID"
wpa-psk "$C_SECRET"
EOT
OLD11='iface wlan0 inet manual'
NEW11='#iface wlan0 inet manual'
OLD12='wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf'
NEW12='#wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf'
sudo sed -i 's,'"$OLD11"','"$NEW11"',g' /etc/network/interfaces
sudo sed -i 's,'"$OLD12"','"$NEW12"',g' /etc/network/interfaces
OLD20='iface wlan0 inet static'
OLD21=' address*'
OLD22=' netmask 255.255.255.0'
OLD23=' pre-up ifconfig wlan0'
OLD24=' hostapd /etc/hostapd/hostapd.conf'
NEW20='#iface wlan0 inet static'
NEW21='#address='
NEW22='#netmask 255.255.255.0'
NEW23='#pre-up ifconfig wlan0'
NEW24='#hostapd /etc/hostapd/hostapd.conf'
sudo sed -i 's,'"$OLD20"','"$NEW20"',g' /etc/network/interfaces
sudo sed -i 's,'"$OLD21"','"$NEW21"',g' /etc/network/interfaces
sudo sed -i 's,'"$OLD22"','"$NEW22"',g' /etc/network/interfaces
sudo sed -i 's,'"$OLD23"','"$NEW23"',g' /etc/network/interfaces
sudo sed -i 's,'"$OLD24"','"$NEW24"',g' /etc/network/interfaces
echo "All done!"
reboot_message
}
do_ethernet_server(){
install_dnsmasq
DNSMASQ_CHECK_2=$(ls /etc/init.d/dnsmasq)
if [[ "$DNSMASQ_CHECK_2" = "" ]]
then
sudo apt-get -yqq install dnsmasq
fi
DNSMASQ_CHECK_2=$(ls /etc/init.d/dnsmasq)
if [[ "$DNSMASQ_CHECK_2" != "" ]]
then
echo -e "Please enter the fixed IPv4 address for your ethernet LAN."
echo -e "For example 192.168.4.1"
echo -e "Please enter a IP address?"
read IP_ADDRESS
echo -e "Please enter a DHCP start and finish IP address seperated by a comma."
echo -e "For example 192.168.4.2,192.168.4.100"
echo -e "in the example above there will be 98 available IP addresses."
echo -e "Please enter the DHCP IPv4 scope or range?"
read DHCP_RANGE
sudo cat <> /etc/dnsmasq.conf
interface=eth0
dhcp-range=$DHCP_RANGE,255.255.255.0,24h
EOT
sudo cat <> /etc/network/interfaces
iface eth0 inet static
address $IP_ADDRESS
netmask 255.255.255.0
pre-up ifconfig eth0

EOT
OLD31='iface eth0 inet manual'
NEW31='#iface eth0 inet manual'
sudo sed -i 's,'"$OLD31"','"$NEW31"',g' /etc/network/interfaces
echo "All done!"
fi
reboot_message
}
install_enhanced_networking(){
CRON_ROOT_CHECK=$(ls /var/spool/cron/root)
if [[ "$CRON_ROOT_CHECK" = "" ]]
then
sudo touch /var/spool/cron/root
sudo chown root:root /var/spool/cron/root
fi
#Check for and remove enhance setting in crontab of root
CHECK_ENHANCED=$(crontab -u root -l | grep '@reboot sudo /etc/enhance_network.sh')
if [[ "$CHECK_ENHANCED" != "" ]]
then
LINE0='@reboot sudo /etc/enhance_network.sh'
crontab -u root -l | grep -v "$LINE0" | crontab -u root -
fi
#Add IPv4 forwarding and masquerading
IPV4_FORWARDING=$(crontab -u root -l | grep '@reboot sudo echo 1 > /proc/sys/net/ipv4/ip_forward')
if [[ "$IPV4_FORWARDING" = "" ]]
then
LINE1='@reboot sudo echo 1 > /proc/sys/net/ipv4/ip_forward'
(crontab -u root -l; echo $LINE1 ) | crontab -u root -
fi
MASQ_CHECK_1=$(crontab -u root -l | grep '@reboot sudo /sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE')
if [[ "$MASQ_CHECK_1" = "" ]]
then
LINE2='@reboot sudo /sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE'
(crontab -u root -l; echo $LINE2) | crontab -u root -
fi
MASQ_CHECK_2=$(crontab -u root -l | grep '@reboot sudo /sbin/iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE')
if [[ "$MASQ_CHECK_2" != "" ]]
then
crontab -u root -l | grep -v "$MASQ_CHECK_2" | crontab -u root -
fi
}
do_masq_eth0(){
install_enhanced_networking
MASQ_CHECK_3=$(crontab -u root -l | grep '@reboot sudo /sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE')
if [[ "$MASQ_CHECK_3" = "" ]]
then
(crontab -u root -l; echo $MASQ_CHECK_3) | crontab -u root -
fi
echo "All done!.."
reboot_message
}
do_masq_wlan0(){
install_enhanced_networking
LINE3='@reboot sudo /sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE'
LINE4='@reboot sudo /sbin/iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE'
crontab -u root -l | grep -v "$LINE3" | crontab -u root -
(crontab -u root -l; echo $LINE4) | crontab -u root -
echo "All done!.."
reboot_message
}
do_about() {
whiptail --msgbox "\
This tool provides a straight-forward way of doing network
configuration of the Raspberry Pi 3. Although it can be run
at any time, some of the options may have difficulties if
you have heavily customised your installation.

The tool was built for a Raspberry Pi 3 Model B SBC.

Wireless adapter can be configured in access point or
client configuration. The onboard ethernet adapter can
be configured in dhcp or configured with a static
IPv4 address and provide a dhcp service. \
" 20 70 1
}
do_restore(){
echo "Stopping services..."
sudo service dnsmasq stop
sudo service hostapd stop
echo "Removing autostart of DNS and DHCP services..."
cd /etc/init.d && sudo /usr/sbin/update-rc.d -f dnsmasq remove
echo "Backing up old configuration files..."
sudo mv /etc/network/interfaces /etc/network/interfaces.$RANDOM
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.$RANDOM
sudo mv /etc/hostapd/hostapd.conf /etc/hostapd/hostapd.conf.$RANDOM
echo "Rebuilding original /etc/network/interfaces file..."
sudo cat > /etc/network/interfaces <<'INTERFACES_EOF'
# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

iface eth0 inet manual

allow-hotplug wlan0
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

allow-hotplug wlan1
iface wlan1 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
INTERFACES_EOF
whiptail --msgbox "\
The original default Network configuration
has been restored!

Your Raspberry Pi now needs to be rebooted
for the configuration to take effect.
Please select OK to return to the main menu
then select finish to reboot.
\
" 20 70 1
}
# Interactive use loop
#
calc_wt_size
while true; do
FUN=$(whiptail --title "Raspberry Pi Network Configuration Tool (network-config)" --menu "Setup Options" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Finish --ok-button Select \
"1 Install Wireless AP" "Setup Raspberry Pi onboard Wi-Fi as wireless hotspot." \
"2 Install Wireless client" "Connect Raspberry Pi to existing network (WPA-PSK)." \
"3 Install DNS/DHCP Server" "Setup Raspberry Pi as DNS and DHCP server (Ethernet)." \
"4 IP Forward & Masquerade (Ethernet)" "When onboard ethernet adapter is connected to internet." \
"5 IP Forward & Masquerade (Wi-Fi)" "When onboard wireless adapter is connected to internet." \
"6 Restore Network Config" "Restores the original default raspbian os network configurations." \
"7 Exit" "Exit this menu without rebooting." \
"8 Exit & Reboot" "Exit and reboot your Raspberry Pi." \
"9 About network-config" "Information about this configuration tool." \
3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
do_finish
elif [ $RET -eq 0 ]; then
case "$FUN" in
1\ *) do_wireless_ap ;;
2\ *) do_wireless_client ;;
3\ *) do_ethernet_server ;;
4\ *) do_masq_eth0 ;;
5\ *) do_masq_wlan0 ;;
6\ *) do_restore ;;
7\ *) do_exit ;;
8\ *) do_reboot ;;
9\ *) do_about ;;
*) whiptail --msgbox "Programmer error: unrecognized option" 20 60 1 ;;
esac || whiptail --msgbox "There was an error running option $FUN" 20 60 1
else
exit 1
fi
done
NETWORK_MENU_EOF
chmod +x /etc/network_menu.sh`

Sign In or Register to comment.

Contact us

info@iiot.co.za

Get In Touch