Can anyone help me write a script to reconnect shares automatically?

Discussion in 'macOS' started by Spanky Deluxe, Apr 14, 2009.

  1. Spanky Deluxe macrumors 601

    Spanky Deluxe

    Joined:
    Mar 17, 2005
    Location:
    London, UK
    #1
    Hi everyone,

    I've got a mac mini under my TV that I use as a media centre, however, all of the video content is stored on my Mac Pro. All of the video is accessed by two standard shared folders.

    However, everytime I restart my Mac Pro the shares obviously stop working. I'd like a way to automatically keep these shares alive. I think I'd need something that does the following and would be in a script of sorts (terminal script I guess):

    On bootup of OS X on the Mac Pro do the following:

    1. Connect to the mac mini (maybe via ssh)
    2. Close all existing shares on the mac mini
    3. Initialise shares on the mac mini

    Any ideas?

    On a different note, any idea how to execute a terminal script in the background at a set time every day? I've got a few lines of code that sync up my iPhoto and iTunes library across my machines and I'd like to schedule it to run at a certain time.

    Thanks,

    Phil
     
  2. Spanky Deluxe thread starter macrumors 601

    Spanky Deluxe

    Joined:
    Mar 17, 2005
    Location:
    London, UK
    #2
    Right, I've managed to work this out myself so I figured I'd share what I've done with anyone else that might be interested or that might stumble across this thread via a Google search.

    My example works with two shares but it should be easy enough for you to work out how to modify it to only reconnect one share or more shares by removing or duplicating the lines of code in the script file.

    First some syntax for my instructions:

    10.0.0.2 is the IP address of machine A, the machine on which the shares are found (Mac Pro)
    10.0.0.3 is the IP address of machine B, the machine that needs access to the shares (Mac Mini)
    usernameA is the username of machine A (Mac Pro)
    usernameB is the username of machine B (Mac Mini)
    passwordA is the username of machine A (Mac Pro)
    passwordB is the username of machine B (Mac Mini)
    "Share 1" is the name of the first share on machine A (Mac Pro)
    "Share 2" is the name of the second share on machine A (Mac Pro)

    Now the first thing you need to do is enable machine A to be able to silently ssh in to machine B (i.e. without having to stop to ask you for a password every time).

    1. Open up a terminal window on machine A.

    2. Type the following:

    Code:
    ssh-keygen -t rsa
    It'll prompt you for a few things but simply press return for each one to use the default setting.

    3. Type the following:

    Code:
    ssh usernameB@10.0.0.3 mkdir -p .ssh
    It'll ask you for the password of machine B, so do that.

    4. Type the following:

    Code:
    cat .ssh/id_rsa.pub | ssh usernameB@10.0.0.3 'cat >> .ssh/authorized_keys'
    It'll ask you for machine B's password again.

    You can now ssh into machine B without having to type a password every time.

    Next, you need to create the script that does all the work.

    1. Open up TextEdit, click the Format menu then "Make Plain Text" to get it in the right format.

    2. Type in the following:

    Code:
    #!/bin/sh
    ssh usernameB@10.0.0.3 'umount "/Volumes/Share 1"'
    ssh usernameB@10.0.0.3 'umount "/Volumes/Share 2"'
    ssh usernameB@10.0.0.3 'mkdir "/Volumes/Share 1"'
    ssh usernameB@10.0.0.3 'mkdir "/Volumes/Share 2"'
    ssh usernameB@10.0.0.3 'mount -t afp "afp://usernameA:passwordA@10.0.0.2/Share 1" "/Volumes/Share 1"'
    ssh usernameB@10.0.0.3 'mount -t afp "afp://usernameA:passwordA@10.0.0.2/Share 2" "/Volumes/Share 2"'
    If the shares in question aren't standard Apple shares then you'll probably want to change the "-t afp" to "-t smb" for Windows share for example.

    3. Save this file in your root directory as "connectshares" (Make sure to uncheck the box that tells it to add a file extension of ".txt" automatically.

    4. Open up a terminal window (or use the one you may have open still from the first part) and in your home directory (type simply "cd " to get back there if you're not there) type the following:

    Code:
    chmod +x connectshares
    Now you need to get this script to automatically run at login.

    1. Open up your System Preferences. Click on Accounts, then select your user account and click on the "Login Items" tab.

    2. Click the little plus sign and in the following window navigate to your home directory (it should be in your "Places section" as your username or failing that click your main hard drive, then Users, then your username).

    3. Select your connectshares script and click "Add". You can click the "Hide" checkbox in the resulting login items list for the script so that it doesn't show anything on login.

    That's it for machine A.

    If you want your second machine to also automatically connect to these shares if it reboots then you'll want to do the following on Machine B this time


    1. On machine B, open up TextEdit, click the Format menu then "Make Plain Text" to get it in the right format.

    2. Type in the following:

    Code:
    #!/bin/sh
    umount "/Volumes/Share 1"
    umount "/Volumes/Share 2"
    mkdir "/Volumes/Share 1"
    mkdir "/Volumes/Share 2"
    mount -t afp "afp://usernameA:passwordA@10.0.0.2/Share 1" "/Volumes/Share 1"
    mount -t afp "afp://usernameA:passwordA@10.0.0.2/Share 2" "/Volumes/Share 2"
    Again, if the shares in question aren't standard Apple shares then you'll probably want to change the "-t afp" to "-t smb" for Windows share for example.

    3. Save this file in your root directory as "reconnectshares" (Make sure to uncheck the box that tells it to add a file extension of ".txt" automatically.

    4. Open up a terminal window (or use the one you may have open still from the first part) and in your home directory (type simply "cd " to get back there if you're not there) type the following:

    Code:
    chmod +x reconnectshares
    Now you need to get this script to automatically run at login.

    1. Open up your System Preferences on Machine B. Click on Accounts, then select your user account and click on the "Login Items" tab.

    2. Click the little plus sign and in the following window navigate to your home directory (it should be in your "Places section" as your username or failing that click your main hard drive, then Users, then your username).

    3. Select your reconnectshares script and click "Add". You can click the "Hide" checkbox in the resulting login items list for the script so that it doesn't show anything on login.

    That's now it! Your two shares "Share 1" and "Share 2" should now always be connected correctly on Machine B.

    If you restart Machine A for updates or to play some games in Boot Camp etc then when you come back, things should all reconnect automatically. Restart Machine B and things should reconnect automatically. Obviously, if Machine A is switched off or is currently in Windows then you won't be able to access the shares from Machine B but, in theory, as long as both machines are in OS X, the shares should remain live. If you want, you can also create an alias to the "reconnectshares" script on Machine B on Machine B's desktop so that you can manually reconnect shares if needed.

    Hopefully this will all help someone other than me one day!!

    Phil
     

Share This Page