Archive for the ‘Tips’ Category

Cool linux tips

Sunday, February 22nd, 2009

Useful and cool Linux tips collection:

1. Debian add ip address:

auto eth0:0
iface eth0:0 inet static

    address 192.168.1.41
    netmask 255.255.255.0
    broadcast 192.168.1.255

2. Find modified lately file:

find . -mtime -1 -print

3. Find information about memory slots, speed, type etc’ (requires package lshw):

lshw | grep *-memory -A 40

4. Find DDOS! Find and sort connections by source ip:

netstat -ntu | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n

5. Remove annoying system bell:

echo ‘set bell-style none’ >> ~/.inputrc

6. Trying to rm and getting ‘list too long’ ? try this:

 find . | xargs rm

7. Extract only numbers from a string:

xxxxx/545454/xxxxx —> 545454
cat list.txt | sed  ‘s/.*\/\([0-9]*\)*\/.*/\1/g’

8. Use colors in vim:

cat > ~/.vimrc

syntax on

^D

To be updated!

nginx debian init script

Friday, December 26th, 2008
#! /bin/sh

# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'sudo update-rc.d nginx defaults', or use the appropriate command on your
# distro.
#
# Author:	Ryan Norbauer <ryan.norbauer@gmail.com>
# Modified:     Geoffrey Grosenbach http://topfunky.com

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/nginx.conf
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

d_start() {
  $DAEMON -c $CONFIGFILE || echo -n " already running"
}

d_stop() {
  kill -QUIT `cat $PIDFILE` || echo -n " not running"
}

d_reload() {
  kill -HUP `cat $PIDFILE` || echo -n " can't reload"
}

case "$1" in
  start)
  	echo -n "Starting $DESC: $NAME"
  	d_start
  	echo "."
	;;
  stop)
  	echo -n "Stopping $DESC: $NAME"
  	d_stop
  	echo "."
	;;
  reload)
  	echo -n "Reloading $DESC configuration..."
  	d_reload
  	echo "reloaded."
  ;;
  restart)
  	echo -n "Restarting $DESC: $NAME"
  	d_stop
  	# One second might not be time enough for a daemon to stop,
  	# if this happens, d_start will fail (and dpkg will break if
  	# the package is being upgraded). Change the timeout if needed
  	# be, or change d_stop to have start-stop-daemon use --retry.
  	# Notice that using --retry slows down the shutdown process somewhat.
  	sleep 1
  	d_start
  	echo "."
	;;
  *)
	  echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
	  exit 3
	;;
esac

exit 0

Easy create packages via checkinstall

Monday, November 17th, 2008

Sometimes an application you want to install is not exists in the package manager or the package is too old so you have to compile it by yourself. You know the mess it makes, you need to remmember what you compiled and not overwrite the installation when you decide to install from a package.

It’s good to create a package from your compiled binaries, you can also reuse the package in other machines.

Here come ‘checkinstall’, you can use checkinstall to generate Debian packages (Slackware too),

Get it from:http://www.asic-linux.com.mx/~izto/checkinstall/download.php (Debian i386 package).

Lenny has the package in APT, but Etch doesn’t.

Ubuntu also got it in APT.

After you finish compiling, just run ‘checkinstall’ command from the source directory.

regular expression (e?grep) log search

Wednesday, February 27th, 2008

Regular expression is a very big word and world, they are very popupal in the last years and used at the most programming languages (perl, php), unix tools (grep, egrep, awk, sed), unix text editors (ed, vi, vim), servers (Apache, mysql, nginx).

I will demonstrate a method of searching log files using the grep/egrep tool. Most of us are familiar with the ‘grep’ tool we have in Linux/Unix systems, (Debian system has the GNU grep/egrep tool version). We apply grep tool usually when we want to find a word or a sentense in a text file; as you know grep scan the file line by line and so is the match applied, for instance if we want to find accesses to the web server on the 24 Feb 2008:

grep 24/Feb/2008 /var/log/httpd/access_log yields:

84.95.86.128 – - [24/Feb/2008:14:17:42 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
87.68.37.48 – - [24/Feb/2008:14:50:32 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
87.68.37.48 – - [24/Feb/2008:15:28:51 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:16:41:20 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:17:15:06 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:17:24:35 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:17:41:05 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:18:03:52 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:18:24:45 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:19:08:57 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:19:58:26 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:20:00:48 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:20:37:34 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:20:53:12 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:21:21:16 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:21:24:54 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:21:46:11 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:22:11:31 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:22:20:48 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:22:29:12 +0200] "GET / HTTP/1.0" 200 198 "-" "-"

If want to to match accesses on 24 Feb 2008 but only at 17 o’clock:

grep 24/Feb/2008:17 /var/log/httpd/access_log yields:

84.95.106.251 – - [24/Feb/2008:17:15:06 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:17:24:35 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:17:41:05 +0200] "GET / HTTP/1.0" 200 198 "-" "-"

But what if we want to find matches from 17 and 18 o’clock ? We can use regex class:

grep 24/Feb/2008:1[78] /var/log/httpd/access_log

This will search matches for both 17 and 18 because we used a class of possibilities.

What about matches between 17-20?

grep 24/Feb/2008:[12][07-9] /var/log/httpd/access_log

You may notice that this grep regex will also match hours as 10, 27, 28, 29. Which will cause 10 o’clock also to be matched sometimes it may be critic to match exactly what we need, therefore you need to extend the regex by using alternations:

grep "24/Feb/2008:\(1[7-9]\|20\)" /var/log/httpd/access_log

In the grep tool you have to escape the special characters with a backslash or just switching to egrep or ‘grep -E’, also the double quotes are added to escape shell special characters and to treat them as regex.

egrep "24/Feb/2008:(1[7-9]|20)" /var/log/httpd/access_log

84.95.106.251 – - [24/Feb/2008:17:15:06 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:17:24:35 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:17:41:05 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:18:03:52 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:18:24:45 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:19:08:57 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:19:58:26 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:20:00:48 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:20:37:34 +0200] "GET / HTTP/1.0" 200 198 "-" "-"
84.95.106.251 – - [24/Feb/2008:20:53:12 +0200] "GET / HTTP/1.0" 200 198 "-" "-"

It is also good to add the –color parametes for colorized match output.

egrep "24/Feb/2008:(1[7-9]|20)" /var/log/httpd/access_log –color

The program’s name derives from the command used to perform a similar operation, using the Unix text editor ‘ed’: g/re/p This command searches a file globally for lines matching a given regular expression, and prints them.

fsck on boot

Thursday, February 21st, 2008

Force fsck check on boot:

cd /

touch forcefsck

It will force fsck check on next boot, the system will automatically remove the file when the boot process finish.

 

Disable auto fsck check on boot (after 30 mounts):

edit /etc/fstab

# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    defaults        0       0
/dev/sda7       /               ext3    defaults,errors=remount-ro 0       1
/dev/sda10              /home                   ext3    defaults,usrquota,grpquota      0       2
/dev/sda9       /tmp            ext3    defaults        0       2
/dev/sda6       /usr            ext3    defaults        0       2
/dev/sda8       /var            ext3    defaults        0       2
/dev/sda5       none            swap    sw              0       0
/dev/hda        /media/cdrom0   iso9660 ro,user,noauto  0       0

Change the ending 2 to 0 on all disks that you want to remove from the auto check.

# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    defaults        0       0
/dev/sda7       /               ext3    defaults,errors=remount-ro 0       1
/dev/sda10              /home                   ext3    defaults,usrquota,grpquota      0       0
/dev/sda9       /tmp            ext3    defaults        0       0
/dev/sda6       /usr            ext3    defaults        0       0
/dev/sda8       /var            ext3    defaults        0       0
/dev/sda5       none            swap    sw              0       0
/dev/hda        /media/cdrom0   iso9660 ro,user,noauto  0       0

HOWTO: SMS Gateway with Nokia 6230i GSM

Thursday, December 27th, 2007

This howto will guide you to build a SMS gateway with Nokia 6230i on Linux machine (Tested on Debian etch).

Hardware  requirements:

DKU2 cable (Nokia cable which connects the phone to machine’s USB port)

GSM Nokia 6230i

Linux box with USB interface (Debian prefered :)

There are many tools for sms under linux, I tested the Gnokii (Nokia unique tool) and sms-tools v3. Found that sms-tools much more professional and faster.

Connect your phone to the AC power (the DKU2 cable doesn’t charge the phone) and to the machine using the cable.

When you type ‘lsusb’ you should see:

chiq:~/smstools3/scripts# lsusb
Bus 003 Device 015: ID 148f:2573 Ralink Technology, Corp.
Bus 003 Device 001: ID 0000:0000 
Bus 002 Device 002: ID 045e:009d Microsoft Corp.
Bus 002 Device 001: ID 0000:0000 
Bus 001 Device 017: ID 0421:0428 Nokia Mobile Phones
Bus 001 Device 001: ID 0000:0000 

Now the DKU2 cable connects the serial port of the phone to the USB interface, you can access the phone using the serial protocol or the usb protocol. To access using the usb, you need to load gnokii nokia usb module which works slower than the serial way. When using serial connection the GSM AT commands sent directly to the phone and it works better and faster.

You should see /dev/ttyACM0 device, its your emulated serial device.

 

If you don’t check ‘lsmod’ if you have this modules loaded:

usbcore               112644  7 usbserial,cdc_acm,rt73,usbhid,ehci_hcd,ohci_hcd

 

 If you do, you can use the SMS-TOOLs v2/3 to operate the phone. (On the sms-tools site the Nokia 6230/i is not in the supported phones list but when you use it in serial connection it doesn’t metter at all which phone type you have because it sends plain AT commands to the GSM modem).

Download the sms-tools v3 from http://smstools3.kekekasvi.com/ at http://smstools3.kekekasvi.com/packages/smstools3-3.0.10.tar.gz

tar xvfz smstools3-3.0.10.tar.gz

cd smstools3

make

make install

it will create simple smsd.conf in /etc/smsd.conf

edit the device parameter, put /dev/ttyACM0 there, should look like this:

# Example smsd.conf. Read the manual for a description

devices = GSM1
logfile = /var/log/smsd.log
loglevel = 7

[GSM1]
device = /dev/ttyACM0
incoming = yes
#pin = 1111

 

Now start the smsd daemon /etc/init.d/sms3 start

check the log file: tail -f /var/log/smsd.log

should look like that:

2007-12-27 21:04:47,7, GSM1: <- 425010502703950
OK
2007-12-27 21:04:47,6, GSM1: IMSI: 425010502703950
2007-12-27 21:04:47,6, GSM1: Checking memory size
2007-12-27 21:04:47,7, GSM1: -> AT+CPMS?
2007-12-27 21:04:47,7, GSM1: Command is sent, waiting for the answer
2007-12-27 21:04:47,7, GSM1: <- +CPMS: "SM",0,20,"SM",0,20,"MT",150,170
OK
2007-12-27 21:04:47,6, GSM1: Used memory is 0 of 20
2007-12-27 21:04:47,6, GSM1: No SMS received

 

The sms-tools use spool files, all incoming sms messages are stored at:

/var/spool/sms/incoming/

To send a message you can use the tool that comes with the sms-tools.

cd smstools3 (the untared directory of the source)

cd scripts

the tool called sendsms.

What the script does is creating a temp file in the outgoing folder of the spool which looks like this:

To: 491721234567

Hello, this is the sms.

 

You can create flash SMS messages too:

From: Stefan
To: 491721234567
Flash: yes
Alphabet: ISO

Hello Stefan, how are you?

 

More information about SMS file format here:

http://smstools3.kekekasvi.com/index.php?p=fileformat

Howto: Debian route-map (part 1)

Saturday, April 14th, 2007

Route by source Interface IP rule

In this howtos series we will show  how do we implement policy based routing under Debian (or any other Linux), the howtos divided into different parts, each part will provide a howto of a specific feature.

The old ip tools replaced by the iproute2 tool, many many features became availble such as interface management, PBR, source-route, route tables aggregation, QOS and more.

Under Debian the installation is very easy, there is a package called iproute which you can install easily:

apt-get install iproute

Don’t worry, it won’t replace your current ip/route tools but just work simultaneously to the current route/interface/ip tables.

Now you can use the iproute2 ip tools, to show your current route table just type: ip route list

The default route table is numbered as 254, in iproute2 we can create many route tables and assign them accordingly, to see all route tables type:

ip route list table all

If we want to route a connection that came to our specific IP address back to predefined specific gateway, we need to use ip rules and seperate route table that triggered by an ip rule, this is how we get it done:

ip route add default via GATEWAY table 113

ip rule add from LOCAL_IP table 113

GATEWAY – New gateway address to which we want to route classified traffic.

LOCAL_IP - An inbound connection to this IP address will be routed back via the GATEWAY.

That’s all, the table number is just a tag.

Examples of useful usage:

  • A rule that says if there is a connection to our real IP, route it back via our real gateway otherwise all will be default routed via the VPN gateway. In that way we still can connect to our real IP and manage the server.
  • Split Tunnel VPN connection: create rule that will route all far side networks via the vpn gateway, all other traffic will be routed via the default gateway.

Howto: Debian bandwidth monitor via CLI

Thursday, April 5th, 2007

Very useful tool for Linux systems which allows one monitor the interface statistics, TCP port connections & bandwidth usage – ‘iptraf’:

(apt-cache search iptraf)

iptraf – Interactive Colorful IP LAN Monitor

Debian has a package of iptraf, the installation is easy:

apt-get install iptraf

I will demonstrate you how to monitor your outgoing bandwidth on your machine,

To run iptraf type the ./iptraf command in the shell,

 

 Select "Detailed interface statistics" from the menu:

Choose your interface to monitor (In our example eth0):

 Wait about 5secs (iptraf needs it for synchronization):

The current outgoing bandwidth usage is circled in red.

Debian SOCKS4 server install

Sunday, April 1st, 2007

To install a SOCKS4 server on Debian you may apt-get the SOCKS4 server package (apt-get install socks4-server):

socks4-server – SOCKS4 server for proxying IP-based services over a firewall

After the installation you need to edit your inetd configration by typing:

update-inetd –add "socks stream tcp nowait nobody /usr/sbin/sockd"

It will edit the /etc/inetd.conf configuration and reload the daemon.

Now just edit the /etc/sockd.conf access list file for authorized connections, example:

permit 192.168.0.2 255.255.255.255

deny  ALL  0.0.0.0  .my.domain  0.0.0.0

Howto: Debian PMTU discovery

Thursday, March 29th, 2007

There is no option to set the DF bit in the regular PING tool in Linux/Debian, there are two alternatives. One is to use the HPING tool and the second good tool TRACEPATH.

Download the Debian package:

iputils-tracepath – Tools to trace the network path to a remote host

After the installation just run tracepath <destination> and the tool will provide path MTU discovery, here is an example:

debian@july:~# tracepath cisco.com
 1:  july.interhost.co.il (84.95.244.160)                   0.204ms pmtu 1500
 1:  84-95-244-2.reserved.interhost.co.il (84.95.244.2)     0.515ms
 2:  80.179.229.1.static.012.net.il (80.179.229.1)         18.426ms
 3:  pt-212.199.73.69.static.012.net.il (212.199.73.69)     0.597ms
 4:  pos3-0.ar03.ldn01.pccwbtn.net (63.218.13.65)          74.534ms
 5:  63.218.94.186   asymm  9 160.245ms
 6: 63.218.94.186  asymm  9 159.253ms
 7:  ded4-g8-3-0.sntc01.pbi.net (151.164.41.165)       asymm 11 227.422ms
 8:  64.161.0.62 asymm 16 214.835ms
 9:  sjc5-dmzbb-gw1.cisco.com (128.107.224.105)      asymm 15 214.120ms
10:  sjce-dmzbb-gw1.cisco.com (128.107.224.2)         asymm 15 212.825ms
11:  sjck-dmzdc-gw1-gig1-1.cisco.com   asymm 15 212.723ms

It will also finds asymmetric routes which is very useful too.