Using Top Efficiently

March 30, 2006

Here is a link disussuing using TOP to create performance snapshots and other tips.


PERL for search and replace

March 16, 2006

You can use PERL as a stream editor to replace strings in one or more files in a directory like this:

perl -pi.orig -e ‘s/bgcolor=#ffffff/bgcolor=#000000/i’ *.html

The above replaces the color white with black in all html files in this directory and saves the original files with .orig extension.


Keep local environment when logging to remote servers

March 15, 2006

Use the script movein.sh from O’Reilley.

#!/bin/sh

if [ -z "$1" ]; then
echo "Usage: `basename $0` hostname"
exit
fi

cd ~/.skel
tar zhcf - . | ssh $1 "tar zpvxf -"

Read link for more info...


Generate SSL Certificate

March 15, 2006

Need OpenSSL for this -

openssl genrsa 512/1024 http://www.mysite.com.key
openssl req -new -key http://www.mysite.com.keyhttp://www.mysite.com.csr

Enter country, state, locality, org name, org unit, comon name, and email when asked. Note: Common Name is http://www.mysite.com.

openssl -req -x509 -key http://www.mysite.com.key -in http://www.mysite.com.csr http://www.mysite.com.crt


Apache Tweaks

March 15, 2006

Avoid truncated filenames in log by editing httpd.conf:
IndexOptions FancyIndexing NameWidth=*

Here is a handy Perl script to compress all Apache logs automatically called LogFlume.

Serve an additional site under same DocumentRoot:

Add new Virtual Host:

ServerName http://www.newsite.biz
ServerAdmin mike@newsite.biz
DocumentRoot /home/www/htdocs #same as other site
RewriteEngine On
RewriteRule ^/$ /home/www/htdocs/newsite/index.html
RewriteRule ^/index.html$ /home/www/htdocs/newsite/index.html


Apache Toolbox

March 15, 2006

When installing Apache, check out Apache Toolbox and this article/script.


Restore single MySQL table from large DB dump

March 15, 2006

Here’s how to restore one or more tables from a full database archive. It assumes you used mysqldump to save the database and stored it in a gz file. First create the file extract-table.pl:

#!/usr/bin/perl -wn
# extract-table.pl
BEGIN { $table = shift @ARGV }
print if /^create table $table\b/io .. /^create table (?!$table)\b/io;

Then do the following to restore the table:

zcat /path/mysqldump.date.gz | extract-table Mytable> ~/Mytable.dump
mysql  mydatabase -e "drop table Mytable"
mysql mydatabase


mtop/mkill – MySQL Monitoring Tools

March 15, 2006

mtop can be used like top in Unix. Download from HERE first.

Must start mysql with mtop user and passwd:
mysql --dbuser=monitor --password=n0tell


ssh Tips

March 15, 2006

X over ssh: ssh -X hostname

Forward port over ssh:
ssh -f -N -L110:mailhost:110 -l user mailhost


Monitoring Web Traffic in Real Time with httptop

March 15, 2006

See who’s hitting your web server the hardest up to the second with httptop a Perl script from O’Reilley at  http://www.oreillynet.com/pub/h/65#code


nmap Get remote system o/s and more

March 15, 2006

To find out what type of machine a host is, type:

nmap -O host

Compare services on your server or subnet, run this at 2 different times and then diff the files -

nmap -sX 10.14.34.0/24 | egrep -v '^(Nmap|Starting)' > nmap.out.date


ntop Quickstart Guide

March 15, 2006

To get ntop real-time network statsitics – Do the following as root:

groupadd ntop
useradd -c "ntop user" -d /usr/local/etc/ntop -s /bin/true -g ntop ntop
mkdir /usr/local/etc/ntop
(unpack and build ntop as per directions)
ntop -A -u ntop -P /usr/local/etc/ntop
ntop -u ntop -P /usr/local/etc/ntop -W4242 -d

Access it via port 3000 in your browser. 


Find open ports and associated processes

March 15, 2006

netstat -lnp (as root)

gives you PID for any port, then just type:
ps auwex |grep -w PID

Also, try lsof -p PID to get files and sockets.


Use watch for repeated commands

March 15, 2006

Instead of constantly typing ‘ps -ef|grep yada’ over and over, use watch to highlight differences and tell you when the command finishes.

watch 'ps -ef|grep tar'


iptables tips & tricks

March 15, 2006

iptables is a firewall app that can filter packets.

#Detect DoS attack by limiting to 12 connections/sec
iptables -t nat -N syn-flood
iptables -t nat -A syn-flood -m limit --limit=12/s --limit-burst 24 -j RETURN
iptables -t nat -A syn-flood -j DROP
iptables -t nat -A PREROUTING -i $EXT_IFACE -d $DEST_IP -p tcp --syn -j syn-flood

Note that $DEST_IP is ultimate requested IP of packet, and $EXT_IFACE is public interface of the firewall.

#Drop Xmas and NULL packets
iptables -t nat -A PREROUTING -p tcp --tcp-flags ALL ALL -j DROP
iptables -t nat -A PREROUTING -p tcp --tcp-flags ALL NONE -j DROP


Follow

Get every new post delivered to your Inbox.