Skip to content

Instantly share code, notes, and snippets.

@eduardoaugustojulio
Last active April 10, 2024 19:25
Show Gist options
  • Save eduardoaugustojulio/b167361734400168edd9b038778f8387 to your computer and use it in GitHub Desktop.
Save eduardoaugustojulio/b167361734400168edd9b038778f8387 to your computer and use it in GitHub Desktop.
bash dialog script to configure network interface in linux based system.
#!/bin/bash
#The MIT License
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.
list_interfaces()
{
interfaces=$(ls /sys/class/net/)
}
get_if_address()
{
echo $(ifconfig $1 | grep -o 'inet addr:[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+' | grep -o [0-9].*)
}
get_if_netmask()
{
echo $(ifconfig $1 | grep -o 'Mask:[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+' | grep -o [0-9].*)
}
get_if_gateway()
{
echo $(ip route show | grep -o 'default via [0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+' | grep -o [0-9].*)
}
get_if_hwaddress()
{
echo $(ifconfig $1 | grep -o '[A-F0-9]\+\:[A-F0-9]\+\:[A-F0-9]\+\:[A-F0-9]\+\:[A-F0-9]\+\:[A-F0-9]')
}
select_interface()
{
list_interfaces
interfaceArray=()
while read name; do
interfaceArray+=($name "")
done <<< "$interfaces"
interface=$(dialog --stdout \
--title "Network Interfaces" \
--backtitle "Linux managment network interface" \
--ok-label "Next" \
--no-cancel \
--menu "Select an interface:" \
20 30 30 \
"${interfaceArray[@]}")
echo $interface
}
create_interface_file()
{
cat << EOF > $1
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
auto lo
iface lo inet loopback
EOF
}
write_interface_file()
{
if [ ! -f $1 ]; then
create_interface_file $1
fi
echo "" >> $1
echo "$2" >> $1
}
config_interface()
{
if="$1"
address="$2"
netmask="$3"
gateway="$4"
hwaddress="$5"
exec 3>&1
config=$(dialog --ok-label "Ok" \
--backtitle "Linux managment network interface" \
--title "Network interface $if config" \
--form "Configure network interface" \
15 50 0 \
"address:" 1 1 "$address" 1 10 15 0 \
"netmask:" 2 1 "$netmask" 2 10 15 0 \
"gateway:" 3 1 "$gateway" 3 10 15 0 \
"hwaddress:" 4 1 "$hwaddress" 4 11 17 0 \
2>&1 1>&3 |
{
read -r address
read -r netmask
read -r gateway
read -r hwaddress
result=$(printf "auto $if \niface $if inet static\n\taddress $address\n\tnetmask $netmask\n\tgateway $gateway\n\thwaddress $hwaddress")
write_interface_file "interfaces" "$result"
})
exec 3>&-
}
while : ; do
if="$(select_interface)"
config_interface "$if" "$(get_if_address $if)" "$(get_if_netmask $if)" "$(get_if_gateway)" "$(get_if_hwaddress $if)"
if (dialog --title "Finish" \
--backtitle "Linux managment network interface" \
--yesno "Do you want configure another interface?" 10 60) then
continue
else
break
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment