Skip to content

Instantly share code, notes, and snippets.

@VAD3R-95
Created May 11, 2017 09:26
Show Gist options
  • Save VAD3R-95/35f963a8466a34f9ea0089eac62c680f to your computer and use it in GitHub Desktop.
Save VAD3R-95/35f963a8466a34f9ea0089eac62c680f to your computer and use it in GitHub Desktop.
Playing with shell
#!/bin/bash
# Everyday bash script to automate the boring stuff :-)
#
# ---++++++-------VAD3R--------++++
date; # displays date
echo "uptime:"
uptime # total up time for client
echo "Currently Connected to:"
w # info about current connection for user
echo "+++++++++++++++++++++++"
echo "Last logins :"
last -a |head -3 # info about last 3 logins on systems by users
echo "+++++++++++++++++++++++"
echo "Disk and memory usage :"
df -h # disk space ih human -readable format
echo ""
free -m | xargs | awk '{print "Free/total memory: " $8 " / " $17 " MB"}' # free/used space free -m
echo "+++++++++++++++++++++++"
tart_log=`head -1 /var/log/messages |cut -c 1-12`
oom=`grep -ci kill /var/log/messages`
echo -n "OOM errors since $start_log :" $oom # info on out of memory (killed process) error
echo ""
echo "++++++++++++++++++++++"
echo "Utilization and most expensive process: "
top -b |head -3 # cpu % usage and tasks
echo
top -b |head -10 |tail -4 # cpu % usage and tasks w.r.t. users info
echo "++++++++++++++++++++++"
echo "Open TCP ports:"
nmap -T4 127.0.0.1 # Tcp port scanning
echo "++++++++++++++++++++++"
echo "Current connections :"
ss -s # connections info
echo "processes: "
ps auxf --width=200 # info about all processes
echo "++++++++++++++++++++++"
echo "vmstat :"
vmstat 1 5 # more info on os processes
echo "+++++++++++++++++++++++"
echo "finally update system alias :"
xterm -hold -e sudo apt-get dist-upgrade # sysupdate
#----- taken from techmint.com (tweek as much as you like)
#
# Port Scanner +++++-----VAD3R------+++
# (inspiration - null_byte)
# usage ./portscanner.pl -h
#
use Net::Ping;
use Socket; # socket(SOCKET, DOMAIN, TYPE, PROTOCOL)
$| = 1; # auto flush
my ($ip, $protocol, $port, $iaddr, $myhouse, $log, $p_icmp, $ret_icmp, $duration, $duration_icmp, $ret_tcp, $duration_tcp, $p_tcp);
($ip, $port, $port_stop, $log) = @ARGV;
chomp(@ARGV);
if (($ip eq '-h') or (@ARGV ne 4) ) {
&usage();
}
$p_tcp = Net::Ping->new("tcp"); # to check if host is up/down
($ret_tcp, $duration_tcp) = $p_tcp->ping($ip);
sleep(1);
$p_icmp = Net::Ping->new("icmp");
($ret_icmp, $duration_icmp) = $p_icmp->ping($ip);
sleep(1);
if((!$ret_tcp) && (!$ret_icmp) ) {
$duration = $duration_tcp + $duration_icmp;
print "Time Duration to check if host up or down : $duration\n";
print "Host $ip is probably down check again...\n";
exit 1;
}
else {
$protocol = getprotobyname('tcp') ;
unless (open (LOG_FILE, ">>$log")) {
die "can`t open file $log: $!\n"
}
# Flushing file buffer after every write
select((select(LOG_FILE), $| = 1)[0]);
print LOG_FILE "Following ports are opened on $ip between $port and $port_stop\n\n";
print "Checking $ip for open ports....\n";
for (; $port < $port_stop; $port++) {
socket(SOCK, PF_INET, SOCK_STREAM, $protocol) || die "socket: $!";
$iaddr = inet_aton($ip) || die "Cannot connect : $!";
$myhouse = sockaddr_in($port, $iaddr);
if(!connect(SOCK, $myhouse)) {
printf "%d\r", $port;
}
else {
printf "%d <- open\n", $port;
print LOG_FILE "$port\n";
close SOCK || die "Close : $!\n";
}
}
close LOG_FILE || die "close : $!\n";
printf "PORT SCAN COMPLETE.\n";
printf "ABOVE ALL OPEN PORTS FOR: $ip\n";
}
sub usage() {
print "Usage : ./portscanner.pl [host] [min_port] [max_port] [log_file]\n";
print "NO DEFAULT !! Use as mentioned above\n";
exit 0;
}
#
# takes lot of time on firewalled n/w but is silent and accurate ;-)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment