Archive for the ‘Tips and Tricks’ Category

ISO image as a filesystem

Saturday, January 3rd, 2009 by karkoma

If you want to mount an ISO image as a filesystem, simply proceed as follows:

mkdir /mnt/iso0
mount -o loop -t iso9660 /tmp/debian-40r6-amd64-netinst.iso  /mnt/iso0

System stats with saidar

Sunday, April 6th, 2008 by karkoma

saidar is a top like tool. It provides a curses based interface for viewing system statistics such as network I/O, disk I/O, CPU, memory and more. The core of the functionality is provided by the libstatgrab library:

libstatgrab is a library that provides cross platform access to statistics about the system on which it's run. It's written in C and presents a selection of useful interfaces which can be used to access key system statistics. The current list of statistics includes CPU usage, memory utilisation, disk usage, process counts, network traffic, disk I/O, and more.

On a Debian system install the package as usual:

apt-get install saidar

and then run:

saidar

Saidar in action

Serial terminal support in GRUB

Saturday, February 9th, 2008 by karkoma

In some situations it is very useful to have the possibility to access the GRUB boot menu from a serial line. For example you could remotely reboot your machine with the new kernel you just compiled.

In my case I have a couple of remote machines linked together by a serial cable. I can log into machine A via ssh and get the Grub menu of machine B through minicom. Thus I can boot whatever OS/kernel I want as if I were in front of the machine. Kind of KVM IP for the rest of us.

Simply add this to Grub’s menu.lst:

serial –unit=0 –speed=9600 –word=8 –parity=no –stop=1
terminal serial

MySQL: quick replacement

Thursday, December 6th, 2007 by karkoma

Say that you have a table called myTable with several rows and a column called URL that contains the IP address of a log server. Now imagine that you want to change the value of URL so IP address is substituted by the server name. One way to do that is as follows:

use dbname

update myTbl set URL=replace(URL,’172.16.1.10′,’logserver’);

OpenSSH: simplifying logins

Thursday, December 6th, 2007 by karkoma

OpenSSH provides a per-user configuration file usually located in ~/.ssh/config. This file can help ease your life as Sysadmin. Let’s say that you frequently connect to a server in this way:

ssh admin@boring-servername.boring-domain.com

With OpenSSH there is an easy way to do the job. Edit or create a config file:

vi ~/.ssh/config

and then add the following:

Host servername
     User admin
     HostName boring-servername.boring-domain.com

Where Host is the alias for the remote server you want to connect to, HostName is the full name of the remote server and User is the login name.

References:

Perl on the command line

Sunday, May 27th, 2007 by pascal

Perl, you know perl, is not just a jewel for scripting, it ’s also a potent onliner.

The magic trick is the diamond:

perl -e ‘while (){print}’ file1

says perl “keep on as long as you read something in the standard in”, in the case of the example above, “print the line” … and each line goes in the memory but not the whole file: you have a powerful stream editor.

The ” -e” switch says to perl that what follows in the command line between the single quotations is a script and not a script file name.

But you can have it quicklier :

perl -ane ‘print’ file1

The switches “-ane” do the “while stuff” for you. It says “do a while () loop” (-n) “and split each record in the @F array” (-a), I haven’t used the array yet.
Perl has a difficulty : you can implicit everything. Some guys abuse of it in scripting, like spirits, but it’s good for an onliner.

This line will print file1. Great (in fact implicit in “print $_” ; $_ is the variable where perl puts what you do not explicit). Printing is great but let do some awkish things :

perl -ane ‘/$m[aiy]/ && print $_’

That will print the lines that contain $ma, $mi or $my, (have a look at the article from karkoma about sed), patterns in perl are like sed’s ones but larger. Take a look at the tutorial of perl for pattern or type “perldoc perlreftut” if you have installed perldoc.

Well, just let do some sedish thinks :

perl -ane ’s/

]*)>//; s/
/
/;print’ file1.html

replaces the paragraph in html( by newline () but keeps the options of the paragraph ($1, first match, here the parenthesis are not optional) in an html comment.

perl -F”:” -ane ‘/pascal/ && print $F[2]‘ /etc/passwd

will print the UID of user pascal (third field), the field separator is “:” (-F$-1òý:òý)

perl -ane ‘/$m[aiy][^ s]*/ && print “line : $. variable : $&n”‘

will print the number of line ($.) where the variables that begin with “$ma”, “$mi” o “$my” and the variables that have matched the description ($&)

bad news, you received this bad MS file with carriage return (r) and newline and you want only the newline :

perl -pi.old -ane ’s/rn$/n/;print;’ file1

and I obtain directly a file1 without carriage return and I have a backup of the original in file1.old (-pi.old)
you can have a look to a quick look at some useful perl onliners.

Changing MAC address

Sunday, October 22nd, 2006 by karkoma

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 by karkoma

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 by karkoma

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 by karkoma

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 ‘