Sunday, May 28, 2006

top 10 tips dealing with vi editor

as a unix/linux system admin, my main text editor is vi/vim, since it is available in most of the unixes. still remember when i first started using it, not really an easy job man! but now i'm quite comfortable with it and would like to share my top 10 tips when dealing with this editor, in no particular order:

1. undo & redo
don't be surprise that vi also have undo & redo capabilities. but for vi is only one level undo. not really sure how many level for vim, for sure more than vi. just press u to find out.
Esc + u or :u to undo
Ctrl + r to redo

2. search pattern
a few ways to search a pattern in vi:
/pattern search down for pattern
?pattern search up for pattern
pressing n or N to go to the next or previous matching pattern

press * or # search for a pattern under the cursor
pressing * or # to go to the next or previous matching pattern

3. search & replace
search & replace is quite important in my job where sometime i need to do a bulk replacement
:s/pattern/new_patten/ replace a pattern with new pattern
:1,$ s/pattern/new_patten/g g for replace all occurrences of pattern
:1,$ s/pattern/new_patten/c c for confirm replace

1 start from line 1
$ end at last line
Read more...

4. go to specific line
do you hate when you run a scripts, an error occurred in specific line!
this is how you go directly to the line to find out what went wrong
:n will go to line n (eg: :69 will bring your cursor to line 69)
:se nu or :set number will display line number
:.= shows current line number
:= shows number of line in file
Ctrl + g shows file name, current line number, total lines in file & % of the file location

5. vi few files
i'm sure sometime you need to edit several files at once. especially those log files.
while you are in vi, you can open up other file using:
:split other_file to open other file & will split into 2 screens
Ctrl + w + w to switch between the files

while in command prompt, simply:
$ vi file1 file2 file3
:n edit next file (file2)
:n edit next file (file3)
:rew rewind to the first file (file1)

6. shell access or run a command
while editing a file in vi, you can also access shell to run specific commands without quiting from vi
:shell will gives you a command prompt, type exit when you done with the shell, will return back to vi
or simply use :!command to execute the command

7. command output into vi
don't waste time to copy & paste the command output, instead redirect it into the file
:r!cat /etc/passwd will inserts content of /etc/passwd into the current file

8. recovering your file
life is not perfect, sometime something goes wrong while you are editing your file, eg: power failure or lost connection. indeed you can recover it back by using:
$ vi -r your_filename

9. word completion
for me this feature is quite useful for developers since they most likely repeating the same words/patterns:
type a few characters of the word and then keep on pressing Ctrl + p or Ctrl + n until you find the match, but make sure the words/patterns are already there in the file.

10. get help, don't panic!
if you get lost or no idea how to do things in vi, don't panic! get help
use :h or :help to access the main help file
to go directly to specific help, give an argument to the :help command
or to search for help use :help any_word eg: :help me will bring you to the line which contains word me, pressing Ctrl + d to see matching help entries for me.
:help!
E478: Don't panic!

and of course there are a lot more vi tips out there!
happy vi-ing :)

note: certain tips are not working with vi, only for vim!

Thursday, May 25, 2006

equivalent ethtool for solaris

if you are looking for equivalent ethtool for solaris, the answer is ndd
ndd - get and set driver configuration parameters

to get your network interface card (eg: hme0) info:
# ndd -get /dev/hme link_status
# ndd -get /dev/hme link_speed
# ndd -get /dev/hme link_mode

link_status
0 for Link Down
1 for Link up

link_speed
0 for 10 Mbps
1 for 100 Mbps

link_mode
0 for Half-Duplex mode
1 for Full-Duplex mode

and of course for more info:
# man ndd

mii-tool vs ethtool

both commands are actually doing the same things. an useful tool to display or change ethernet cards settings, but bare in mind that mii-tool only for 10/100M NIC while ethtool can support all hardware. so in case you are using GB cards, you should use ethtool then.
# ethtool eth0
Settings for eth0:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Advertised auto-negotiation: Yes
Speed: 100Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 0
Transceiver: internal
Auto-negotiation: on
Supports Wake-on: umbg
Wake-on: g
Current message level: 0x00000007 (7)
Link detected: yes

for more info
# man ethtool

change hostname - solaris vs linux

in order to change your machine's hostname, you will need to edit some files.

for solaris:
there are 6 files you need to edit:
/etc/hosts
/etc/nodename
/etc/hostname.network_interface (e.g hostname.hme0)
/etc/net/*/hosts (3 hosts files)

but for linux:
only 1 file
/etc/sysconfig/network

for temporary hostname (i'm not sure why you need it temporary), you can use command:
hostname new_hostname

but if you want it permanent, you need to reboot the system after changing all the necessary files.

Saturday, May 20, 2006

one-liner to check disk/file systems usage

as requested by my friend, here is the one-liner to check disk/file systems usage that exceed certain percentage:
$ df -k | sed -e 's/%//g' | awk '{if ($5 > 70) print $1 " " $5 " " $6}'
Filesystem capacity Mounted
/dev/md/dsk/d0 82 /
/dev/md/dsk/d8 78 /home

change 70 to any number that suite your need.

RX packets errors

still figuring out on why this box having RX packets errors:
# netstat -i
Kernel Interface table
Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1500 0 6562755167 851549 849523 849523 3191554302 0 0 0 BMRU
lo 16436 0 43728443 0 0 0 43728443 0 0 0 LRU

# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:11:85:XE:B2:XE
inet addr:XXX.X.XXX.XX Bcast:XXX.X.XXX.XXX Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:6562755167 errors:851549 dropped:849523 overruns:849523 frame:1189
TX packets:3191004809 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3964758124916 (3781087.9 Mb) TX bytes:336413334315 (320828.7 Mb)
Base address:0x3000 Memory:d6000000-d6020000

or you can see from /proc/net/dev

since this is production server, can't simply reboot to test it out. it could be hardware issue here, maybe switch, cable or the NIC itself or maybe tcp/udp packets. any clue :(

rsync error / csh bug?

one of our users complaining that he having a problem with his rsync script. the error was:
protocol version mismatch - is your shell clean?
(see the rsync man page for an explanation)
rsync error: protocol incompatibility (code 2) at compat.c(62)

there is a good way on how to debug this error:

is your shell clean

The "is your shell clean" message and the "protocol mismatch" message are usually caused by having some sort of program in your .cshrc, .profile, .bashrc or equivalent file that writes a message every time you connect using a remote-shell program (such as ssh or rsh). Data written in this way corrupts the rsync data stream. rsync detects this at startup and produces those error messages. However, if you are using rsync-daemon syntax (host::path or rsync://) without using a remote-shell program (no --rsh or -e option), there is not remote-shell program involved, and the problem is probably caused by an error on the daemon side (so check the daemon logs).

A good way to test if your remote-shell connection is clean is to try something like this (use ssh or rsh, as appropriate):

ssh remotesystem /bin/true > test.dat

That should create a file called test.dat with nothing in it. If test.dat is not of zero length then your shell is not clean. Look at the contents of test.dat to see what was sent. Look at all the startup files on remotesystem to try and find the problem.

refer to http://samba.anu.edu.au/rsync/FAQ.html

and in my case, it is because of the csh error or is it bug? somehow once csh executed this is the error message.
free(2a95e9b7c0) above top of memory. (memtop = 5c2800 membot = 58d000)

no idea what it mean (maybe related to memory problem... i dunno) but for temporary solution i changed the permission of /etc/profile.d/z-lti.csh to 600, so the csh will not sourcing this file when the user uses csh. the error gone & he able to rsync :)

Friday, May 05, 2006

free cool gadgets from thegadgetblog.com


thegadgetblog.com is giving away cool gadgets (Apple ipod, Scope watch, Wireless PC Lock, etcetera) for its 1st birthday. hopefully i win one of them :)
here is the contest

happy birthday thegadgetblog!