Advertisement
sebastiangiro

Shell script to restart wifi on loss

Aug 18th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.31 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. Version='0.2.10'
  4. # Put your wifi interface here
  5. interface='wlp3s0'
  6. # Put the ip to ping here (google by default)
  7. # Use IPS because some parameters of the shell only work with ip's
  8. # If not the shell will run slow (-w maxping on ping)
  9. host='google.com'
  10. maxping=2
  11.  
  12. # Reset
  13. NC='\033[0m'       # Text Reset
  14.  
  15. # Regular Colors
  16. Red='\033[0;31m'          # Red
  17.  
  18. # Bold
  19. BRed='\033[1;31m'         # Red
  20. BGreen='\033[1;32m'       # Green
  21. BYellow='\033[1;33m'      # Yellow
  22. BCyan='\033[1;36m'        # Cyan
  23.  
  24. echo -e "${BGreen}____________________________________________________________${NC}"
  25. echo -e "Version ${BGreen}${Version}${NC}"
  26. echo -e "Made with love by"
  27. echo -e "${BCyan}
  28. ▄▄▄·▪         ▐ ▄       ▄▄
  29. ▐█ ▄███ ▪     •█▌▐█▪     ██▌
  30. ██▀·▐█· ▄█▀▄ ▐█▐▐▌ ▄█▀▄ ▐█·
  31. ▐█▪·•▐█▌▐█▌.▐▌██▐█▌▐█▌.▐▌.▀
  32. .▀   ▀▀▀ ▀█▄▀▪▀▀ █▪ ▀█▄▀▪ ▀
  33. ${NC}"
  34. echo -e "Please run with ${BRed}sudo${NC} and make it executable with ${BRed}chmod +x${NC}"
  35. echo -e "Do ${BRed}iwconfig${NC} and modify the script with your wifi interface"
  36. echo -e "${BGreen}____________________________________________________________${NC}"
  37.  
  38. while true;
  39.   do
  40.     echo -e "${BCyan}CHECKING ${NC}network availability"
  41.     echo -e "${Red}------------------------------------------------${NC}"
  42.     sleep 5
  43.     ping -q -W ${maxping} -c1 ${host} > /ping.$
  44.     if [ $? -eq 0 ]
  45.     then
  46.       echo -e "The connection is ${BGreen}OK${NC}"
  47.       echo -e "${Red}------------------------------------------------${NC}"
  48.     else
  49.       ping -q -W ${maxping} -c1 ${host} > /ping.$
  50.       if [ $? -eq 0 ]
  51.       then
  52.         echo -e "Packet lost but now is ${BYellow}'OK'${NC}"
  53.         echo -e "${Red}------------------------------------------------${NC}"
  54.       else
  55.         echo -e "Connection ${BRed}LOST${NC}"
  56.         echo -e "${Red}------------------------------------------------${NC}"
  57.  
  58.         echo -e "${BCyan}Restarting ${NC}wifi"
  59.         echo -e "${Red}------------------------------------------------${NC}"
  60.  
  61.         echo -e "${BCyan}Wifi ${BRed}off${NC}"
  62.         nmcli n off
  63.         # nmcli radio wifi off
  64.         sudo iwconfig ${interface} txpower off
  65.         sleep 1
  66.         echo -e "${BCyan}Wifi ${BGreen}on${NC}"
  67.         sudo iwconfig ${interface} txpower on
  68.         sleep 3
  69.         # nmcli n is deprecated, now we use nmcli radio wifi on, same for nmcli n off
  70.         nmcli n on
  71.         # nmcli radio wifi on
  72.         sleep 10
  73.  
  74.         ping -q -W ${maxping} -c1 ${host} > /ping.$
  75.         if [ $? -eq 0 ]
  76.         then
  77.           echo -e "${BGreen}SUCCESS!${NC}"
  78.         else
  79.           echo -e "${BRed}Attempting last resource...${NC}"
  80.           echo -e "${BGreen}___________________________________________${NC}"
  81.           echo -e "${BCyan}RESTARTING ${NC}network manager service from systemctl"
  82.           sudo /bin/systemctl restart network-manager.service
  83.           sleep 4
  84.         fi
  85.       fi
  86.     fi
  87.   done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement