Text Manipulation

WHILE loop:

while read line; do (echo $line); done

FOR loop:

for name in {1..10}.txt; do touch $name; done

Read A Line On A File:

$ cat file.txt | while read line; do (echo $line); done
cut -d":" -f1 -s /etc/group | (while read line; do sudo chown -R $line.$line ./$line; done)
cut -d":" -f1 -s /home/ec2-user/passwd | while read line; do sudo chown -R $line.$line /home/$line ; done
cut -d"/" -f3 DreamPunsDBSST.list | while read line; do mv ./DreamParkDB/$line ./DreamPunsDBSST/$line; done

Read the mailboxes lines and copy to archive directory

cat mailboxes.txt | while read line; do cp ./mail/$line ./mail_archives/ ; done

Substitution:

replace ‘cat’ by ‘dog’ in file in.txt and output it to file out.txt the ‘g’ means replace all matches, not just the first on a given line

sed -e 's/cat/dog/g' in.txt > out.txt

Batch File Rename:

for file in *.* ; do mv [SOURCE_NAME] [DEST_NAME]; done

Batch File Rename Ext:

for file in *.bkp
do
mv $file `basename $filename .bkp`;
done

sudoing with a tty using pssh

ENABLE sudo COMMAND WITH A tty USING pssh

pssh -h /usr/local/etc/group/cassadb -i -l ec2-user -x '-t -t -c blowfish' 'sudo service cassandra status'

SSH Keys Tricks

SSH With Identity Key:

sudo ssh -i /home/myself/.ssh/id_rsa user@domain.com

SSH Agent Forwarding:

eval `ssh-agent`
sudo ssh -A user@domain.com

Appending File Using SSH:

cat id_rsa.pub | ssh user@host.domain.com 'cat >> /home/user/.ssh/authorized_keys'
cat ~/.ssh/id_dsa.pub | ssh user@host.domain.com 'mkdir .ssh; cat >> .ssh/authorized_keys'
cat ~/.ssh/id_rsa.pub | ssh user@domain.com 'cat >> .ssh/authorized_keys'
for i in server1 server2 server3 server4; 
do 
    echo "sending file to $i";
    cat localfile | ssh -p portnumber user@$i "cat >> remotefile"; 
done;

SSHFS With Identity Key:

sshfs -o idmap=user -o ssh_command="ssh -i /root/server.pem"  user@domain.com:/media/vol /mnt/partition
sshfs -o sshfs_sync -o idmap=user apache@domain.org:/var/www/srv DevOps/ 

Software RAID manipulations

Creating Software RAID Array:

mdadm --create --verbose /dev/md0 --level=linear --raid-devices=2 /dev/sdb6 /dev/sdc5
mdadm --create --verbose /dev/md0 --level=stripe --raid-devices=2 /dev/sdb6 /dev/sdc5

mdadm --create --verbose /dev/md0 --level=raid0 --raid-devices=2 /dev/sdb /dev/sdc
mdadm --create --verbose /dev/md0 --level=raid0 --raid-devices=2 /dev/xvdb /dev/xvdc
mdadm --create --verbose /dev/md0 --level=raid0 --raid-devices=4 /dev/xvdb /dev/xvdc /dev/xvdd /dev/xvde

mdadm --create --verbose /dev/md0 --level=raid0 --raid-devices=4 /dev/xvdf /dev/xvdg /dev/xvdi /dev/xvdj

Generate A Lost mdadm.conf File:

mdadm -Es | grep md > /etc/mdadm/mdadm.conf
mdadm --examine --scan > /etc/mdadm.conf

Network Services And Ports

NETCAT:

  • Test a listening port
nc -vz mail.domain.com 587

NETSTAT:

  • List all active TCP and UDP port
netstat -tupanc
  • List all TCP ports in listening state
netstat -tl
  • List all UDP ports in listening state
netstat -ul
  • List all UNIX domain sockets in listening state
 netstat -xl

TRACEROUTE:

traceroute -T mail.domain.com -p 25

NMAP:

  • Network Fast Discovery With Nmap
nmap -A -F -T5 192.168.1.200-250