lexthebrony

ifconfig MAC changer

Nov 12th, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.21 KB | None | 0 0
  1. #! /bin/bash
  2. #This script changes the mac address of a specified interface in modern linux OSes using only (mostly) the ifconfig command.
  3. #It should work out of the box without any third party downloads.
  4. #Noah B.
  5. #Last edit 2/2/2016
  6.  
  7. #Prompt for root
  8. [ "$UID" -eq 0 ] || exec sudo bash "$0" "$@"
  9.  
  10. int=$1
  11.  
  12. #if no int specified, ask for one
  13. if [ -z "$int" ]
  14. then
  15.     read -p "Enter interface: " int
  16. fi
  17. if [ ! -z "$int" ]
  18.  
  19. if ! [ -z $2 ]
  20. then
  21.     newmac="$2"
  22. else
  23.     newmac="02:"`cat /dev/urandom | tr -dc 'A-F0-9' | fold -w 10 | head -n 1 | sed 's/\(..\)/\1:/g;s/:$//'`
  24. fi
  25.  
  26. curmac=`ifconfig $int | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'`
  27.  
  28. #take down int
  29. then
  30.     printf "\nTaking down interface...\n\n"
  31. sleep 1
  32. ifconfig $int down
  33. echo "Current (old) MAC for $int: $curmac"
  34.  
  35. sleep 1
  36.  
  37. #change mac address
  38. ifconfig $int hw ether $newmac
  39. #bring up int & restart netmanager
  40. printf "\nMAC changed, bringing interface up and restarting netman...\n\n"
  41. sleep 1
  42. ifconfig $int up
  43. service network-manager restart
  44. #update curmac value
  45. curmac=`ifconfig $int | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'`
  46. printf "\nCurrent (new) MAC for $int: $curmac\n"
  47. #ifconfig $int
  48. else
  49. echo "error"
  50. fi
Advertisement
Add Comment
Please, Sign In to add comment