This is a oneliner bash command to determine my 10 most used linux commands according to my history file
history | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n10
The result
1 188 37.6% vi
2 38 7.6% ls
3 24 4.8% cat
4 22 4.4% apt-get
5 12 2.4% date
6 11 2.2% tail
7 11 2.2% cd
8 10 2% rm
9 10 2% man
10 9 1.8% basename
It looks like i use vim a lot on my home server. You should try it yourself and see what commands you use the most.
Source: http://linux.byexamples.com
Tags: awk, bash, count, grep, head, nl, sort
Posted by Hans-Henry Jakobsen
This is a bash oneliner to show Apache web connections pr hour. It lists up the IPs that has accessed your webserver and the amount og accesses.
# cat /var/log/apache2/access_log_pario.no | grep "21/Jan/2008:.." | awk {' print $4":"$1 '} | sed 's/\[//g' | awk -F : {' print $1":"$2"\t\t"$5 '} | sort | uniq -c
Example output
37 21/Jan/2008:00 192.168.0.10
This shows that I had 37 hits from 00:00 – 01:00 in 20th February 2008.
Tags: Apache, awk, bash, grep, sed
Posted by Hans-Henry Jakobsen
This is one way to determine the ports needed to open in your iptables rules to get NFS to work properly. First we need to determine the ports NFS uses
rpcinfo -p | awk -F " " '{print $3 ", " $4 ", " $5}' | sort | uniq
Notice!
Since portmap assigns ports on random this example is only valid as long as you don’t restart your NFS.
On my system, a RedHat Enterprise Linux WS 4, the result was
proto, port, tcp, 111, portmapper tcp, 2049, nfs tcp, 32771, nlockmgr tcp, 768, rquotad tcp, 782, mountd udp, 111, portmapper udp, 2049, nfs udp, 32768, nlockmgr udp, 765, rquotad udp, 779, mountd
This gave me a nice overview of protocols (tcp/udp) and ports used.
Now the rules
iptables -A RH-Firewall-1-INPUT -s 192.168.0.0/255.255.255.0 -i eth0 -p tcp -m state --state NEW -m multiport --dports 111,2049,32771,768,782 -j ACCEPT iptables -A RH-Firewall-1-INPUT -s 192.168.0.0/255.255.255.0 -i eth0 -p udp -m state --state NEW -m multiport --dports 111,2049,32768,765,779 -j ACCEPT
You see that the multiport statement is just like the result of my rpcinfo command above.
Remember to save your new rules, othervise they will disappear the next time the iptables rules are being loaded.
In addition to this rule you should add the iptables rule for ssh access I wrote about earlier.
Another way to determine the ports
nmap -sC -p 111 localhost
Notice!
This solution won’t work after a reboot of the server since NFS changes ports. One way to overcome this problem is to follow the instructions in a newer post I’ve made about RedHat and NFS.
Tags: awk, iptables, nmap, rpcinfo, sort
Posted by Hans-Henry Jakobsen
This bash script adjusts images to fit in a digital frame with a resolution of 480×234 pixels however by using the size 832×468 pictures are displayed sharper on some frames. Normally, narrower pictures will have black borders at both sides when displayed. but this script makes the border color the average of the picture.
The following linux tools are used in the script: convert and montage, bash, od and awk. od is part of the textutils package in Debian.
#!/bin/bash
# current directory contains source JPG files
# $DESTINATION is where the prepared JPG files are stored
# $TEMP is a temporary directory
TEMP=TEMP
DESTINATION=DESTINATION
GEO=832x468 # geometry of target JPG files and resize value
for FILE in *.JPG
do
convert -resize 1x1 "$FILE" "$TEMP/1x1.bmp"
od -A n -j 54 -t u1 "$TEMP/1x1.bmp" | awk -v f="$FILE" -v des="$DESTINATION/" -v geo="$GEO" '
{
print "montage -geometry", geo, "-resize", geo, "-background rgb\\(" $3 \
"," $2 "," $1 "\\) \"" f "\" \"" des f "\"";
exit;
}'
done | sh -x
Source: comp.graphics.misc
Tags: awk, bash, convert, Debian, montage, od
Posted by Hans-Henry Jakobsen
#!/bin/bash
# nslookup-scan of IP-range
# It's possible to add more networks separated with space
NETS="192.168.0"
IPRange="1 254"
for NET in $NETS; do
for n in $(seq $IPRange); do
ADDR=${NET}.${n}
echo "${ADDR},`nslookup ${ADDR} | awk -F "=" '{ print $2 }'|sed 's/^[ t]*//' | sed '/^$/d' | |sed 's/.$//'`"
done
done
Result
192.168.0.1,cba.infra.no 192.168.0.2,bca.infra.no 192.168.0.3,abc.infra.no 192.168.0.4, 192.168.0.5, 192.168.0.6, 192.168.0.7, 192.168.0.8,
Tags: awk, bash, nslookup, sed
Posted by Hans-Henry Jakobsen