luni, 31 ianuarie 2011

Exemple de automatizari in BASH

Au ramas nepostate cateva scripturi pe care le-am facut de-a lungul timpului pentru conky sau chiar pentru utilizare directa in consola, asa ca, tu, cel care cauta pe google, daca ai ajuns aici, te poti folosi cu incredere de aceste exemple.
Pot fi folosite si pe post de template din care sa te inspiri in creearea altor scripturi BASH.


Monitor internet:
(te poate ajuta la identificarea problemelor de conectare la ISP)

#! /bin/bash
# Script de diagnostic al punctelor importante din conexiunea la internet
# Pentru conexiunea externa am folosit unul din ip-urile google-ului
if ping -c 1 -w 1 209.85.229.104 > /dev/null; then
echo "INTERNET ON"
echo "x" > /tmp/netok
else
echo "INTERNET OFF"
rm -f /tmp/netok
fi
if ping -c 1 -w 1 $(cat /etc/resolv.conf | grep -m 1 nameserver | awk '{ print $2}') > /dev/null; then
echo "DNS/ISP GOOD"
else
echo "DNS/ISP BAD"
fi
if ping -c 1 -w 1 $(route -n | grep 'UG' | cut -d: -f2 | awk '{ print $2}') > /dev/null; then
echo "GATEWAY GOOD"
else
echo "GATEWAY BAD"
fi

Ultimele postari active din forum.ubuntu.ro:



#!/bin/bash
numarlinii=5
wget -q http://forum.ubuntu.ro/rss.php -O /tmp/rss.php
cat /tmp/rss.php | grep "description>Topic:" | cut -d"<" -f2 | cut -d">" -f2 | while read line; do
echo $line | sed 's/Topic: /</'| cut -d"<" -f2 | gawk '{
while (match($0,/&#[0-9]+;/))
$0 = substr($0, 1, RSTART-1) sprintf("%c", substr($0, RSTART+2)+0) \
substr($0, RSTART+RLENGTH)
print
}' >> /tmp/content
done
cat /tmp/content | awk '{
if ($0 in stored_lines)
x=1
else
print
stored_lines[$0]=1
}'| head -n $numarlinii
rm -f /tmp/content
rm -f /tmp/rss.php
Extragerea IP-ului WAN curent:

#!/bin/bash
wget http://checkip.dyndns.org/ -q -O - |
grep -Eo '\<[[:digit:]]{1,3}(\.[[:digit:]]{1,3}){3}\>'

In .conkyrc introduceti:



${if_existing /proc/net/route eth0}${font Ubuntu:size=8}WAN IP: ${execi 60 ~/cnkscr/ipshow}${endif}
Lista calculatoare LAN active:
(inlocuiti ip-urile si numele dupa preferinte)

#! /bin/bash
# Script de diagnostic al lan-ului
# Creat pentru conky de Dan Radu in iunie 2010
#echo "Checking hosts..." | zenity --progress --title="Working..." --auto-close
cat /dev/null > /tmp/lanlist
if ping -c 1 -w 1 192.168.123.5 > /tmp/valoare; then
echo "Laptop WIFI " $(cat /tmp/valoare | grep ttl | awk '{ print $7}' | cut -d"=" -f2 ) "ms" >> /tmp/lanlist
fi
if ping -c 1 -w 1 192.168.123.6 > /tmp/valoare; then
echo "Laptop ETH " $(cat /tmp/valoare | grep ttl | awk '{ print $7}' | cut -d"=" -f2 ) "ms" >> /tmp/lanlist
fi
if ping -c 1 -w 1 192.168.123.3 > /tmp/valoare; then
echo "Desktop1 ETH " $(cat /tmp/valoare | grep ttl | awk '{ print $7}' | cut -d"=" -f2 ) "ms" >> /tmp/lanlist
fi
if ping -c 1 -w 1 192.168.123.4 > /tmp/valoare; then
echo "Laptop2 WIFI " $(cat /tmp/valoare | grep ttl | awk '{ print $7}' | cut -d"=" -f2 ) "ms" >> /tmp/lanlist
fi
if ping -c 1 -w 1 192.168.123.7 > /tmp/valoare; then
echo "Laptop2 ETH " $(cat /tmp/valoare | grep ttl | awk '{ print $7}' | cut -d"=" -f2 ) "ms" >> /tmp/lanlist
fi
rm -f /tmp/valoare
if [ $(wc -l /tmp/lanlist | cut -d" " -f1) -gt 0 ]; then
#txtbuf=$(cat /tmp/lanlist)
cat /tmp/lanlist
rm -f /tmp/lanlist
else
echo "NO KNOWN LAN COMPUTER ONLINE"
rm -f /tmp/lanlist
fi

Lista conexiunilor "TCP-IP Incoming" cu rezolvarea numelui de domeniu:


#!/bin/bash
numarlinii=10
netstat -nat | grep ESTABLISHED | awk '{ print $5}' |cut -d":" -f1 | head -n $numarlinii > /tmp/honk
cat /tmp/honk | while read line; do
whois $(echo $line)| egrep -i "address|netname" > /tmp/whoz
if [ $(wc -l /tmp/whoz | cut -d" " -f1) -gt 0 ]; then
echo $(echo $line)" - "$(cat /tmp/whoz | grep -i netname | awk '{ print $2}') $(cat /tmp/whoz | grep -i address | awk '{ print $2,$3,$4,$5}' ) >> /tmp/listazone
fi
done
if [ -f /tmp/listazone ]; then
echo "nimic" > /dev/null
else
echo "NO CONNECTIONS FOR NOW*"
exit
fi
if [ $(wc -l /tmp/listazone | cut -d" " -f1) -gt 0 ]; then
cat /tmp/listazone
rm -f /tmp/whoz
rm -f /tmp/listazone
else
echo "NO CONNECTIONS FOR NOW"
rm -f /tmp/whoz
rm -f /tmp/listazone
fi


Share this on: Share On Facebook ! Add To Del.icio.us ! Share On Digg ! Share On StumbleUpon ! Blog Feed !