Showing posts with label Terminal. Show all posts
Showing posts with label Terminal. Show all posts

Wednesday, January 9, 2008

Modifying Terminal's Prompt String, Revisited.

Soon after modifying my prompt string in Terminal, I wasn't too happy about the excessive length that this added to each Terminal Prompt. I also didn't like how it wasn't exactly 100% consistent with the look of Tiger's Terminal. So after a few days I decided to go back to the post that I originally saw this tip from and actually read it a bit more thoroughly this time.

After taking a glance at the table of environment variables, I quickly constructed a string, that I believe is identical to the prompt string that appears in Tiger.

export PS1="\h:\w \u\\$ "

The only thing that I am a bit confused with is the double "\\" near the end of the string and the use of double quotes. Some examples on the blog post use double quotes, whereas some use just single quotes. I'm assuming that these are interchangeable.

Another one I like, is even shorter, and I'll probably be switching in between these two depending on what I'm working on.

export PS1="\h:\w\\$ "

Add a "Message Of The Day" to Terminal.

This one is super simple and can be done in both Tiger and Leopard. I was curious how to do this because I wanted to add the "Welcome to Darwin!" that I missed so much from Tiger's Terminal.

To add a Message of the Day:

1.) Open Terminal and enter the following command.

sudo vim /etc/motd

Since we are not logged into the Root User, we have to precede the command with a sudo, this then prompts you, asking for your administrative password.

(Also Note: that you can use any editor of your choice, now that I know a few of the shortcuts for vim, it is starting to grow on me. "Nano" is also a very easy Text Editor, and the shortcuts are all displayed at the bottom of the screen for your convenience.)

2.) Once you have entered vim, press "i" to enter the "Input mode" and type whatever you'd like for your message of the day. Example: "Welcome to Darwin!"

3.) Push Esc to exit out of "Input mode" and type: ":wq" (explained in this post) to save the file.

4.) Now close and re-open Terminal and you'll now see your new Message of the Day!

Tuesday, January 8, 2008

Spoofing your MAC Address in Tiger.

Spoofing your MAC (Media Access Control) address can be a very handy tool. Most people argue that it is only used for not-so-legit things, but I beg to differ. A very common case where spoofing your MAC address could be both helpful and education is in the area of network trouble-shooting.

Many ISPs (Internet Service Providers) tie the customer's IP address to the MAC address of the computer (or router) connected to the modem. The Physical Layer (Layer 1) is always the first layer that you should check when trouble-shooting a network. This layer includes, modems, routers, computers, cables, etc. So in order to be able to more thoroughly test your network, being able to swap out your current device connected to your modem is key. This can obviously be very tricky when the ISP has locked the IP address to a single MAC address.

Here's where spoofing your MAC address comes extremely handy, and doing it on Mac OS X Tiger, (unfortunately this command does not seem to work in Leopard) and I'm assuming most Linux/UNIX system, is by far the easiest I've ever seen.

1.) Simply open Terminal and type in the following command:

sudo ifconfig en0 lladdr aa:bb:cc:dd:ee:ff

(where aa:bb:cc:dd:ee:ff is the new desired MAC address)

After entering that command, Terminal will ask for your password and now all connected devices connected to the wired ethernet connection should see this new MAC address. And as if this tutorial couldn't get any easier, to revert, simply restart your Mac.

Monday, January 7, 2008

Have Terminal show "ls" Colors by Default.

Working in Terminal is very tedious. And it doesn't help much when all of the files, folders, etc. are the same color. There is a simple way to make it so that when you invoke the "ls" command, it is always color-coded.

Adding this functionality is super simple, and is almost identical to the process I explained in the "show entire path of current directory" post. I was screwing around, trying to get this working at work on our Tiger machine and Dale gave me a few tips on using Vi. He actually suggested that I use Vim (Vi IMproved). Thankfully this is part of Tiger as well as Leopard so I gave it a shot.

In order to add the colors to ls we have to add another line to our .bash_profile file.

1.) Open Terminal

2.) It should start you off in your Home directory, just to make sure type "cd"

3.) Type the following line:

vim .bash_profile

4.) If this file has not been created yet, it will then display a blank screen, however if you've created this file before (maybe you did my post about showing the entire directory path!) then it will show you the previous lines that you have entered

5.) Press "i" to enter "Input mode"

6.) Now enter the following line:

alias ls="ls -G"

7.) Press the escape button. This will take you out of "Input mode"

8.) Type ":wq" and then press Enter (The colon prompts you to enter commands and the w and q commands stand for "Write," and "Quit")

9.) Finally, quit out of Terminal and re-open it. Now when you invoke the "ls" command, it should return the list of files and folders, but this time color-coded!


Before


After



I got most of my information about this tip from this blog, however Leopard differs and does not seem to look at the ".profile" file. After a little bit of experimenting, I found out that instead Leopard seems to be looking at ".bash_profile". However if you are still running Tiger, the blog that I just posted works perfectly! Also see that blog for additional information on what all the color codes mean. Thanks again Dale for a lot of the hints!

Make Leopard's Terminal show the ENTIRE path.

There are quite a bit of changes to Leopard's Terminal, one of which, I am not too keen on. In Tiger, when you were using the Terminal, navigating deep into folders and directories you could easily check the latest prompt to see where exactly you currently were. However soon after messing with Leopard's Terminal, I was saddened to see that it no longer did this, but instead only displays the current name of the directory that you are in.

At first, this didn't bug me to much, however it gets really annoying typing in pwd every-other time just to make sure you are in the right directory

"...am I in /usr/bin/ or am I in /usr/local/bin/... am I in /Library/, or /System/Library/, or am I in ~/Library/?!"

This got old quickly so I decided to ask my Linux mentor, who also happens to be my boss, if he had a solution. He did a little research, we found a solution, and I was so happy to have figured this out I thought I'd post a blog in hopes that I can help other people out who are also bothered by this.

First things first: You'll need to know what "~" means. ~ refers to your "Home" folder. You're Home folder is located in the /Users/ folder in Leopard. A lot of times this is your full name. In my case, my Home Directory is jonathansmith68. The tilde comes handy when you want to cd to your Home Directory quickly.

-Simply enter the following command into Terminal at any time in order to cd (or changedirectory) into your Home Folder.

cd ~


Also note that anytime you open Terminal, by default, it places you in your Home Directory.

Now that we've discussed what the Home Directory is, explaining how to add this functionality will be much easier.

First you'll have to create a file named ".bash_profile" inside your Home Directory. It is under my impression that most, if not all Linux distros already have this file, but it appears that Leopard does not, so you'll have to make it yourself.

Note: Files and Folders that begin with "." are hidden in Leopard. In terminal in order to see these hidden files and folders you must enter the "ls" (list) command along with the "-a" (all) option. This will show all the files and folders located in your current directory.

You can create the ".bash_profile" file in any kind of text editor, whether it be Vi Editor, TextEdit, or TextMate. I'm lazy and didn't want to learn Vi so I decided to just create the file in TextEdit.

1.) Open TextEdit and Enter the following String of text:

export PS1="[\u@\h \w]\\$

2.) Go to File --> Save As...

3.) Uncheck the Box that says "If no extension is provided, use ".txt"."

4.) Name the file ".bash_profile"

5.) Make sure to save this to your Home Directory, this can be found in the sidebar and/or the drop down box and is represented by a small Home Icon.

6.) A Dialog box will pop up telling you that filenames that being with a "." are reserved for the system (hidden files) and asks if you are sure that you want to use the "." in the beginning. Simply click "Use ".""

Once the file has been saved into your Home Directory, all you need to do is quit out of Terminal, and reopen it.

You should now be able to see the full Directory Path at all times!




Before (only shows the current directory, not entire path)


After (shows the entire path of the current directory)


I'd like to give credit to this website for most of the information. Thanks to Mac OS X's Unix roots, it was possible to adapt this to Leopard.