Showing posts with label Network. Show all posts
Showing posts with label Network. Show all posts

Friday, July 9, 2010

Simple SH script to monitor basic connection counts on *nix systems

I wrote near a year ago a sh script to see my traffic information which can be used to see basic connection counts like unix connection count, tcp connection count, http inbound and outgoing request counts and ftp connection count, ssh connection count.
Save as mycons.sh then run the command
chmod +x mycons.sh
For short access run
ln mycons.sh /bin/mycons
Sample usage after shortcut
mycons -hl
Here is the code for mycons.sh: http://pastebin.com/dBpPMGeY
#HEADER SECTION STARTS
#------------------------------------------------------------------------
#This script is written by Mustafa TURAN (HOME http://mustafaturan.net/
#BLOG http://vpslife.blogspot.com/) to monitor some basic network actions
#and display some user specific information like hd usage...
#you are feel free to distribute re-use in any of your application
#without deleting HEADER section. Also, it licensed with CC 3.0
#Also see: http://creativecommons.org/licenses/by/3.0/
#-------------------------------------------------------------------------
#HEADER SECTION ENDS


#
# Function to print help
#
print_help()
{
        echo "Usage: $0 -c -h -l -i";
        echo "Where -c clear screen";
        echo "      -h shows hd usage size";
        echo "      -l count of connections";
        echo "      -i show ip list";
        return
}

#
# Function to clear the screen
#
cls()
{
        clear
        return
}

#
# Function to show hd space
#
print_hdspace()
{
        echo -n "Total Space Used: "
        /usr/bin/du -hs /home/www-data/mustafat #write here your home path
        echo "-------------------------------------------------------------------------"
        return
}

#
# Function to connection counts
#
print_connection_count()
{
        unix_connections=`/bin/netstat -an | grep unix | grep CONNECTED | wc -l` #unix connection count
        tcp_connections=`/bin/netstat -an | grep ESTABLISHED | wc -l` #tcp connection count
        ftp_connections=`/bin/netstat -an | grep :21\  | grep ESTABLISHED | wc -l` #fcp connection count
        ssh_connections=`/bin/netstat -an | grep :22\  | grep ESTABLISHED | wc -l` #ssh connection count
        http_connections=`/bin/netstat -an | grep :80\  | grep ESTABLISHED | wc -l` #http connection count
        h_out_connections=`/bin/netstat -an | grep :80\  | grep ESTABLISHED |  awk '{print $5}' | grep :80 | wc -l` #http requests count (downloading file from somewhere else)
        h_in_connections=`/bin/netstat -an | grep :80\  | grep ESTABLISHED |  awk '{print $4}' | grep :80 | wc -l` #http response count (someone display a web page from this server)

        echo "CONNECTIONS"
        echo "-------------------------------------------------------------------------"
        echo -e "UNIX\tTCP\tFTP\tSSH\tHTTP\tH-Input\tH-Output"
        echo -e "$unix_connections\t$tcp_connections\t$ftp_connections\t$ssh_connections\t$http_connections\t$h_in_connections\t$h_out_connections\n"
        return
}

#
# Function to ip list http connections
#
print_iplist()
{
        echo "List of ip addresses:"
        echo "-------------------------------------------------------------------------"
        /bin/netstat -an | grep :80 | grep ESTABLISHED | awk '{print $5}'
        return
}

#
# Main procedure start here
#
# Check for sufficent args
#

if [ $# -eq 0 ] ; then
    print_help
    exit 1
fi

# Now parse command line arguments
#
while getopts chlik: opt
do
    case "$opt" in
        c) cls;;
        h) print_hdspace;;
        l) print_connection_count;;
        i) print_iplist;;
        \?) print_help; exit 1;;
    esac
done

VNSTAT: Network Traffic Monitor for *nix Systems

Vnstat is a network stats software, an opensource project for *nix systems and can be downloaded from http://humdi.net/vnstat/. For more than a year I am using Vnstat and it works silently without using too much resources and gives required stats when it is needed.
In details, it is possible to see your network adapters' traffic data based on years, months, weeks, days, hours and even real time data flow rates, running vnstat at the background with a small system resource like 1mb ram.

Setup

cd /usr/local/src
wget http://humdi.net/vnstat/vnstat-1.10.tar.gz
tar zxvf vnstat-1.10.tar.gz
cd vnstat-1.10
make
make all
make install
vnstat -u -i eth0 #eth0 should be replaced by your network adapter
vi /etc/vnstat.conf #change eth0 in conf file with your network adapter
OK configuration fineshed, now it is time to setup startup script which can be downloaded from
/etc/init.d scripts for vnStat daemon
Some Commands
vnstat --help # I think this will be enough :)

Sunday, March 15, 2009

Change Hostname on Centos 5x

On terminal screen apply the commands:
vi /etc/sysconfig/network

Then change HOSTNAME=xxxx.xxx line to HOSTNAME=your_desired_hostname
You have to save the data and quit(thats all!):

wq!

Tuesday, March 10, 2009

Copy Remote Database to Local Database

Remote mysqldump

Run this one line command as a user:


mysqldump --opt --compress --user=REMOTE_DB_USERNAME --password=REMOTE_DB_PASS --host=REMOTE_DB_HOSTNAME REMOTE_DB_NAME mysql --user=LOCAL_DB_USERNAME --password=LOCAL_DB_PASSWORD --host=LOCAL_DB_HOSTNAME -D LOCAL_DB_NAME -C LOCAL_DB_NAME

Notes: You have to enable remote mysql on remote computer! (To do this you have to add ip address of local computer to remote computer's db remote access ip list)

Thursday, October 2, 2008

How to open port on Linux?

By default Linux Operating Systems has a few ports open such as SSH(22) FTP(21) etc... However, we need more ports needed to open for another applications such as admin panels, several servers like apache, mysql and so on.
Open file with your favorite editor my own is vi:
# vi /etc/sysconfig/iptables
Add following line to the file to enable https:// which is secure http for your server :
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT

You can change 443 port to which port you want to enable incoming transfers.

After doing changes you shoul restart network adapter with command on RHEL, CentOS, Fedora using:
# service iptables restart

For other Linux OS'es you can reboot or restart iptables somehow.