2008年7月29日 星期二

3 methods to read text file line by line as variable in shell script of Unix

1.

while read var
do
..$var..
done < FILE

2.

cat FILE |
while read var
do
..$var..
done

3.

for var in `cat FILE`
do
..$var..
done

2008年7月10日 星期四

Reset gnome settings on Ubuntu

if you delete gnome panels accidently, these steps can help you reset all your gnome settings to default

1. logout

2. ctrl + alt + f1 to terminal console

3. rm -rf .gnome .gnome2 .gconf .gconfd .metacity

4. ctrl + alt + f7 to gui desktop

5. login

2008年7月9日 星期三

Set up MRTG on Ubuntu

1. MRTG( Multi Router Traffic Grapher ) working depends on SNMP( Simple Network Management Protocol )

sudo apt-get install apache2 snmpd mrtg

2.

vi /etc/snmp/snmpd.conf
--
..
# sec.name source community
#com2sec paranoid default public
com2sec readonly default public
#com2sec readwrite default private
..

3. use cfgmaker command to create a mrtg config file

# public is the community name set in step 2
cfgmaker public@localhost > /etc/mrtg.cfg

4. creat mrtg webpages by using indexmaker to read mrtg con

indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html

2008年7月3日 星期四

Load IPTables rules while booting on Ubuntu

1. make you rules as a script

vi /root/firewall.start

2. make another script to flush all rules and allow all connections

vi /root/firewall.stop
--
echo "Stopping firewall and allowing everyone..."
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

3. chmod 700 /root/firewall.*

4. load rules

sudo /root/firewall.start


5. use iptables.save to save rules as a formatting file for iptables-restore

sudo sh -c "iptables-save > /etc/iptables.rules"

# restore rules with iptables-restore
# sudo iptables-restore < /etc/iptables.rules


6. to restore configuration on startup, the suggested method is to use ifup.d networking scripts, which are executed on state changes of the network interfaces.

sudo vi /etc/network/if-pre-up.d/iptables

--
#!/bin/sh

# Load iptables rules before interfaces are brought online
# This ensures that we are always protected by the firewall
#
# Note: if bad rules are inadvertently (or purposely) saved it could block
# access to the server except via the serial tty interface.
#

RESTORE=/sbin/iptables-restore
STAT=/usr/bin/stat
IPSTATE=/etc/iptables.rules

test -x $RESTORE || exit 0
test -x $STAT || exit 0

# Check permissions and ownership (rw------- for root)
if test `$STAT --format="%a" $IPSTATE` -ne "600"; then
echo "Permissions for $IPSTATE must be 600 (rw-------)"
exit 0
fi

# Since only the owner can read/write to the file, we can trust that it is
# secure. We need not worry about group permissions since they should be
# zeroed per our previous check; but we must make sure root owns it.
if test `$STAT --format="%u" $IPSTATE` -ne "0"; then
echo "The superuser must have ownership for $IPSTATE (uid 0)"
exit 0
fi

# Now we are ready to restore the tables
$RESTORE < $IPSTATE

7. make the rules safer with permission change and make this script executable

sudo chmod 600 /etc/iptables.rules
sudo chmod +x /etc/network/if-pre-up.d/iptables