2010-11-28

Taking More Control: Even More BASH Tips

This is an extension of my last BASH post, Taking Control: More BASH Tips. In that post we discussed some of the control commands that are available for clearing the screen, searching the history and moving the cursor around. This time we are going to talk about modifying the text in the current command.

CTRL+_ or CTRL+-
Let's start out with the thing that we all need sometimes, undo. Control and underscore or dash will undo the last control command done in bash. It WILL NOT undo the last command executed on the system. For example if you cut all of the text from the cursor to the beginning of the line but really ment to cut the last word you can use undo.

#I want to change badger to fox.
j2consulting:~ user1$ The quick brown badger jumps over the lazy dog.
#I hit CTRL+u instead of CTRL+w
j2consulting:~ user1$ jumps over the lazy dog.
#CTRL+- saves me from my mistake
j2consulting:~ user1$ The quick brown badger jumps over the lazy dog.


CTRL + u, CTRL + w, CTRL + k
As I showed in the previous example, you can remove text from the current command. Using these control commands you can delete text based on the current cursor position. Control and u will delete all of the text from the point of the cursor to the beginning of the command. Control and k will delete all of the test from the point of the cursor to the end of the command. Finally, control and w will delete the previous word in the command.

#I want to change badger to fox.
j2consulting:~ user1$ The quick brown badger jumps over the lazy dog.
#I move the cursor to the space between badger and jumps and then hit CTRL+w
j2consulting:~ user1$ The quick brown jumps over the lazy dog.
#Now I can add fox.
j2consulting:~ user1$ The quick brown fox jumps over the lazy dog.
#I don't like the end so I move the cursor to the space between fox and jumps and hit CTRL+k
j2consulting:~ user1$ The quick brown fox
#And now I change the ending.
j2consulting:~ user1$ The quick brown fox hides while the dog chases him.
#But now it's just wrong and I want to delete everything from the end to the beginning so I hit CTRL+u
j2consulting:~ user1$


CTRL + y
So you have just got done cutting all kinds of text, what is the next logical thing? Paste. Any of the text you cut using the previous 3 commands can be pasted into the line. Let's switch the sentence around to put the dog first.


j2consulting:~ user1$ The quick brown fox jumps over the lazy dog.
#Putting my cursor on the . and hitting CTRL+w I cut the previous word.
j2consulting:~ user1$ The quick brown fox jumps over the lazy .
#Now I put my cursor between fox and jumps and hit CTRL+y
j2consulting:~ user1$ The quick brown foxdog jumps over the lazy .
#Now I put my cursor on the d in dog and hit CTRL+w
j2consulting:~ user1$ The quick brown dog jumps over the lazy .
#Finally I put my cursor on the . again and hit CTRL+y to paste the fox
j2consulting:~ user1$ The quick brown dog jumps over the lazy fox.


CTRL + t
It is a fairly common mistake to transpose two characters while typing. Control and t can help us quickly fix this by swapping the previous two characters.

j2consulting:~ user1$ Teh
#Oops, I ment to spell The, hit CTRL+t
j2consulting:~ user1$ The
#This can also work if you want to move the cursor to the mistake.
j2consulting:~ user1$ Teh quick brown fox jumps over the lazy dog.
#By placing the cursor between the h and q and hitting CTRL+t we can fix the mistake.
j2consulting:~ user1$ The quick brown fox jumps over the lazy dog.


By using the tricks in this and previous BASH posts you can significantly reduce the time it takes you to get things done. If you know of any other tricks I'd love to hear about them in the comments.

2010-11-23

Taking Control: More BASH Tips

Between the numerous servers I manage via ssh and my personal machines I spend a lot of time in the BASH shell and, as with anyone and who uses a tool for a long time, I have discovered numerous shortcuts to getting things done efficiently. I wrote a couple months ago about using ! and $ to manipulate previous commands.
This time I am going to talk about moving the cursor around in the current command and manipulating the window.

CTRL + l
Before we get started let's clean up by clearing our screen. Control and l will clear the screen without modifying any of the text on your current line. It is very helpful for keeping your terminal clean or preparing it for a large amount of output that your next command may bring.

CTRL + r
This is probably one of the most frequently used control combinations. Control and r allows you to pull up a command from your history by typing in and having it match the last used command with those characters. For example, if you used a command yesterday that had very complex syntax and can't remember what it was, you could hit ctrl+r and then start typing the part of the command that you know.

j2consulting:~ user1$ echo The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
(reverse-i-search)`fox': echo The quick brown fox jumps over the lazy dog.


CTRL + a and CTRL + e
There are many times when you need to jump to the beginning of the line to fix a command and then jump back to the end to finish what you were typing. Control and a as well as Control and e take care of this for you. Say I wanted to use print instead of echo in the previous example. I could hit the up arrow, CTRL+a, change echo to printf ", CTRL+e, add \n" and be done.

j2consulting:~ user1$ echo The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
j2consulting:~ user1$ printf "The quick brown fox jumps over the lazy dog.\n"
The quick brown fox jumps over the lazy dog.


ESC, ALT, CTRL + b and ESC, ALT, CTRL + f
In addition to these there are commands to move back and forward one word at a time. I don't frequently use these because they have mixed success depending on which terminal emulator is used. If you are on a mac on the local terminal esc and then b will move you back one word and esc then f will move you forward one word. If you are on a mac and ssh'd into a system CTRL + the left arrow will move you back one word and CTRL + the right arrow will move you forward one word. I have also read about ALT + b and ALT + f working on different systems. Experiment with this one but because of the inconsistency I tend not to depend on it.

There are more control commands available and I will be looking at those in my next post. Until then happy BASHing.

2010-11-12

rscurl: A Rackspace Cloud Server command line tool

rscurl.sh
License: GPL
Download: https://github.com/jsquared/rscurl

After looking around for a tool that would allow me to manage my Rackspace Cloud Servers from the terminal and not finding anything I decided to write my own. My primary goal for this was to have a tool that could be used from the shell or used in scripts to manage and automate the Rackspace Cloud Servers. I have used Amazon's EC2 API Tools in the past to manage hosts on their service and found it frustrating that they were written in Java, requiring anything that used them to have Java running on it. Because of this I made it a secondary goal to use tools that are commonly available to systems that have bash. Currently that has allowed me to use the script on Apple OS X, Red Hat based systems (CentOS), and Debian based systems (Ubuntu) without any modifications or special permissions on the systems.

rscurl allows you to list your servers, server images available to you ( including the images you make from your servers ), and server flavors ( the memory and disk combinations ). In addition to that you can create, delete, resize and reimage servers as well as create and delete images. All of the options except for the ones that specifically list data have a quiet option so that they can be used in automation scripts. To use rscurl you will need your Rackspace username and api key. The username is the same one you use to log into the web interface and the api key can be found in the Rackspace web interface under Your Account, API Access.

rscurl -u username -a apiKey -c command [ -s serverID ] [ -n name ] [ -i imageID ] [ -f flavorID ] [ -q -h ]
v 0.1
rscurl is a command line tool for managing Rackspace Cloud Servers. It uses curl, awk, sed,
and tr to accomplish this in the hopes that it will work on most systems that use bash.

-u Your rackspace username.
-a Your rackspace api key, found on your rackspace cloud dashboard under Your Account, API Access.
This cannot be your password.
-c command, possible commands are:
list-servers - Lists all the servers you have on your account.
list-flavors - Lists all the types of server that are available to you.
list-images - Lists all the server images that are available to you.
create-server - Creates a new server
requires an imageID (-i) and flavorID (-f)
optional name (-n)
delete-server - Deletes a server, requires serverID (-s).
DANGER: Server deleted without prompt, be sure.
rebuild - Rebuilds a server with the new image, all data will be lost.
requires an imageID (-i) and serverID (-s)
resize - Resizes a server, requires flavorID (-f)
confirm-resize - Confirms a recently resized server, after 24 hours it is done automatically.
requires serverID (-s)
revert-resize - Reverts a recently resized server to the previous size.
requires serverID (-s)
reboot - Reboots a server, requires serverID (-s)
force-reboot - Forces a server to reboot, equivalent to pulling the power.
Requires serverID (-s)
create-image - Creates a new image based on an existing server.
Requires serverID (-s), optional name (-n)
delete-image - Deletes a server image that you own.
Requires imageID (-i)
-s Server ID, required for some commands. To see the servers run list-servers.
-i Image ID, required for some commands. To see the images run list-images.
-f Flavor ID, required for some commands. To see the flavors run list-flavors.
-n Name of server(required) or image(optional) when creating them.
-q Quiet mode, all commands except list-* will exit quietly
-h show this menu

This is my first attempt at writing something like this and there are most defiantly bugs but I think that it's ready for other people to look at. I have licensed it under the GPL and stored it on github in the hopes that others will find it useful and possibly contribute. If this is a tool you can use or would like to contribute to please check it out at https://github.com/jsquared/rscurl.

2010-11-08

Installing Nagios on Ubuntu 10.10

Nagios is a powerful monitoring software package that basically consists of a web interface, a scheduling and evaluation engine, and various scripts or plug-ins. It is a widely used monitoring tool due to it's flexibility and stability. I've personally seen it being used at numerous operation centers and set it up for countless clients. Until recently I had never used Ubuntu as a nagios host but that has changed and the difference in setup was worth documenting. This

I'll be starting with a basic install of Ubuntu Server 10.10, specifically the Rackspace Cloud Server image and all commands will be run as root ( sudo su - ) to simplify things. After building it I like to make sure that I'm all patched up before starting.

apt-get update
apt-get upgrade


No Compiling?
If you have ever built a nagios system in the past you have probably had to go to nagios.org, download the source, compile it and install it. Ubuntu keeps nagios in their repository so there is no need for that.

apt-get install nagios3

This will prompt you to install a large number of packages including apache2, php, postfix, samba, and a slew of libraries all used to either present the nagios interface or support the underlying scripts. The install will prompt you to set the postfix ( mail ) settings. Internet Site and your domain name are probably what you want to use there. It will also prompt you for the nagiosadmin password, this is for the htaccess on the website. Once all of the packages are done installing you have a working nagios install. You can verify this by going to the web interface at http://your.server.ip/nagios3 use the username nagiosadmin and the password you set during the install.


Where are the files?
Traditionally nagios installs into /usr/local/nagios as a complete package. The way the ubuntu package installs these files are put into the larger os layout. Below is a rough cheat sheet as to where things ended up.

Purpose ................... Old ..................................... New
binary ..................... /usr/local/nagios/bin/nagios ... /usr/sbin/nagios3
configuration files ... /usr/local/nagios/etc .............. /etc/nagios3 & /etc/nagios-plugins/config
plugins ................... /usr/local/nagios/libexec ........ /usr/lib/nagios/plugins
website files ........... /usr/local/nagios/share ........... /usr/share/nagios3/htdocs
command file ......... /usr/local/nagios/var .............. /var/lib/nagios3

One last hurdle.
At this point you have a very basic nagios system that is functioning, almost. If you are in the web interface and try to use any of the links under Service Commands or Host Commands you will see this error:

Sorry, but Nagios is currently not checking for external commands, so your command will not be committed!

Like the error says, external commands are not enabled. To fix this edit /etc/nagios3/nagios.cfg, find the line check_external_commands=0 and change it to check_external_commands=1

If you restart nagios now and try the commands again you will get another error:

Error: Could not stat() command file '/var/lib/nagios3/rw/nagios.cmd'!
The external command file may be missing, Nagios may not be running, and/or Nagios may not be checking external commands.
An error occurred while attempting to commit your command for processing.

This error is coming up because the apache process does not have the permission to write to the file that nagios checks for the commands you are requesting. The fix for this is to allow the www-data group read and execute permission to the directory where the command file lives and then add the www-data user to the nagios group because the command file gets created with nagios:nagios as the owner:group and 660 as the permissions.

chmod g+rx /var/lib/nagios3/rw
usermod -a -G nagios www-data
/etc/init.d/apache2 restart
/etc/init.d/nagios3 restart

Now the host and service commands will work and you can start configuring the rest of your nagios setup.

2010-10-10

Maverick Meerkat's clever coming out party.

In a clever move by Canonical the next release of Ubuntu, 10.10, was released today, the tenth day of the tenth month of the tenth year. This release, named Maverick Meerkat, only has minor upgrades to most packages that effect servers with the only major upgrade being to Eucalyptus. It does include the update to 3.2.1 for Nagios which includes a slightly improved site design as well as bug fixes which I was happy to see.

This release also serves as a reminder that Ubuntu 9.04 ( Jaunty Jackalope ) will enter end of life in 13 days, October 23. What does this mean? From Ubuntu's official announcement: "At that time, Ubuntu Security Notices will no longer include information or updated packages for Ubuntu 9.04."

To find out more about the release, check out the release notes or download and install it for yourself. You can also take Ubuntu up on it's offer of a free hour of Maverick in the cloud via their Ubuntu on Cloud 10 project.

2010-08-15

Using Screen to share your terminal.

As a consultant who works with remote clients and, before that, a telecommuting systems engineer for companies who used linux I have often encountered the problem of collaborating inside of a remote client that does not have X and who's only access is ssh. Servers with GUI interfaces like Windows or Mac OS X have a long history of tools to share the desktop with multiple users, oddly enough both are called Remote Desktop. Servers that only have a command line interface, like most linux servers, do not have such an obvious tool for sharing the terminal with another user. While bash and ssh can be manipulated to accomplish this, screen fills this roll nicely.

Screen allows you to create virtual terminals that you can detach from and attach to at will. Detaching from a screen will leave it running in the background. Attaching to it again allows you to keep executing commands where you were or see the current state of a process you left running. If you are the user who started the screen you can attach to a screen that is already attached to, this is is how the shared screen is accomplished.

To set it up, one user logs in to the server and, using su or sudo su, changes to either the second users account or an account that all parties can access. Next, start screen with the -S option and give it a meaningful name.

me@jjtest:~# sudo su - theOtherGuy
theOtherGuy@jjtest:~# screen -S blogTest


If screen is not installed, it is available as a package on most popular linux distributions. You are now in a screen session as theOtherGuy. Now just tell theOtherGuy to log in and that your screen is called blogTest and he will simply need to run screen with the -x option and give the screen name.

theOtherGuy@jjtest:~# screen -x blogTest


At this point you are both in the same screen session and can both interact with it. This is great for showing another user what kind of problems you may be having or how to do a certain process. It can also be used to accomplish remote pair programing on the remote system. And this doesn't have to be limited to just two users, as long as someone can get to that system as that user they can join the screen as well.

Typing exit at this point will kill the screen and kick both of you out. If one user wants to leave but not kick the other person off the user just presses Ctrl+a and then d, this will detach you from the screen. Ctrl+a is the default hot key for activating screen commands. You can read more about the variety of command available on screen at this site or read abut it fully on the man page.

Screen can be used to do so much more than I've talked about here but it's ability to facilitate the sharing of a terminal between users can be a life saver when you need it. It is not normally installed by default to may linux systems so you may have to install it or ask that it be installed.

2010-07-22

Heather's Curried Chicken Salad

Some explanation before I get started with this uncommon post. Last month I had the honor of dropping in on a photography workshop for food bloggers, Food and Light, put on by my good friend Jen Yu as well as Diane Cu and Todd Porter from WhiteOnRiceCouple.com and Helen Dujardin from Tartelette. All wonderful people who take beautiful photographs of, among other things, food. So, in honor ( or maybe fallout ) of that I present you with Heather's chicken salad. We love to make a bit of extra grilled chicken in the summer and use the leftovers to make this salad.

Curried-Chicken-Salad-1

After the chicken has cooled, chop it into bite-size cubes.

Curried-Chicken-Salad-5

Cut some grapes in half, K helped us out for this one.

Curried-Chicken-Salad-3

Mix the grapes with chopped green onion and chopped celery.

Curried-Chicken-Salad-2

The dressing is something that Heather found on epicurious.com for their Curried Chicken Salad.

Curry, Ginger, Salt and Pepper

Curried-Chicken-Salad-4

Finally, mix the chicken and dressing in with the grapes and chill. Heather and I enjoy it alone, in a tortilla, or on crackers for dinner on hot summer nights.

Curried-Chicken-Salad-6

Heather's Curried Chicken Salad
Salad
4-5 cups diced chicken
1/4 cup sliced green onion
2-3 cups red grapes sliced in half
1-3 stalks celery diced

Dressing from Epicurious.com
1/2 cup mayonnaise
1/3 cup plain yogurt
5 teaspoons curry powder
1 tablespoon fresh lime juice
1 teaspoon honey
1/2 teaspoon ground ginger
1/2 teaspoon salt
1/4 teaspoon black pepper