Bash Scripting

From Dikapedia
Jump to: navigation, search


My fave cheatsheet site: https://devhints.io/bash


How to commit scripts to GitHub: Git


Ardika's GitHub Repo


Add:

netstat -plan | awk '/.*[0-9]+.[0-9]+.[0-9]+.[0-9].*/{gsub(/::ffff:/,"",$0);print $4"\t" $5}'|cut -sd. -f 1->netstat.log;echo "Netstat  report";echo;echo "Number of Connections to each port:";cat netstat.log |awk {'print $1'}|cut -d: -f 2|sort|uniq -c|sort -nk 1|tail;echo;echo  "Number of connections from each IP:";cat netstat.log |awk {'print $2'}|cut -d: -f 1|sort|uniq -c|sort -nk 1|tail;echo;echo "The number of instances of a particular IP connecting to particular port";cat netstat.log |awk {'print $1 "\t" $2'}|cut -d: -f 2|sort|uniq -c|sort -nk 1|tail;

Wanted to share a quick netstat "tally" script I wrote, that I've found to be useful on several occasions:

Shows top 10 IPs holding connections, as well as a breakdown of active connnections by port. Useful for troubleshooting connection flooding issues as well as helps making usage based recommendations for things like Apache spare server settings etc. Hope some of you may find it helpful.


For loop


Basic for loop to create 30 users:

# for i in {1..30}; 
> do useradd user$i; 
> done


For loop to delete the line in files that contains string "aws-replication"

# cat /usr/bin/forloop.sh 
#!/bin/bash


etc_file=$(grep -irl "aws-replication" /home/ec2-user/)

for file in "etc_file"; do
    ls -al $etc_file
    sed -i  '/aws-replication/d' $etc_file  

done
-----------------------

# grep -i "aws-replication" /home/ec2-user/test2
aws-replication:!!:19200::::::

# grep -i "aws-replication" /home/ec2-user/test
disk:x:6:aws-replication
aws-replication:x:1001:

# /usr/bin/forloop.sh 
---------- 1 root root 705 Jul 27 18:22 /home/ec2-user/test
---------- 1 root root 705 Jul 27 18:22 /home/ec2-user/test2

# grep -i "aws-replication" /home/ec2-user/test
# grep -i "aws-replication" /home/ec2-user/test2
# (no output as lines were deleted)