Running terminal commands at login

Discussion in 'macOS' started by RossOliver, Nov 13, 2006.

  1. RossOliver macrumors regular

    Joined:
    Nov 6, 2006
    #1
    Hey,

    I have the need to change my mac address at login. I use the terminal command:

    sudo ifconfig en1 ether xx:xx:xx:xx:xx:xx
    <enter my password here>

    This works great, but it's a pain to do it every time I log in. Is there a way I can run these terminal commands automatically at login?

    Thanks for your time,

    -Ross
     
  2. Fearless Leader macrumors 68020

    Joined:
    Mar 21, 2006
    Location:
    Hoosiertown
    #2
    You could get an apple script written and then drag that to the login Items, I could write it if you want.

    edit: Is the mac address always the same?
     
  3. RossOliver thread starter macrumors regular

    Joined:
    Nov 6, 2006
    #3
    If you could, that would be fantastic! I took a look at apple script, but I couldn't get my head around what I needed to do :eek:

    The mac address is always the same...

    Cheers,

    -Ross
     
  4. Fearless Leader macrumors 68020

    Joined:
    Mar 21, 2006
    Location:
    Hoosiertown
    #4
    just tell me the address
     
  5. dunc85 macrumors regular

    Joined:
    Jul 23, 2006
    #5
    Type the following into an Applescript:

    do shell script "sudo ifconfig en1 ether 11:22:33:44:55:66" password "password" with administrator privileges

    then save it as an application and add it to your login items.
     
  6. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #6
    Do you want this to happen at login, or at every boot?

    To get shell commands to run at boot time, you can create a script named /etc/rc.local and put your command in there.

    Code:
    #!/bin/sh
    /sbin/ifconfig en1 ether xx:xx:xx:xx:xx:xx
    
    (no sudo needed because root will be running this.)

    It's a good idea to chown and chmod this file to prevent unexpected modifications, but it really isn't necessary to set it as executable because /etc/rc calls /bin/sh explicitly (but you can still set it executable if you like).
     
  7. Fearless Leader macrumors 68020

    Joined:
    Mar 21, 2006
    Location:
    Hoosiertown
    #7
    Code:
    do shell script "sudo ifconfig en0 ether 11:22:33:44:55:66"
    password "password" with administrator privileges
    
    just a note, don't break that apart into two lines by pressing enter, that changes the way it works
     
  8. RossOliver thread starter macrumors regular

    Joined:
    Nov 6, 2006
    #8
    I got the script to run at logon perfectly, thanks allot!

    It would be better if it ran at boot though. When it's changing the mac address of the airport card it disconnects the wireless until i reconnect it. I think running it at boot would solve this?

    I'm afraid I don't understand how to get to the /etc/rc.local file though :eek:

    The mac address I need to change my interface to is 00:17:f2:c5:17:16 if that helps...

    Thanks again,

    -Ross
     
  9. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #9
    From the terminal, use the command sudo nano /etc/rc.local

    (nano is a simple console text editor included with OS X)
    Type in the ifconfig command as above (using the Ethernet address you want), then use Ctrl-X (it's listed at the bottom of the screen as ^X) to exit and save the file. (answer yes to save changes and all that hooey). Again, no sudo needed within rc.local

    Then, you may want to enter the following commands to touch up the file:

    sudo chown root:wheel /etc/rc.local
    sudo chmod 744 /etc/rc.local
     
  10. RossOliver thread starter macrumors regular

    Joined:
    Nov 6, 2006
    #10
    Sorry if I'm dong something blatantly stupid here...but I run:

    Code:
    sudo nano /etc/rc.local
    
    This brings up the text editor as you mentioned, then I paste in:

    Code:
    #!/bin/sh
    /sbin/ifconfig en1 ether 00:17:f2:c5:17:16
    sudo chown root:wheel /etc/rc.local
    sudo chmod 744 /etc/rc.local
    
    I've also just tried:

    Code:
    #!/bin/sh
    /sbin/ifconfig en1 ether 00:17:f2:c5:17:16
    
    Then I hit Ctrl-X, and then Y to save the changes. I then close terminal and restart, but it doesn't appear to change the mac address...

    If I run:

    Code:
    sudo nano /etc/rc.local
    
    After closing the terminal once editing rc.local it doesn't appear to have saved the changes...

    Sorry again if I'm doing something stupid...

    -Ross
     
  11. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #11
    The chown and chmod commands don't go inside the script. Those are something to run interactively from the shell, just to make sure there are sane settings on the file after you create it with the editor. It really shouldn't affect anything to have them inside the script either, though.

    Now this here is a problem. Something must be setting the address later at startup time, but what? Hmmm.

    I'm sorry, what does this mean, you have an empty rc.local after all that?
     
  12. RossOliver thread starter macrumors regular

    Joined:
    Nov 6, 2006
    #12
    Ah I understand, I'll chmod it after I edit it.

    If I run:

    Code:
    sudo nano /etc/rc.local
    
    Straight after i close the terminal window, once I've edited rc.local, it comes up with an empty file...

    -Ross
     
  13. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #13
    Okay, I see what's happened with the boot sequence, Tiger has the lovely "feature" that the network interfaces aren't initialized by the time rc.local runs. The "improved" required method thanks to launch services is much more complicated.

    And that's after ctrl-X, say yes to saving, and leave the filename offered at the prompt untouched? WTF? I plain old can't duplicate the problem, I can edit and save files (and that one in particular) with nano just fine here :confused:
     
  14. RossOliver thread starter macrumors regular

    Joined:
    Nov 6, 2006
    #14
    Whoops! I was being an idiot :eek: I did Ctrl-X then Y, and I thought that saved it - I didn't see the prompt for the file name :rolleyes:

    Thanks for the link, I'll see if I can wade my way through that later on when I've woken up... you may hear from me again!

    Thanks again,

    -Ross
     
  15. RossOliver thread starter macrumors regular

    Joined:
    Nov 6, 2006
    #15
    Well, I got it working so it changes the mac address at boot, which is great!

    The only problem is, it seems to disconnect the wireless still, so it won't automatically connect when I log in.

    Is there a command I can use to connect to my preferred wireless connection that I could insert into this script, to run after the mac address has changed?

    Code:
       #!/bin/sh
    
       echo "Changing built-in ethernet MAC"
    
       false
       while [ $? != 0 ]; do
         sleep 5
         /sbin/ifconfig en0 lladdr 00:11:22:33:44:55
       done
    
    Thanks,

    -Ross
     
  16. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #16
    I'm not at an AirPort-connected Mac to try this out, but:

    Code:
    /System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport --autojoin
    looks like an interesting possibility. Same command, but with --help as the option, offers more toys to play with. Perhaps you can twiddle with those interactively at the terminal to find something that works.

    I see that it also has an interface to set the MAC address, maybe that one will be more tolerant of changes?
     
  17. RossOliver thread starter macrumors regular

    Joined:
    Nov 6, 2006
    #17
    That appears to do the trick :)

    I can run it from the terminal, but if I want it in the .sh file, would I just add it it at the bottom?

    Code:
       #!/bin/sh
    
       echo "Changing built-in ethernet MAC"
    
       false
       while [ $? != 0 ]; do
         sleep 5
         /sbin/ifconfig en0 lladdr 00:11:22:33:44:55
       done
    
    /System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport --autojoin
    
    Thanks,

    -Ross
     
  18. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #18
    If the part with changing the address works, I would expect that part to work by then too. Only one way to find out… :D

    (As far as it being a valid thing to put in a shell script, yes, that's fine)
     
  19. RossOliver thread starter macrumors regular

    Joined:
    Nov 6, 2006
    #19
    Ah, it wasn't working when I had it in the shell script, so I thought it was wrong. Turns out it was getting ahead of itself - I added a sleep after it changes the mac address and before it tries to connect the wireless and now it's perfect :)

    Thanks for all your help!

    -Ross
     

Share This Page