Toggle between client and router mode on network interface (Linux)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.2 KiB

5 years ago
5 years ago
  1. #!/bin/env bash
  2. # Toggle between client/router mode on selected network interface
  3. # Copyright (C) 2018 Pekka Helenius
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. ########################
  18. # Usage:
  19. # chmod +x snic.sh
  20. # and then
  21. # Wired network interface, example:
  22. # snic.sh eth0 router // Switch eth0 to router mode
  23. # snic.sh eth0 client // Switch eth0 to client mode
  24. # snic.sh wlan0 router wireless // Switch wlan0 to router (Wi-Fi hotspot) mode
  25. # snic.sh wlan0 client wireless // Switch wlan0 to client mode
  26. # snic.sh <interface> <mode> <wireless (optional)>
  27. # <wireless (optional)> should be used only for wireless interfaces!
  28. ########################
  29. # This script is for network interfaces which do NOT support
  30. # simultanous Client/Router mode defined in
  31. # https://archlinux.org/index.php/software_access_point
  32. ########################
  33. nic=${1}
  34. for niccheck in $(ls /sys/class/net); do
  35. if [[ ${nic} == ${niccheck} ]]; then
  36. nicfound=
  37. fi
  38. done
  39. if [[ ! -v nicfound ]]; then
  40. echo "No such network interface. Aborting."
  41. exit 1
  42. fi
  43. ########################
  44. if [[ ${2} != "router" ]] && [[ ${2} != "client" ]]; then
  45. echo "Invalid mode. Use either 'router' or 'client'. Aborting."
  46. exit 1
  47. fi
  48. ########################
  49. # This mode removes dynamically allocated IPv4 address from NIC interface
  50. # After that we allocate a fixed IPv4 address, defined in
  51. # /etc/vnic/vnic-<interface>.conf file.
  52. # After setting up a fixed IPv4 address
  53. # DHCP server is enabled for the NIC interface
  54. # Wired: Enable Ethernet router mode
  55. # Wireless: Enable Wi-Fi hotspot router mode
  56. #
  57. if [[ ${2} == "router" ]]; then
  58. #Do not let NetworkManager interfere our connection on this interface
  59. sudo nmcli device set ${nic} managed no 2>/dev/null
  60. sudo systemctl stop snic-dynamic@${nic}
  61. sudo systemctl restart snic-static@${nic}
  62. sudo systemctl restart snic-dhcpd4@${nic}
  63. if [[ ${3} == "wireless" ]]; then
  64. sudo systemctl restart hostapd
  65. fi
  66. fi
  67. ###############
  68. # This mode removes fixed IPv4 address from NIC interface
  69. # and reserves the interface for dynamic IPv4 retrieved
  70. # from a DHCP server
  71. # Additionally, we stop running DHCP server on the NIC interface
  72. # Wired: Enable Ethernet client mode
  73. # Wireless: Enable Wlan client mode
  74. if [[ ${2} == "client" ]]; then
  75. #Let NetworkManager handle our connection on this interface
  76. sudo nmcli device set ${nic} managed yes 2>/dev/null
  77. sudo systemctl stop snic-static@${nic}
  78. sudo systemctl restart snic-dynamic@${nic}
  79. sudo systemctl stop snic-dhcpd4@${nic}
  80. if [[ ${3} == "wireless" ]]; then
  81. sudo systemctl stop hostapd
  82. fi
  83. fi