Archive for the ‘ scripts ’ Category

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.

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.