📜 ⬆️ ⬇️

A simple script to switch network settings

I use Slackware, in which GUI utilities are minimized. Frankly, I also do not ship X so often. However, at the time when I was sitting on Ubunt, I liked the Network Manager, which was hanging in the tray and indicated the operation of network interfaces. However, another thing was important - it was possible to create “configurations” of network settings in it and switch between them (like “work”, “house”, “cafe”). In Slackware, I only had ncurses-based netconfig on hand, which stupidly copied some files. By repeatedly running it in different places, I didn’t get the effect that I would like - the settings from the previous place were erased. Then I decided to write a simple script that allows you to "switch" settings in the style of Network Manager. I did it roughly, I do not exclude that there is a more subtle solution, but it works and is to some extent universal.

I publish it below, if someone will be useful:
  1. #! / bin / bash
  2. # Files that the script controls
  3. FILES = "/etc/rc.d/rc.inet1.conf /etc/resolv.conf"
  4. print_help ( ) {
  5. echo To use print
  6. echo
  7. echo switch_net.sh location
  8. echo
  9. for file in $ FILES ; do
  10. echo file $ file under control
  11. done
  12. }
  13. if [ -z $ 1 ] ;
  14. then
  15. print_help
  16. exit
  17. fi
  18. for file in $ FILES ; do
  19. if [ ! -f $ file . $ 1 ] ; then
  20. echo File $ file . $ 1 not exists, please point to existing one
  21. exit
  22. fi
  23. done
  24. for file in $ FILES ; do
  25. ln -sfv $ file . $ 1 $ file
  26. done
  27. echo Restarting inet
  28. / etc / rc.d / rc.inet1 restart

It is very easy to use. It pushes links to files in which the actual configuration is stored. I only track /etc/rc.d/rc.inet1.conf and /etc/resolv.conf. In this case, they will no longer be files, but links to /etc/rc.d/rc.inet1.conf.location and /etc/resolv.conf.location. I create them manually, then I do switch_net, then netconfig, when I come to a “new place”. To switch from one to another, it is enough to execute switch_net with superuser rights:
bash- 4.1 $ sudo switch_net home
Password:
" / Etc / rc.d / rc.inet1.conf" - > " / etc / rc.d / rc.inet1.conf.home"
" / Etc / resolv.conf" - > " / etc / resolv.conf.home"
Restarting inet

')

Source: https://habr.com/ru/post/129292/


All Articles