1

I very often need to change a MAC address in my job, so I would like to make two keyboard shortcuts:

  1. When I press a shortcut terminal opens with pasted command

    sudo ifconfig eth0 down && sudo ifconfig eth0 hw ether (xx:xx:xx:xx:xx:xx) && sudo ifconfig eth0 up 
    

    Need that cursor would appear in middle of command where I could write MAC. (or offer another command)

  2. On shortcut press automatically paste from cliboard MAC address to command above and execute.

Kvach
  • 191
  • 1
  • 11

1 Answers1

0

The aim:

You want a nice little MAC changer which you can just call using a keyboard shortcut and feed it a MAC? It should also be able to read it from the clipboard?

Here is what you want:

MAC Changer screenshot

Preparation:

To access the clipboard, we need xsel. Install it with the command

sudo apt-get install xsel

The scripts:

We need to make four little shell scripts to do your work.
Create the following files in /usr/local/bin using your favourite plain text editor. You will need sudo/root privileges to save them in the desired location.

  • get-mac:

    #! /bin/bash
    ip addr show eth0 | awk '/ether/{print $2}'
    
  • get-mac-dialog:

    #! /bin/bash
    zenity --info --title "MAC Changer" --text "Current MAC address: $(get-mac)" 2>/dev/null
    
  • change-mac:

    #! /bin/bash
    ifconfig eth0 down &&
    ifconfig eth0 hw ether "$@" || exit 1
    ifconfig eth0 up
    
  • change-mac-dialog:

    #! /bin/bash
    oldmac=$(get-mac)
    newmac=$(zenity --entry --title "MAC Changer" --text "New MAC address:" --entry-text $oldmac 2>/dev/null) &&
    gksudo change-mac $newmac || zenity --error --title "MAC Changer" --text "Failed to change MAC address"
    
  • paste-mac:

    #! /bin/bash
    gksudo change-mac $(xsel -ob)
    

If your network interface has any other name than eth0, you must replace all 4 occurrences of it in the scripts with the real name!

Now set the correct ownerships and permissions:

chmod 755 /usr/local/bin/get-mac /usr/local/bin/get-mac-dialog /usr/local/bin/change-mac /usr/local/bin/change-mac-dialog /usr/local/bin/paste-mac
sudo chown root: /usr/local/bin/get-mac /usr/local/bin/get-mac-dialog /usr/local/bin/change-mac /usr/local/bin/change-mac-dialog /usr/local/bin/paste-mac 

Usage summary:

  • get-mac:

    Simply print the current MAC address of eth0 to the terminal.

  • get-mac-dialog:

    Show a small dialog window displaying the current MAC address of eth0.

  • change-mac:

    Directly try to change the MAC address of eth0. It will disconnect and reconnect the interface to do this. You must run this as root!

    Example: sudo change-mac 12:34:56:78:9a:bc

  • change-mac-dialog:

    Show a nice little GUI prompt asking you to enter the new MAC address (default text is the current MAC). You will be prompted for a sudo password using the GUI gksudo.

  • paste-mac:

    Call gksudo change-mac with the current clipboard content as argument. You will be prompted for a sudo password using the GUI gksudo.

Shortcuts:

I assume you can create custom shortcuts for scripts yourself, otherwise please refer here.

Note that you should probably not create a shortcut for change-mac (needs to run as root and needs a MAC as argument) or get-mac (prints output to terminal). Use change-mac-dialog and get-mac-dialog as GUI replacement for them. paste-mac does neither need any input nor does it create output, so you can make a shortcut for that without any problem.

Byte Commander
  • 107,489