1. Automating SSH logins with Autossh, Automator and Applescript

    When I start up my computer I find myself creating SSH connections to external servers over and over again. Because I’m lazy and I hate repetitive boring stuff I automated the process. Now when my Macbook or iMac starts up, a terminal opens and the connection is made for me.

    The first thing we will need to do is create a public/private key authentication for SSH, so we don’t need to enter a password on each login attempt.

    Creating public/private key authentication for SSH

    1. On the local machine
    First create a .ssh directory in your home directory e.g: /home/username/.ssh
    mkdir ~/.ssh

    The next thing we need to do is to create the SSH keys. Enter the command below and press enter, when asked for a pass phrase leave blank, since our purpose is to automate things.
    ssh-keygen -t rsa

    There will be created two files in the .ssh directory: id_rsa and id_rsa.pub. The pub file has the public key and will be placed on the remote server.

    2. On the remote server
    Copy the id_rsa.pub file to the remote server via SCP:
    scp ~/.ssh/id_rsa.pub username@example.com:/home/username

    If needed create a .ssh directory on the remote machine e.g: /home/username/.ssh, else just move the id_rsa.pub to it:
    mkdir /home/username/.ssh
    mv /home/username/id_rsa.pub /home/username/.ssh/authorized_keys

    Now all that’s left is setting the correct permissions on the key:
    chown -R username:username /home/username/.ssh
    chmod 700 /home/username/.ssh
    chmod 600 /home/username/.ssh/authorized_keys

    Cited, reproduced and extended from Jon Stacey’s blog: http://jonsview.com/how-to-ssh-public-key-authentication thank you for creating that. Posted here for personal reference.

    Installing AutoSSH

    1. Installing AutoSSH
    What is AutoSSH? Taken from autoSSH’s site: “Autossh is a program to start a copy of ssh and monitor it, restarting it as necessary should it die or stop passing traffic. The idea is from rstunnel (Reliable SSH Tunnel), but implemented in C.”, great that’s what we need!

    Before installing you need to have installed a C compiler, since AutoSSH is written in C. So make sure you have that installed or else you’ll get errors during the install. Xcode ships with a C compiler so if you don’t have Xcode installed yet, go to the App Store and install it. When you’re done follow this post on Stack Overflow: http://stackoverflow.com/questions/9353444/how-to-use-install-gcc-on-mac-os-x-10-8-xcode-4-4 then it should be ok.

    Now go to: http://www.harding.motd.ca/autossh/ and download a copy of AutoSSH, next change directory to your download location, mine downloaded AutoSSH to /Users/username/Downloads. Now unzip, configure, make and install the program. Make sure the AutoSSH version corresponds (time of writing 1.4c):
    gunzip -c autossh-1.4c.tgz | tar xvf -
    cd autossh-1.4c
    sudo make install

    Automator and Applescript

    1. Create Applescript app and save as application
    Next open up Automator from your Applications folder. And press “Create a new Automator workflow”.
    Now select the “Utilities” category from the Library and then “Run AppleScript” on the actions column. Paste the folowing Applescript code in the editor:
    on run {input, parameters}
    	tell application "Terminal"		
    		do script "/usr/local/bin/autossh -M 20000 -p 22 -D 1080 username@example.com"	
    	end tell	
    	return input
    end run

    Change: username@example.com to your username and server, if SSH is running on another port change that as well. Now save the Automator file as an application.

    2. Run app on startup
    1. Select the ‘System Preferences’ item from the Apple menu.
    2. Click the ‘Accounts’ icon in the System section of the System Preferences window.
    3. Click on your user name in the list of accounts.
    4. Select the ‘Login Items’ tab.
    5. Click the + (plus) button below the Login Items window. A standard Finder browsing sheet will open.
    6. Navigate to the program you just created. Click once on it to select it, and then click the ‘Add’ button.

    That’s all.
    1. rigovanov reblogged this from luisroman
    2. tsutomuonoda reblogged this from luisroman
    3. luisroman posted this