Visit Heroix at http://www.heroix.com
Subscribe to the Heroix eNewsletter
Visit Heroix at http://www.heroix.com
Charting Life in the IT Environment

>> Use the Command Line Interface and vi

by Dave Atkins on March 31, 2009

It might seem a bit ironic for a Mac user to prefer command line tools like the vi editor and working in the bash shell. But as I’ve described previously, the command line approach is easier to document and automate, particularly by copying and pasting commands into a wiki for later reference. And while many IT managers and directors are busy working at the enterprise level, it is good to be able to jump in to handle emergencies. The tips below (Linux) are not recommended as standard operating procedure…but sometimes you need a clever alternative.

  • The iPhone app, TouchTerm, allows ssh connections via a PuTTY-like client. I would not recommend administering servers over the touch screen of an iPhone, but sometimes, it might be the only option you have–other than dictating instructions verbally.
  • What if there is no video? You may have to type blind at the console. The KVM over IP software may have crashed and you need some way to access the servers. You could use PuTTY to create a tunnel for VNC…but maybe all you can do is get in to the command line. Then what?
  • One trick I’ve discovered recently is using the WinSCP client to establish an ssh connection, then open files in the native WinSCP text editor. This approach works most of the time, but sometimes, the file can be corrupted unexpectedly, leaving a zero-byte file behind on the server. Not exactly what you were hoping for when you opened httpd.conf on a whim…so copy the files somewhere safe first!

When bad luck seems to be the rule of the day, you may find the need to edit files directly on the server. Remember emacs? Not installed. But there’s always vi (for linux at least)….

vi has a storied history based in the long forgotten days when people dialed in to servers over 300 baud modems. The v stands for “visual” because, believe it or not, vi was intended to be more visual than the alternatives of the time. More lore can be found on the vi lovers home page. It worth a few hours of playing around with vi if you are not already familiar because sometimes it may be your only weapon as you are forced to deal with an emergency.

I’m no expert by any means, but the bare minimum tips needed for survival are:

Inspection

  • Navigation is based on the “home keys” of the keyboard to make it possible to move around with minimal effort. j and k are down/up and the keys to the left and right (h=left, l=right) move the cursor to the left and right.
  • Typing “:1″ takes you to line 1. “:$” is last line. Control-f is page forward, Control-b is page back.
  • to search for a pattern, type “/pattern.” Things can get interesting here as you use regular expressions…or not.

Surgery

  • To make changes to the file, you leave the visual mode and enter insert mode by typing “i”. Hitting ESC takes you back out of insert mode. To start inserting at the end of the current line, type “shift-A” to Append.
  • To delete characters, start from the visual mode; hit ESC to stop inserting…then you will type “d” followed by a key that indicates how much to delete. Keep it safe and just type “d [spacebar]” for one character at a time. Feeling lucky? “dw” means delete word. Whole line? “dd” More? By typing “:” you specify a command will follow which may be preceded by a range; e.g. “:1,10d” would delete lines 1-10. “:1,$d”? Why would you want to do that?

Oops

  • To get out of the file you have now mangled, without saving changes, type “:q!”

Save

  • To save changes, “:w” writes the file. Then, just “:q” out to quit. (or, type “shift-Z shift-Z” to save changes and exit.)

Advanced Usage

Certain situations are just easier to fix with vi, but require more sophisticated command sequences. Look these up to understand them, but once you use them, they will become familiar friends.

  • When a “DOS” file get’s FTP’d over in binary mode because the user didn’t have a modern client tool or forgot to specify ascii mode, use the following keystroke sequence to globally remove all the control-Ms (Windows carriage return): “:%s/control-v control-m//g”
  • to globally update an IP address, you will perform the global replace but escape the dots with a backslash: “:%s/192.168.1.1/10.10.1.1/g”

Now these can seem rather trivial examples. But what I’ve seen in practice is a crisis situation where you, the sysadmin of the moment, have to search through files and find the problem configuration item and fix it. Or perhaps you have set up configuration files with a failsafe configuration commented out, like this:

# note: if things break, just comment out the next 10 lines
# begin new experimental configuration stuff that became production code

It’s nice to be able to simply take note of the current line number in the bottom right corner of the screen and then issue a command like, “:420,429s/^/# /” to put hash marks in front of each line. Then go through and cut out the hash marks for what should be uncommented. If you are lucky, you can type something like “:430,439s/^# //” As you start to think in cryptic vi patterns, you will want to consult a real vi cheatsheet and refresh your knowledge of regular expressions.

Stumbling around on your servers, learning vi in production is not what you want to be doing, but there is a difference between the unfamiliar manager hacking the server vs the experienced technologist who knows what needs to be done, but just doesn’t have the familiar tools at hand. I’ve seen situations where a site was down or some revenue-generating activity was dead because of a simple syntax error…and the power to fix it quickly and surgically “paid for itself.” But there is always a judgement call to be made–don’t make things worse! The more tools and options a problem-solver has, the more likely the problem can be addressed before it spirals out of control. It can also be helpful to simply narrow down the problem and conclude you need help from a particular expert to proceed.

Share this post:
  • E-mail this story to a friend!
  • StumbleUpon
  • Digg
  • del.icio.us
  • Facebook
  • LinkedIn
  • Google
  • Furl

[Post to Twitter] 

No Comments »

No comments yet.

RSS feed for comments on this post. RSS must be enabled on your computer.

TrackBack URI

Leave a comment

© 2010 Heroix | Heroix | RSS | Privacy Policy | Email: info@heroix.com