Archive for the ‘Mac OSX’ Category

Awhile back, I posted a simple expect script for automating SSH sessions on a Mac.  Here is a similar script for automating telnet sessions.

As with the SSH script, create a text file using the naming convention of your choice and .sh for the extension.  In the file, type the following:

expect -c  ‘spawn telnet <ip-address>
expect “Username:”
send “<username>\r”
expect “password:”
send “<password>\r”

That is all you need to log in.  If you need to add an enable password, add the following to the script:

expect “>”
send “enable\r”
expect “password:”
send “<password>\r”

And there you have it.  If you want to log the output to a file, create a file to receive the output and then append the following to the last line:

| tee -a /path/filename.txt

Let me know if you find this helpful.

Read Full Post »

As a network engineer, I’ve discovered that one of the many great things about Apple’s OSX (possibly the best thing)  is that it is built on UNIX.  This means that many of the tools that come with standard UNIX distributions are included in OSX.  This, in turn, means that engineers don’t need to go buy additional tools for everyday tasks.  One of those tools is an SSL client.  OSX comes with OpenSSH already installed.  Many engineers probably know this, but they may be put off by the manual aspect of working with OpenSSH.  You have to know the IP address of the host you want to manage and type in the SSH command and all the credentials.  This is not necessary with clients like Putty, Secure CRT or TeraTerm.  Sessions can be created and saved along with credentials, making your management sessions only a mouse click away.  I’m here to tell you that you can do all of this from the terminal without having to reach for the mouse or touchpad.

You may already know that you can create a simple shell script that will launch OpenSSH and connect to a host just by executing the script.  This is done by:

1. Creating a file with a .sh suffix using either touch, or vi or your favorite text editing tool as follows: vi remote host.sh.

2. In your script, type the command you want to execute: ssh <username>@<ip_address>

3. Change the permissions on the script to make it executable: sudo chmod +x remotehost.sh

4. Execute the script using a ./, as in: ./remotehost.sh

And that’s it.  Now, if you want to automate the login process, you need only take advantage of the expect feature.  Expect is a powerful tool that can be used in a number of ways.  In this case, we will use it to launch the SSH client, tell it to expect a prompt, and how to respond to that prompt:

expect -c ‘spawn ssh <username>@<ip_address> ; expect password ; send “<your_password>\n” ; interact’ 

Voila, you’re done.  If you’re hyper organized, create a Sessions folder with as many sub-folders as you like and organize your scripts in any way you like.  You can launch your script from the terminal no matter where you are in the directory structure.  Just use the ./ and add the path to your scripts.  I’m sure there is a shortcut that will allow you to cut out the full path somehow, but I haven’t got to that yet.

So don’t go out and buy SecureCRT for Mac, and skip the freeware.  Just use the command above, or any variant that works for you, and save yourself the money and a few mouse clicks.  Leave a comment and let me know if you found this useful.

UPDATE: If you would like to log the output from your terminal session to a file, you need only append the following to your script:

| tee -a /<path>/<filename>

This will create a file and log the output automatically.  The -a will tell tee to append future text, so you will always have a running log.  I use the path that leads to the connection script and I give the file the same name as the script, with the exception of using a .log or .txt suffix.

I’m working on adding a timestamp so that I will know what changes were made when.  As soon as I figure out how to do that, I’ll post it here.

Read Full Post »

This is simply a list of handy commands that can be run from the terminal of a Mac and will save you (and me) a few mouse clicks.  Note: It may be necessary to run most of these commands with sudo.  Feel free to make suggestions for inclusion.

dhclient -r – This will release your DHCP obtained IP address

dhclient – This will request a new IP address from a DHCP server

ifconfig <interface> <ip address> netmask <mask> – This will manually set your IP address on the specified interface

The following commands will disable the firewall:

launchctl unload /System/Library/LaunchAgents/com.apple.alf.useragent.plist
launchctl unload /System/Library/LaunchDaemons/com.apple.alf.agent.plist

The following commands will re-enable the firewall:

launchctl load /System/Library/LaunchDaemons/com.apple.alf.agent.plist
launchctl load /System/Library/LaunchAgents/com.apple.alf.useragent.plist

id – This simple command will display your user ID and the IDs of all the groups to which your account belongs

groups – This command will display the group names to which your account belongs

sudo launchctl list – This command will list the services that are running on the machine

route add -n <network/mask> <gateway> – This command will temporarily add a static route to the machine

/bin/launchctl load -w /System/Library/LaunchDaemons/ftp.plist – Will turn on the FTP server if it is not already running.

/bin/launchctl unload -w /System/Library/LaunchDaemons/ftp.plist – Will turn off the FTP server

Read Full Post »

This for all those folks who want to know how to connect to a console port on a Cisco device (or any console, really) using a Mac.  Because OSX is built on a Unix derivative, it includes a number of tools that are built in, including a little application called screen.  You don’t need SecureCRT, Putty, or any other application.  Screen is a powerful application with a number of different uses, but the manual page is as cumbersome as any.  Using it to connect to a Cisco device, however, is easy.  You only need to take three steps to use it successfully.

First, connect your Mac to the console port using any Mac compatible USB/serial adapter.

Second, from the terminal, run the ls /dev/tty.* command.  This will show you the name of the USB/serial adapter as it appears in the /dev directory.

Third, from the terminal, run the screen /dev/tty.<device name> 9600,-cstop,-cs8,-parenb command.  This invokes Screen using the USB/serial adapter, instructs it to use 9600 baud, one stop bit, 8 data bits, no parity.

That’s it.  When you are finished, press and hold the <control> <a> key combination and then press the <d> key.  This will disconnect screen and release the terminal.  If you want to know more about how Screen works and all that it can do, read the man page.

Read Full Post »

Have you ever been frustrated by your desire to copy music from your iPod to your Mac?  Let’s say that after years of flawless performance your Mac dies and you never got around to making that backup you’d been planning.  All of your music files are gone, except for the ones that were synced to your iPod. You would love to be able to copy the music from your iPod to your Mac, but iTunes does not include an option for that.  What are your options?  You could search for and buy an application that will do this for you, or you could follow the steps below.  DISCLAIMER:  Some of these steps require that you enter commands via the terminal.  This is potentially dangerous, so proceed at your own risk.

1.  In iTunes, under Preferences, Devices, check the box labeled “Prevent iPods, iPhones, and iPads from syncing automatically.  This will allow you to connect your old iWhatever to the new Mac without erasing the contents.

2.  Press and hold the Command and Option keys

3.  While holding the keys, connect the iPod to your Mac.

4.  Launch the terminal application, located under Applications – Utilities

5.  In the terminal, type the lines below.  The first ensures that the Finder will display hidden folders, the second closes and restarts all instances of the finder.  These are case sensitive:

defaults write com.apple.finder AppleShowAllFiles TRUE

killall Finder

6.  Double-click on the iPod icon, or select the iPod icon from the Finder.

7.  Open the “iPod Control” folder

8.  Select the “Music” folder

9.  Right-click (or command-click) to bring up the menu

10.  Select “Copy”

11.  Right-click (or command-click) on a blank area on the desktop and select “Paste”.  Your Mac will copy the contents of the Music folder to your desktop.

12.  Once the copy is complete, return to the terminal window.  Here is where things get just a little tricky, but don’t panic.  You can do this.

13. Type the following in the terminal.  It will tell you where you are in the file structure:


The output should look something like /Users/<your name or machine name>.  If that is correct, type the following:

cd Desktop

14.  If that is not the output, type the following.  It will change the directory to the desktop

cd /Users/<your username>/Desktop

15.  Type the following to change the ownership of the Music file to your new computer.  After you hit enter, you will be prompted for your password.  If you do not take this step, iTunes will not be able to see the folder in order to import the files:

su chown -fhv -R <your username> Music

16.  Type the following to change the permissions on the Music file.  This will allow iTunes to import the files:

su chmod -R 777 Music

17.  At this point, I would copy the contents of this folder to the Music folder in the Finder.

18.  Now you can run the import from iTunes.  Open iTunes, go to “File”, and select “Add to Library”.  This will open a Finder window. Navigate to the folder where your music files now reside, select it, and click Choose.  iTunes will import all of the music in the folder and will be “synced” to your old iPod.

19.  Last, but not least, you want to type the following in your terminal to hide the system folders again:

defaults write com.apple.finder AppleShowAllFiles FALSE

killall Finder

It’s a good bit of work, but it will save you a few dollars by not having to buy a third-party tool to take these steps for you.

Good luck!

UPDATE: If you find that your music folders are hidden after copying them, take the following steps.

1.  Using the terminal, navigate to the folder where the files are located.  If you copied your music folders to the iTunes music folder, the command should look something like this:

cd /Users/<your username>/Music/iTunes/

2.  Run the list -la command to list all of the folders, hidden or not.  Your old music folders should be there.  If not, verify the location of the folders and navigate to that folder using the cd command as above.

3.  Change the hidden flag by typing the following command:

chflags nohidden <filename>

If you music folders share a common first letter, or a string of first letters, you can use the * character as a wildcard.  For example, my folders all had a F at the beginning of the folder name.  I used the command chflags nohidden F* and that covered all of the folders.

4.  Close and re-open iTunes and run the “Add to Library” function.  It will now find your music files.

Again, good luck and let me know if this works for you.

Read Full Post »


Mac OSX includes a free TFTP server that is fairly easy to use once you get the hang of it.  There are TFTP server applications for Mac that you can download, but why bother when you already have one?  All it takes is a little know-how and a little practice to become a Mac TFTP guru.  I can help you with the know-how part.  DISCLAIMER:  This is not meant to be a detailed tutorial guide.  I do not include command descriptions or explanations.  Those can be found elsewhere.  Some of the commands may be dangerous, so if you have any doubts about your ability to safely manage your Mac, go no further.  There, you’ve been warned.

There are two major steps you need to follow to use the default TFTP server.  First, you must enable the the TFTP server.  Second, you must disable your firewall.

For security reasons, the OSX TFTP server is disabled by default.  Take the following steps to enable it:

  1. Launch the terminal application.
  2. From the terminal, type sudo launchctl
  3. When prompted, type your password or the admin (root) password
  4. Type load -F /System/Library/LaunchDaemons/tftp.plist

Voila, your TFTP server is now running.  To keep things simple, I keep the terminal open while I’m running the TFTP server.  It reminds me that I need to turn it off later (see below).  In order to copy things to your Mac, it will be necessary to temporarily disable your firewall.  Take the following steps to disable your firewall:

  1. Launch the “System Preferences” application.
  2. Click “Security”
  3. Click the “Firewall” button.
  4. Click the lock and enter your credentials to make changes
  5. Click the “Stop” button.

Now your firewall is disabled and you are ready to receive files.  NOTE:  When copying a file from a remote device (a Cisco router, for example), you must include the full path to the TFTP directory in the copy command.  The full path will be something like: /private/tftpboot.  This is where you need to spend some time practicing to get the syntax right.  NOTE:  the tftpboot directory is not visible from the finder.  You can only see it from the command line.

When you have finished using your TFTP server, it is a good idea to re-enable the firewall and disable your TFTP server.  Re-enabling the firewall is easy enough; simply go back to the System Preferences application and click the “Start” button under the firewall.  Disabling the TFTP server requires entering a couple more commands as follows:

  1. From the same terminal where you loaded the TFTP server, type unload /System/Library/LaunchDaemons/tftp.plist
  2. Type exit to unload the launch control

That’s all there is to it.  It only sounds like a lot.  Once you get used to it, it is easier than messing with the third party TFTP applications that are floating around the Internet.

Good luck!

UPDATE:  I noticed that there are occasional issues with permissions when using the tftp server.  If you run into similar issues, take a look at your tftp.plist (cat /System/Library/LaunchDaemons/tftp.plist).  Look under the <key>ProgramArguments</key> section.  If you see <string>-i</string> in the array, you need to edit the file.  You can either remove that line and replace it with a -s flag, or add the -s flag.  I had to experiment a bit before I found a combination that worked.  The end result could look like any of the following:


or try this:


or this:


Remember to make a backup of your existing file before making any changes.  Again, this is not a detailed tutorial.  If you are uncomfortable with changes like these, don’t make them.

Have fun!

Read Full Post »

%d bloggers like this: