Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Shared on Facebook

Shop at Amazon

Ad

OSXDaily on Facebook

Search Command Line

How to Set Up a Password-less SSH Login

May 25, 2012 - Leave a Comment

Set up password-less SSH logins

Setting up passwordless SSH logins is a great way to speed up connections to regularly accessed remote Macs and unix boxes. Because OS X doesn’t include the ssh-copy-id command, you will have to use cat or scp to copy over your ssh key. This is how to set everything up, it only takes a minute or so.

First, on the local machine you will want to generate a secure SSH key:

ssh-keygen

Walk through the key generator and set a password, the key file by default goes into ~/.ssh/id_rsa

Next, you need to copy the generated key to the remote server you want to setup passwordless logins with, this is easily done with the following command string but you can use scp if you’d prefer:

cat ~/.ssh/id_dsa.pub | ssh user@remotehost 'cat >> ~/.ssh/authorized_keys'

This command takes the generated SSH key from the local machine, connects to the remote host via SSH, and then uses cat to append the key file to the remote users authorized key list. Because this connects with SSH to the remote machine you will need to enter the password to use this command.

Finally, confirm that you can now login to the remote SSH server without a password:

ssh user@remotehost.com

Assuming initial setup went as intended, you will connect to the remote machine without having to log in. You can shorten the connection steps even further by creating an alias in bash_profile so that you are only required to type a short command to immediately connect to the specified remote server.

There are some obvious potential security risks with using ssh without a password, the best way to mitigate that is to lock down the client machine with lock screens for screen savers and sleep and boot passwords, all of which you should be utilizing anyway.

Install wget in Mac OS X Without Homebrew or MacPorts

May 22, 2012 - 9 Comments

Install wget in Mac OS X

The command line tool wget lets you retrieve a group of files from FTP and HTTP protocols, it’s a very useful utility for web developers and powerusers to have around because it lets you do things like perform quick and dirty site backups and even mirror websites locally.

This approach is going to build and install wget in OS X from source, this means you’ll need Xcode and the Unix dev tools (free @ Mac App Store) installed, but it has the benefit of eliminating the need of a package manager like Homebrew or MacPorts.

Assuming you have Xcode and the command line tools installed, launch Terminal and enter the following commands:

First, use curl to download the latest wget source:
curl -O http://ftp.gnu.org/gnu/wget/wget-1.13.4.tar.gz

Next we use tar to uncompress the files you just downloaded:
tar -xzf wget-1.13.4.tar.gz

Use cd to change to the directory:
cd wget-1.13.4

Configure with the appropriate –with-ssl flag to prevent a “GNUTLS not available” error:
./configure --with-ssl=openssl

Build the source:
make

Install wget, it ends up in /usr/local/bin/:
sudo make install

Confirm everything worked by running wget:
wget --help

Clean up by removing wget source files when finished:
cd .. && rm -rf wget*

You’re all set, enjoy wget in Mac OS X.

Turn a Python, Perl, Ruby, or Shell Script Into a Self Contained Application for Mac OS X

May 22, 2012 - 2 Comments

Script turned into a self contained Mac OS X app

Platypus is an excellent utility that lets you turn virtually any script into a self-contained Mac OS X application. Free and remarkably simple to use, Platypus will support just about any shell script, Perl, Python, PHP, Ruby, Tcl, AppleScript, Expect, and even other scripting languages.

Using the app is easy, drag and drop a script onto the application, give it a name, assign an Output type (progress bar, script output, webkit view, etc), set drag & drop support or not, and adjust a few other variables as necessary. When done configuring, click “Create” to build a self-contained application that holds the script inside. The resulting application is completely portable and runs independently, letting you use the app on your own Mac or elsewhere, allowing for even novice users to perform complex tasks and run scripts that would otherwise be beyond their skill level to even execute.

The possibilities are practically endless so let your creativity go wild, because it’s Playtpus that makes apps like the troubleshooting tool Consultant’s Canary possible.

For a basic yet practical use case, build an app to perform a necessary repetitive task and add it to a users Login Items or put it in the Dock or Launchpad for easy novice access. It certainly beats trying to explain to someone how to run a bash script!

Use Growl Notifications to Alert When Command Line Tasks Have Completed

May 17, 2012 - 4 Comments

Growl Notification sent from the command line with growlnotify

A recent tip covered how to announce when a command line task finished by using OS X’s text-to-speech abilities. The obvious downside to that method is the sound makes it less useful to those who are using Macs in quiet environments like offices, schools, or libraries. An alternate solution is to use growlnotify to create a silent notification when a command line task has finished.

You will need the following for this to work:

Assuming both Growl and growlnotify have been installed, append growlnotify to the end of another command and use the -m flag to specify the notification message to use after the initial command has finished running. For example:

make install && growlnotify -m "Install Completed"

The Growl message “Install Completed” will appear when make install has finished running.

Growl also makes it simple to send notifications to other hosts running growl, this can be done with the -H flag and by specifying an IP address. This is helpful if you’re compiling something large on a desktop Mac and want to send the completion notification to a MacBook Air while you’re working elsewhere.

The notification icon and other details can be modified through growlnotify command as well, use the –help flag to see all the options.

Thanks to Theo & Jason for the tip idea

Make Mac OS X Speak (or Sing) the Output of Any Command Line Task

May 11, 2012 - 2 Comments

Speak the output of a command line task

Along the lines of vocally announcing task completion within the command line, you can also have Mac OS X speak the output of any executed command.

This is really easy, all you need to do is pipe the output of any command directly to “say” like so:

uptime | say

In that example, this will speak the output of uptime using the default OS X text-to-speech voice, though you can change the voice to anything else with the -v flag:

uname -a | say -V Samantha

Taking this a step further, it is possible to export the spoken command output to an audio file. Here’s a pointless example of saving the spoken output of the openssl manual page as an m4a file:

man openssl | say -v Samantha -o "talkopenssltome.m4a"

If you want to get ridiculous, you can use one of the singing voices to, well, sing the output of a command. And who doesn’t want an audio file of of the robotic Cellos voice or the awfully perky Good News singing the output of a manual page? Put it on your iPod for the drive home.

man openssl | say -v Cellos -o "serenadingmanpages.m4a"

If you don’t like the bundled voices you can always add new voices to OS X for free through Speech preference panel.

Get creative, because the ultimate utility of this is entirely up to you.

Thanks to Greg P for pointing this out in the comments

Announce When a Command Line Task is Completed in Mac OS X

May 8, 2012 - 13 Comments

Announce the completion of a command in Mac OS X Terminal

By appending the say command to the end of another command, Mac OS X will vocally announce when the initial task has finished running successfully. For example, to have OS X announce that a particular script has finished running the command could be:

python backup.py && say "jobs done"

The important part is the “&& say” portion, which can also be customized with other voices from Mac OS X’s text to speech options by using the -v flag followed by a voice name, like so:

dscacheutil -flushcache && say -v Alex your cache has been cleared sir

This is perfect for running scripts, making svn/git commits, compiling code, and other tasks that can take an indeterminate amount of time to complete and where it’s easy to become distracted by facebook^H^H^H^H^H^H^H^H other work.

This great tip was posted by @niels on Twitter, follow @osxdaily there too to get our latest posts and updates.

Empty Contents of a Specified File Without Deleting via the Command Line

May 7, 2012 - 3 Comments

Remove Contents from a specified file without deleting the file

If you’re working at the command line and need to quickly empty the contents of a file, throw a greater than symbol and a space in front of the filename like so:

> filename

All content within the target file will be immediately removed without warning, leaving it blank while retaining the files existence, filename, and permissions. This is often preferable and quicker than manually deleting and recreating a file.

A good practical example is for clearing out the contents of log files, for example:

> ~/Library/Logs/updates.log

You can also use this command to create a new 0 byte file at the location, similar to using the touch command.

Generate and Set a Random Valid MAC Address from the Command Line in OS X

May 2, 2012 - 3 Comments

Generate and set random MAC address in OS X

We’ve shown you how to generate MAC addresses randomly and then how to go about changing a MAC address in OS X Lion and OS X Mountain Lion, but why have those be two separate actions? Using the command line, you can combine the two events into a single action to generate a valid MAC and then set it immediately.

Launch the Terminal and paste the following onto a single line:

openssl rand -hex 1 | tr '[:lower:]' '[:upper:]' | xargs echo "obase=2;ibase=16;" | bc | cut -c1-6 | sed 's/$/00/' | xargs echo "obase=16;ibase=2;" | bc | sed "s/$/:$(openssl rand -hex 5 | sed 's/\(..\)/\1:/g; s/.$//' | tr '[:lower:]' '[:upper:]')/" | xargs sudo ifconfig en0 ether

There is no confirmation or output, the MAC address is set immediately which you can verify with the following command:

ifconfig en0 |grep ether

You will probably need to reconnect to the wireless router after issuing the command, and in some cases turn wi-fi on and off again.

If you intend on using this often, setting up an alias in .bash_profile would be a good idea to avoid having to cut and paste the massive block of text.

Thanks to osmium for the great tip

Open Terminal Faster by Specifying a Shell

Apr 30, 2012 - 2 Comments

Open Terminal faster by specifying a login shell

Whenever a new Terminal window is launched, a variety of system logs are read to provide information on the last login. One way to speed up launching a new terminal window is to delete those .asl log files, but you can achieve a substantial speed boost by simply setting Terminal to open a shell rather than /usr/bin/login. This is achieved through the same means of changing the shell and is just a few quick steps within Terminal settings:

  1. Open Preferences from within the Terminal pulldown menu
  2. Click the “Startup” tab
  3. Look for “Shells open with:” and check the box next to “Command (complete path):”

The default shell is /bin/bash but you’re free to choose another.

Specify a shell within Terminal to speed up opening new Terminal windows

Once this setting has been adjusted, launch a new Terminal window and you should immediately notice the difference. This works because the many .asl log files are bypassed, but as a side effect you will lose the “Last login” MOTD message at login unless specified otherwise.

Thanks for the tip Darren & Yorkster

Get Extended CPU Information from the Command Line

Apr 28, 2012 - 1 Comment

Get extended CPU information from the command line

Using sysctl we can get extended information on a Macs processor, covering everything from CPU brand and identifier, clock speed, number of cores, thread count, thermal sensor data, cache size, and some significantly more technical information.

Launch the Terminal and enter the following command:

sysctl -a | grep machdep.cpu

A limited sample of the output looks as follows:
$ sysctl -a | grep machdep.cpu
machdep.cpu.max_basic: 13
machdep.cpu.max_ext: 2147483656
machdep.cpu.vendor: GenuineIntel
machdep.cpu.brand_string: Intel(R) Core(TM)2 Duo CPU P8600 @ 2.40GHz
machdep.cpu.family: 6
machdep.cpu.model: 23
machdep.cpu.extmodel: 1
machdep.cpu.extfamily: 0
machdep.cpu.stepping: 10
machdep.cpu.feature_bits: 3219913727 67691517
machdep.cpu.extfeature_bits: 537921536 1
machdep.cpu.signature: 67194
machdep.cpu.brand: 0
machdep.cpu.features: FPU VME DE PSE TSC MSR PAE MCE CX8 APIC SEP MTRR PGE MCA CMOV PAT PSE36 CLFSH DS ACPI MMX FXSR SSE SSE2 SS HTT TM PBE SSE3 DTES64 MON DSCPL VMX SMX EST TM2 SSSE3 CX16 TPR PDCM SSE4.1 XSAVE
machdep.cpu.extfeatures: SYSCALL XD EM64T LAHF
machdep.cpu.logical_per_package: 2
machdep.cpu.cores_per_package: 2

The sysctl command is fairly powerful and can provide extensive technical information, we have used it before to get basic processor information, see if a Mac is 64 bit compatible, discover the precise boot, sleep, and wake times of a Mac, find CPU clock speed, and to get assorted other tidbits about Macs hardware.

Thanks for the tip nicentral