Some usefull command and bash script

Posted: January 13, 2012 in LINUX
Tags: , , ,

“FOR AEONS HAVE I HAVE ROAMED ON THE INTERNET”

This script and all other things here are collected from internet. There is no where in my creativeness. All thanks going to be those GURUZ who make it available to us..

This is useful to find out if your server is under attack or not. You can also list abusive IP address using this method.
1).###Daily Command for Admin
# netstat -nat | awk ‘{print $6}’ | sort | uniq -c | sort -n

Dig out more information about a specific ip address:
# netstat -nat |grep {IP-address} | awk ‘{print $6}’ | sort | uniq -c | sort -n

Busy server can give out more information:
# netstat -nat |grep 202.54.1.10 | awk ‘{print $6}’ | sort | uniq -c | sort -n

Get List Of All Unique IP Address

To print list of all unique IP address connected to server, enter:
# netstat -nat | awk ‘{ print $5}’ | cut -d: -f1 | sed -e ‘/^$/d’ | uniq
To print total of all unique IP address, enter:
# netstat -nat | awk ‘{ print $5}’ | cut -d: -f1 | sed -e ‘/^$/d’ | uniq | wc -l

Find Out If Box is Under DoS Attack or Not

If you think your Linux box is under attack, print out a list of open connections on your box and sorts them by according to IP address, enter:
# netstat -atun | awk ‘{print $5}’ | cut -d: -f1 | sed -e ‘/^$/d’ |sort | uniq -c | sort -n

A few more tools:

nmap – scan your server for open ports.
lsof – list open files, network connections and much more.
ntop web based tool – ntop is the best tool to see network usage in a way similar to what top command does for processes i.e. it is network traffic monitoring software. You can see network status, protocol wise distribution of traffic for UDP, TCP, DNS, HTTP and other protocols.
Conky – Another good monitoring tool for the X Window System. It is highly configurable and is able to monitor many system variables including the status of the CPU, memory, swap space, disk storage, temperatures, processes, network interfaces, battery power, system messages, e-mail inboxes etc.
GKrellM – It can be used to monitor the status of CPUs, main memory, hard disks, network interfaces, local and remote mailboxes, and many other things.
vnstat – vnStat is a console-based network traffic monitor. It keeps a log of hourly, daily and monthly network traffic for the selected interface(s).
htop – htop is an enhanced version of top, the interactive process viewer, which can display the list of processes in a tree form.
mtr – mtr combines the functionality of the traceroute and ping programs in a single network diagnostic tool.

 

2) ### Script for Hardware information

 

#!/bin/bash

#
# server_info.sh – display server hardware info
#
# 2008 – Mike Golvach – eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

hwinfo=”/usr/sbin/hwinfo –short”
hostname=`hostname`
separator=”—————————————-”
echo $separator
echo “System Information For $hostname”
echo $separator
echo $separator
echo SERVER – MEMORY
echo $separator
/usr/sbin/hwinfo –bios|egrep ‘OEM id:|Product id:|CPUs|Product:|Serial:|Physical Memory Array:|Max. Size:|Memory Device:|Location:|Size:|Speed:|Location:’|sed -e ‘s/”//g’ -e ‘/^ *Speed: */s/Memory Device:/\n Memory Device:/’ -e ‘s/\(Max. Speed:\)/CPU \1 MHz/’ -e ‘s/\(Current Speed\)/CPU \1 MHz/’
echo $separator
echo SMP
echo $separator
$hwinfo –smp
echo $separator
echo CPU
echo $separator
$hwinfo –cpu
echo $separator
echo CD_ROM
echo $separator
/usr/sbin/hwinfo –cdrom|egrep ’24:|Device File:|Driver:’|awk -F”:” ‘{ if ( $1 ~ /[0-9][0-9]*/ ) print $0; else print ” ” $2}’|sed -e ‘s/^.*[0-9] //’ -e ‘s/ //’ -e ‘s/”//g’
echo $separator
echo DISK
echo $separator
$hwinfo –disk
echo $separator
echo PARTITION
echo $separator
$hwinfo –partition
echo $separator
echo NETWORK
echo $separator
$hwinfo –network
echo $separator
echo NETCARD
echo $separator
$hwinfo –netcard
echo $separator

 

3)####Simple Ping monitor on Linux

 

#!/bin/bash
# Simple SHELL script for Linux and UNIX system monitoring with
# ping command
# ————————————————————————-
# Copyright (c) 2006 nixCraft project <http://www.cyberciti.biz/fb/&gt;
# This script is licensed under GNU GPL version 2.0 or above
# ————————————————————————-
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ————————————————————————-
# Setup email ID below
# See URL for more info:
# http://www.cyberciti.biz/tips/simple-linux-and-unix-system-monitoring-with-ping-command-and-scripts.html
# ————————————————————————-

# add ip / hostname separated by white space
HOSTS=”cyberciti.biz theos.in router”

# no ping request
COUNT=1

# email report when
SUBJECT=”Ping failed”
EMAILID=”me@mydomain.com”
for myHost in $HOSTS
do
count=$(ping -c $COUNT $myHost | grep ‘received’ | awk -F’,’ ‘{ print $2 }’ | awk ‘{ print $1 }’)
if [ $count -eq 0 ]; then
# 100% failed
echo “Host : $myHost is down (ping failed) at $(date)” | mail -s “$SUBJECT” $EMAILID
fi
done

 

 

4)##BVegginers Script Show Process or every “N” second

 

#!/bin/bash
# Write a shell script to display the process running on the system for every
# 30 seconds, but only for 3 times.
# ————————————————————————-
# Copyright (c) 2008 nixCraft project <http://www.cyberciti.biz/fb/&gt;
# This script is licensed under GNU GPL version 2.0 or above
# ————————————————————————-
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ————————————————————————-
#
# for loop 3 times
for r in 1 2 3
do
#see every process on the system
echo “**************************** x^x^x ****************************”
ps -e
echo “**************************** x^x^x ****************************”
#sleep for 30 seconds
sleep 3
# clean
done

 

 

 

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s