Sunday, January 18, 2009

Solaris Tips

As I start working again my OpenSolaris, I'm posting few tips i learned recently

  • Finding text strings in binary files

Ever wondered what's inside some of those binary files on your system (binary executables or binary data)? Several times I've gotten error messages from some command in the Solaris system, but I couldn't tell where the error was coming from because it was buried in some binary executable file.

The Solaris "strings" command lets you look at the ASCII text buried inside of executable files, and can often help you troubleshoot problems. For instance, one time I was seeing error messages like this when a user was trying to log in:

Could not set ULIMIT

I finally traced the problem down to the /bin/login command by running the "strings" command like this:

root> strings /bin/login | more

The strings command lists ASCII character sequences in binary files, and help me determine that the "Could not set ULIMIT" error was coming from this file. Once I determined that the error message I was seeing was coming from this file, solving the problem became a simple matter.

  • A vi macro to display line numbers

While the vi editor has been known to be a little rough around the edges, it still has some pretty nice features. One of those features is the ability to define cool macros. Here we'll show you how to create two macros--one to display line numbers of the file you're editing, and one to hide the line numbers.

First, create a file named .exrc in your home directory (or edit the current file if it already exists). This is the configuration file that vi reads when it is started. Put the following two lines into this file:

:map #1 :set number^M
:map #2 :set nonumber^M

(A very important note: create the ^M characters in this file by typing the key sequence [CTRL-V][CTRL-M]. This key sequence embeds an actual ^M character (the carriage return) into the file.)

Now, save this file and re-start vi. From vi's command-mode, you'll now be able to display line numbers beside your file contents by hitting the [F1] function key, and clear line numbers by hitting the [F2] key. If you like these macros, create your own powerful macros by following this same technique!

  • Use traceroute to find network problems

With the Internet and TCP/IP networks all around us, it's important to know a little bit about TCP/IP network troubleshooting. The Solaris "traceroute" command lets you trace the route your packets are taking to get from your current workstation to a remote workstation you're trying to reach.

For instance, suppose you're on the Internet and you're not getting an HTTP response from a remote server named www.zdtips.com. You try to "ping" the remote site like this:

ping www.zdtips.com

but get no answer. Is the remote server down, or is there a broken link between you and the remote site? Issue the following command to see where the problem lies:

traceroute www.zdtips.com

The traceroute command works its way through the network, and tells you the path it's taking to get to the destination site as it goes along. Watch for the point traceroute fails to learn more about the network segment that has failed. Of course you can also try this on working connections to learn more about how your Internet packets get from one site to another.

  • Using telnet's command mode

When you use the Solaris telnet command to log in from one site to another, don't forget that you can enter telnet's command mode at any time during
your session, usually by entering the following key sequence:

[CTRL] ] (the control key and right-bracket key at the same time)

When this is successful, you'll see the following prompt:

telnet>

Then, from the "telnet>" prompt, enter "?" to learn more about the available telnet commands. A few commands that will give you more help at the prompt are shown below:

display displays many current Telnet settings
send ? displays commands you can "send" to the remote site
set ? displays variables that can be set

One of my favorites (mostly when fooling around) is:

send ayt

which means "send this message: are you there?".

  • Keep commands running after you leave with nohup

It's 3 p.m., and you want to start a long job running. Unfortunately, you can't be sure that the job will finish by 5 p.m. when you need to leave, and the company is very strict about making sure you log off when you leave. However, if you log off the system, the job will be stopped. What can you do?

On Solaris systems you can use the "nohup" (no hang-up) command to keep jobs running long after you log off the system. Using nohup tells the system not to "hang-up" on your job after you've logged off the system.

Here's how to run the job, and keep it running after you log off:

root> nohup my-long-job &

This creates a file named "nohup.out" in the current directory that contains the standard output of the command ("my-long-job") you're running. Everyone is happy because the job keeps running, you get to leave at 5 p.m., and you're properly logged off the system.

  • Use rsh to run commands on somebody else's computer

Here's a hypothetical situation for you: On the "Sesame Street Network", you're logged into a Solaris workstation named "elmo", and you need to look at the man pages for the tar command. Unfortunately the man pages aren't installed on this workstation.

If you're on a network of Solaris computers like this, and you know that the man pages are installed on another workstation named "bigbird", the solution is simple. Simply run the man command on bigbird, like this:

elmo> rsh bigbird "man ksh" | more

This runs the "man ksh" command on bigbird, and pipes the output of the command into the "more" command on elmo.

Here's a few other "rsh" commands you can run on remote workstations:

rsh bigbird "who"
rsh ernie "ps -ef"
rsh grover "ls -al /home"

  • Use CDPATH to traverse file systems faster

If you're like many Solaris users and administrators, you spend a lot of time moving back and forth between directories in similar locations. For instance, you might often work in your home directory (such as "/home/al"), the /usr/local directories, web page directories, or other user's home directories in /home.

If you're often moving back-and-forth between the same directories, and you use the Bourne shell (sh) or Korn shell (ksh) as your login shell, you can use the CDPATH shell variable to save yourself a lot of typing, and quickly move between directories.

Here's a quick demo. First move to the root directory:

cd /

Next, if it's not set already, set your CDPATH shell variable as follows:

CDPATH=/usr/spool

Then, type this cd command:

cd cron

What happens? Type this and see what happened:

pwd

The result should be "/usr/spool/cron".

When you typed "cd cron", the shell looked in your local directory for a sub-directory named "cron". When it didn't find one, it searched the CDPATH variable, and looked for a "cron" sub-directory. When it found a sub-directory named cron in the /usr/spool directory, it moved you there.

You can set your CDPATH variable just like your normal PATH variable:

CDPATH=/home/al:/usr/local:/usr/spool:/home

  • Rearranging columns with awk

Have you ever had a column-oriented text file, similar to a spreadsheet, but the columns weren't in the order you wanted? For instance, suppose you had the following information in a file named "checkbook.orig":

COST DATE BALANCE
10.00 040198 1000.00
20.00 040298 980.00
30.00 040298 950.00

The information is good, but you'd prefer to have the DATE column first, followed by the COST information in the second column, and the BALANCE column third.

Using awk, you can easily rearrange the columns. The following command reads the data from the file named "checkbook.orig", and writes the data to a file named "checkbook.new":

awk '{print $2, $1, $3}' checkbook.orig > checkbook.new

This brief awk command reads each line of the original file, and for each line it reads, it writes an output line to the "new" file. As it writes each record to the new file, it rearranges the order of the columns, so that the columns now appear in the desired order!

If you prefer a little more control of the printed output, awk also has a "printf" function that's very similar to printf in the "C" programming language. Here's the same example, with a tab character in-between each column of the output:

awk '{printf ("%s\t%s\t%s\n", $2, $1, $3) }' checkbook.orig > checkbook.new

The awk command is a powerful programming utility that takes care of things like opening files and reading each line automatically, so all you have to do is tell awk how to process each line as it goes by.

0 comments:

Post a Comment