Archive for the ‘Tips and Tricks’ Category

Changing MAC address

Sunday, October 22nd, 2006

The acronym MAC stands for Media Access Control and it is a unique number that identifies your network interface. In an interconnected computer network a so called ARP table relates your IP address to your network interface card’s MAC.

Despite the fact that physical MAC addresses are permanent, it is possible to change this address. In Linux proceed as follows:

ifconfig eth0 down
ifconfig eth0 hw ether 01:01:02:02:03:03

Prevent non-root users from logging

Friday, August 25th, 2006

Imagine that for some reason (i.e. maintenance tasks) you want to prevent non-root users from logging into the system. The next tip is a very simple way to achieve this goal.

If a file called /etc/nologin exists login will disable the begin of a session in this system. If you put some text into the file, users will be shown this text and their login attempts will be refused.

vi /etc/nologin

Server under maintenance. No access allowed at this moment.

Sed basics

Thursday, August 3rd, 2006

According to sed’s man page…

Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors.

Next, we are going to explore some of the basic usage of sed.

Substitute every occurrence of string1 with string2. The g stands for global, which means that all matching occurrences in the line would be replaced and the output would be directed to outputFile instead of the default standard output.

sed ‘s/string1/string2/g’ inputFile > outputFile

From inputFile show me lines 5732 to 5797. Note the importance of the n switch and the p command to print only the range of lines specified. Without n sed will print the entire file:

sed -n ’5732,5797p’ inputFile

Now I want sed to print everything but lines 5732 to 5797:

sed ’5732,5797d’ inputFile

A kind of cat inputFile1 >> inputFile2:

sed r inputFile1 inputFile2

Show me a paragraph of inputFile that begins with the words “simple mail transport protocol” and ends with the word “postfix”:

sed -n ‘/simple mail transport protocol/,/postfix/p’ inputFile

Next trick will delete empty lines or lines that only contain space characters. Note that the caret denotes the begining of a line, de dollar symbol denotes end of line, the period a single character and asterisk matches zero or more occurrences of the previous character.

sed -e ‘/^ *$/d’ inputFile

Add blank lines to inputFile:

sed ‘G’ inputFile > outputFile

And now delete those blank lines:

sed ‘n;d’ outputFile

A kind of cat inputFile | wc -l:

sed -n ‘$=’ inputFile

Number each line of inputFile:

sed = inputFile | sed ‘N;s/n/t/’

References:

Automatisation of telnet, ftp or ssh

Monday, June 12th, 2006

For ftp you have a quick and dirty trick :

$ ftp -n yourhost <<-here
> quote USER youruser
> quote PASS yourpassword
> ls
> quit
> here

(you can do a put, get, whatever you want instead of “ls”)

For telnet similar idea:

$ (echo -e “open yourhostr”; sleep 2;echo -e “youruserr”; sleep 1; echo -e “yourpassword”; sleep 1; echo -e “unamer”; sleep 1; echo -e “exit”) | telnet

That sound pretty good but … it does not work on solaris (ok on Linux), and no way for ssh :(

So if this shoddy doesn’t meet your expectations…

Expect is what you are looking for. Available for GNU-linux, solaris… MSwindows… at http://expect.nist.gov/
For debian do it cool:

apt-get install expect

I do it with 2 scripts. I’m sure that you can do better, but that’s work ;-)

– script to call the expect and to make things good looking:
host=$1
psswd=$2
cmd=$3
os=$( ./expectaTion.tcl $host $psswd $cmd| awk “/$cmd/ “‘{getline; print}’|tr -d ‘

Linux: skip or force fsck on reboot

Tuesday, April 11th, 2006

shutdown is the typical way to bring your system down in a Unix environment. It takes care of notifying logged in users that the system is going down, takes care of sending SIGTERM to processes and more.

One interesting feature of shutdown is the ability to force or skip the check and repair of the filesystem after a reboot. This is done by two flags that could be passed to the command. According to the man pages:

The -f flag means 'reboot fast'. This only creates an advisory file /fastboot which can be tested by the system when it comes up again. The boot rc file can test if this file is present, and decide not to run fsck(1) since the system has been shut down in the proper way. After that, the boot process should remove /fastboot.

The -F flag means 'force fsck'. This only creates an advisory file /forcefsck which can be tested by the system when it comes up again. The boot rc file can test if this file is present, and decide to run fsck(1) with a special 'force' flag so that even properly unmounted file systems get checked. After that, the boot process should remove /forcefsck.

Insufficient memory to execute commands

Sunday, November 27th, 2005

If you have played with Linux computers for a while, it is possible that in some ocassions one of them run out of memory for some reason. In this situation it is impossible to run simple commands such as ls. Recently I’ve read a little trick from Prentice Bisbal at Linux Journal that may help.

From Prentice…

About five years ago, a Linux system I was responsible for ran out of memory. Even simple commands, such as ls, failed with an insufficient memory error. The obvious solution to this problem was simply to reboot. One of the other system administrators wanted to look at a file that may have held clues to the problem, but he couldn’t remember the exact name of the file. We could switch to different directories, because the cd command is part of bash, but we couldn’t get a list of the files, because even ls would fail. To get around this problem, the other system administrator created a simple loop to show us the files in the directory:

$ for file in *; do echo $file; done

This worked when ls wouldn’t, because echo is a part of the bash shell, so it already
is loaded into memory. It’s an interesting solution to an unusual problem.

Cool! Isn’t it? Thanks to Prentice Bisbal.

How to delete a file whose name begins with a “-” character?

Saturday, October 8th, 2005

The easiest way to do the job is something as follows:

rm ./-file_to_delete

Another approach that is useful for commands that use getopt to parse the arguments is shown next:

rm — -file_to_delete

This tells the command rm (or whatever command that uses getopt) that anything after “–” is not an option.

Multivolume devices

Tuesday, September 20th, 2005

If you have a big file to back-up to a remote server to which there is a multivolume tape attached, you can tar it even if your device do not change automaticaly the tape:

tar clPM –new-volume-script /root/change_tape \
–tape-length=12582912 –preserve –atime-preserve \
–rsh-command=/usr/bin/ssh -f root@backup:/dev/rmt/2n mnt/backup1

Where…

  • -c is for create
  • -l is for local
  • -P is for preserving the absolute name (or tar automaticaly strip the leading / converting all paths into relative path names)
  • -M is for multivolume
  • –preserve preserves the order and the permitions
  • –atime-preserve preserves the access times
  • –rsh-command=/usr/bin/ssh to avoid using rsh
  • -f for the file to which the back-up goes in. Here we give the user (root) at the server (backup) and the path to the device (the user must have in this machine the right to write to the device and in order to avoid futher complexity to log in the machine without giving a password)
  • –tape-length=12582912 is the length of the tape in kbytes (here is a 12G tape. For testing purposes you can give this option very short numbers to try out the script)
  • –new-volume-script you can give a relative or an absolut path, you can separate with a space or a “=” as you wish

You can use the next script for this purpose (with no guaranty at all, of course). You have to notice that this script is made for a 6 tapes multivolume device. When the 6th tape is full, you have to place new tapes in the device; no script usefull for this ;)

#!/bin/bash
export tape=`ssh root@backup “mtx -f /dev/rmt/2 status”| awk ‘/Empty/ {a= a + 1; if (a == 1) print $3}’| cut -c1`
case $tape in
6)
echo “change the tapes and tip o”
read a
ssh root@backup “mtx -f /dev/rmt/2 load 1″
echo “load 1″
ssh root@backup “mt -f /dev/rmt/2 rewind”
echo “rewind 1″
export tape=` ssh root@backup “mtx -f /dev/rmt/2 status”| awk ‘/Empty/{ a= a +1; if (a == 1) print $3}’| cut -c1`
case $tape in
1)
exit 0;;
*)
exit 1;;
esac
;;

1 | 2 | 3 | 4 | 5)
ssh root@backup “mtx -f /dev/rmt/2 unload”
echo “mtx -f /dev/rmt/2 unload”
ssh root@backup “mtx -f /dev/rmt/2 load `expr $tape + 1`”
echo “load” `expr $tape + 1`
ssh root@backup “mt -f /dev/rmt/2 rewind”
echo “rewind ” `expr $tape + 1`
export tape1=` ssh root@backup “mtx -f /dev/rmt/2 status”| awk ‘/Empty/{ a= a +1; if (a == 1) print $3}’| cut -c1`
echo “tape1 = $tape1″
echo expr $tape1 – $tape – 1
exit `expr $tape1 – $tape – 1`
;;

*)
echo “no tape! Please feed me!”;
exit 1
;;
esac

Determine what program dumped core

Wednesday, September 14th, 2005

When a process causes a memory violation or issues an illegal instruction or something like that it will probably terminate abnormally and will generate a file named core. This file is a memory image of the process.

The GNU debugger, gdb allows you to determine the program that caused the core dump:

gdb -core core.16124

…and you’ll get something like this:

Using host libthread_db library “/lib/tls/libthread_db.so.1″.
(no debugging symbols found)
Core was generated by `kded’.
Program terminated with signal 6, Aborted.

Reverse cat

Wednesday, September 14th, 2005

How to concatenate and/or print files in reverse order? tac is your friend.

From the man page:

Write each FILE to standard output, last line first. With no FILE, or when FILE is -, read standard input.

The easiest way to use it…

tac filename.txt