Useful CLI tools (bash) for Arch Linux administration
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.

156 lines
5.4 KiB

6 years ago
  1. #!/bin/bash
  2. # Randomize MAC Address
  3. trap ctrl_c INT
  4. function ctrl_c() {
  5. echo -e "\nAborting.\n"
  6. return
  7. }
  8. random_mac() {
  9. MAC=$(printf '%02x' $((0x$(od /dev/urandom -N1 -t x1 -An | cut -c 2-) & 0xFE | 0x02)); od /dev/urandom -N5 -t x1 -An | sed 's/ /:/g')
  10. }
  11. insert_mac() {
  12. echo -e "\nChanging MAC address information (root permission required).\n"
  13. sudo sed -i "/\[Link\]/!b;n;cMACAddress=$MAC" /etc/systemd/network/00-default.link
  14. echo -e "MAC address changed from '$MAC_OLD' to '$MAC' for interface '$MAC_DEVICE'.\n\nPlease restart this interface to apply the changes.\n\nTo restore original MAC address, either delete configuration file '/etc/systemd/network/$linkname' or set real MAC address manually into it.\n"
  15. return 0
  16. }
  17. geninsert_mac() {
  18. gennew_mac() {
  19. while true; do
  20. unset response
  21. read -r -p "Generate a new MAC address? [Y/n] " response
  22. if [[ $(echo $response | sed 's/ //g') =~ ^([yY][eE][sS]|[yY])$ ]]; then
  23. random_mac
  24. newname_mac
  25. else
  26. echo -e "\nKeeping old MAC address configuration.\n"
  27. return
  28. fi
  29. done
  30. }
  31. newname_mac() {
  32. unset response
  33. read -r -p "New MAC address for '$MAC_DEVICE' will be '$MAC'. Accept? [Y/n] " response
  34. if [[ $(echo $response | sed 's/ //g') =~ ^([yY][eE][sS]|[yY])$ ]]; then
  35. insert_mac
  36. else
  37. gennew_mac
  38. fi
  39. }
  40. newname_mac
  41. }
  42. gen_mac() {
  43. real_mac() {
  44. AVAILABLE_MACS=$(ip -br link show | sed '/LOOPBACK/d' | awk '{print NR"\t"$1"\t"$3"\t"$2}')
  45. IFS=$'\n'
  46. echo -e "\nAvailable network interfaces with their MAC addresses are:\n\n${AVAILABLE_MACS[*]}"
  47. echo -e "\nPlease select the interface which MAC address you want to spoof of\n"
  48. read -r -p "Selection [number]: " number
  49. if [[ ! $number =~ ^[0-9]+$ ]]; then
  50. echo -e "\nInvalid input value. Aborting.\n"
  51. return 1
  52. fi
  53. for INTERFACE in $(echo -e "${AVAILABLE_MACS[*]}"); do
  54. intf_num=$(echo $INTERFACE | awk '{print $1}')
  55. if [[ $number -eq $intf_num ]]; then
  56. MAC_REAL=$(echo $INTERFACE | awk '{print $3}')
  57. MAC_DEVICE=$(echo $INTERFACE | awk '{print $2}')
  58. break
  59. fi
  60. done
  61. unset IFS
  62. if [[ $MAC_REAL == "" ]]; then
  63. echo -e "\nNot a valid MAC address found for interface number $number. Aborting.\n"
  64. return 1
  65. fi
  66. }
  67. real_mac
  68. PREV_CONF=$(grep -Ril "$MAC_REAL" /etc/systemd/network/)
  69. if [[ ! $(echo $PREV_CONF | wc -w) -eq 0 ]]; then
  70. echo -e "\nUsing existing configuration file for interface '$MAC_DEVICE':\n$PREV_CONF\n"
  71. linkname=$(basename $PREV_CONF)
  72. MAC_OLD=$(awk -F= '/\[Link\]/{getline; print $2}' $PREV_CONF)
  73. else
  74. MAC_OLD=$MAC_REAL
  75. echo -e "\nPrevious configuration file not found. Creating it (root permission required).\n"
  76. read -r -p "Configuration file name? (must follow syntax: 00-default.link, 41-default.link, 98-default.link etc.): " linkname
  77. if [[ $linkname =~ ^[0-9][0-9]-default.link ]]; then
  78. if [[ ! $(sudo -n true) ]]; then
  79. sudo echo ""
  80. fi
  81. echo -e "[Match]\nMACAddress=$MAC_REAL\n\n[Link]\nMACAddress=$MAC_REAL\nNamePolicy=kernel database onboard slot path" \
  82. | sudo tee /etc/systemd/network/$linkname > /dev/null
  83. echo -e "Created new configuration file: /etc/systemd/network/$linkname\n"
  84. else
  85. echo -e "\nInvalid file name given. Aborting.\n"
  86. return 1
  87. fi
  88. fi
  89. unset response
  90. echo -e "Either randomly generated or manually specified MAC address can be used.\n"
  91. read -r -p "Do you want to use randomly generated MAC address? [Y/n] " response
  92. if [[ $(echo $response | sed 's/ //g') =~ ^([yY][eE][sS]|[yY])$ ]]; then
  93. random_mac
  94. geninsert_mac
  95. else
  96. if [[ $(echo $response | sed 's/ //g') =~ ^([nN][oO]|[nN])$ ]]; then
  97. read -r -p "Please type a new MAC address (Syntax is e.g. aa:bb:33:zz:f0:4a): " MAC
  98. maxtries=5
  99. while [[ $maxtries -gt 0 ]]; do
  100. case "$MAC" in
  101. [[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]])
  102. insert_mac
  103. ;;
  104. esac
  105. unset MAC
  106. read -r -p "Invalid MAC address given. Please type again ($maxtries tries left): " MAC
  107. let maxtries--
  108. done
  109. else
  110. echo -e "\nInvalid answer. Aborting.\n"
  111. fi
  112. fi
  113. }
  114. echo -e "\nWARNING: Changing MAC address WILL DISRUPT connections to any network device using MAC-based authentication methods. These devices may include configured routers, WiFi hotspots etc. Remember to write down the new MAC address, and make sure you are authorized to configure the MAC address to all affected network devices if needed.\n"
  115. read -r -p "You are going to spoof a MAC address of a network interface of this computer. Are you sure? [Y/n] " response
  116. if [[ $(echo $response | sed 's/ //g') =~ ^([yY][eE][sS]|[yY])$ ]]; then
  117. gen_mac
  118. else
  119. echo -e "\nKeeping old MAC address configuration.\n"
  120. fi