ADMIN Created by Jeffrey Ness 2009

Nessy's Linux Oneliners

Search by command:
Apache:
Rewrite to add www.

Bash essentials:
For Loops
While Loops
If Statement
Remove Comments from file
Comment entire file
Print row if LIKE value

Forensics:
Read data from Hard disk

Hardware:
Physical Memory Modules

Network:
Top connecting IP's
Route outbound connection

PHP:
Linux processlist via PHP

RPM:
Verify file's RPM package

System Monitoring:
Check HDD Percent Used

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Print row if LIKE value
Added On: Tue, 19 Jan 2010 12:30:03 -060 | Visitor Rating: 147 Like It

This command will print an entire line if the first column "$1" as the like "42", the $0 represents entire line.
awk '{ if ($1 ~ 42) print $0}' access_log

Rewrite to add www.
Added On: Tue, 19 Jan 2010 11:37:12 -060 | Visitor Rating: 36 Like It

This rewrite rule will check that www. is added to the request and if not add it in.
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^domain.com$ [NC] 
RewriteRule (.*) http://www.domain.com$1 [L,R=301] 

Check HDD Percent Used
Added On: Wed, 06 Jan 2010 15:52:12 -060 | Visitor Rating: 33 Like It

This script can easily be added to a cron job to inform you by email when your server's root filesystem is getting full.
#!/bin/bash
if [ $(/bin/df -h / | tail -n 1 | /bin/awk '{print $5}' | sed 's/%//g') -gt '95' ]
then
/bin/mail -s'Your Drive is over 95% Full' user@domain.com < /dev/null
fi 

Linux processlist via PHP
Added On: Thu, 19 Nov 2009 12:18:20 -060 | Visitor Rating: 27 Like It

With this PHP script you can run Linux command and print them nicely to your browser
<?php
echo "<pre><code>";
$process=`ps aux`;
$process1 = str_replace("<", "&lt;", $process);
$process2 = str_replace(">", "&gt;", $process1);
echo $process2;
echo "</code></pre>";
?>

Comment entire file
Added On: Fri, 11 Sep 2009 09:19:21 -050 | Visitor Rating: 28 Like It

With this simple sed command we can easily comment and entire file.
sed -i"~" 's/^/#/' FILENAME

Route outbound connection
Added On: Tue, 08 Sep 2009 10:32:00 -050 | Visitor Rating: 24 Like It

Using this IPTables rule you can route all outbound traffic through one of your addtional IP addresses on your machine, this is useful if one of your IP addresses get blacked listed.
iptables -t nat -A POSTROUTING -p tcp --dport 25 -j SNAT --to-source 192.168.1.2 

Read data from Hard disk
Added On: Mon, 07 Sep 2009 11:25:48 -050 | Visitor Rating: 26 Like It

By reading the raw binary data off the block device of your hard drive we can then pass it through strings and get human readable data.
dd if=/dev/sda | strings

Physical Memory Modules
Added On: Sun, 06 Sep 2009 18:46:14 -050 | Visitor Rating: 26 Like It

This dmidecode command will display how many physical memory slots are available and which have memory installed (along with the size)
dmidecode | egrep "Memory Device$" -A8 | egrep "Memory|Size"

Remove Comments from file
Added On: Sun, 06 Sep 2009 18:08:00 -050 | Visitor Rating: 24 Like It

Removes comments and spaces from a file with regular expressions
egrep -v "^#|^$" /etc/httpd/conf/httpd.conf

Top connecting IP's
Added On: Sun, 06 Sep 2009 18:08:00 -050 | Visitor Rating: 26 Like It

Discover which IP address has the most open TCP sockets to your machine.
netstat -naplt | grep -v LISTEN | awk '{print $5}' | egrep "^[0-9]" | awk -F: '{print $1}' | sort | uniq -c | sort -rnk1

Verify file's RPM package
Added On: Sun, 06 Sep 2009 18:08:00 -050 | Visitor Rating: 28 Like It

Using RPM we are able to verify if a file belongs to a RPM package and that packages name.
rpm -qif /etc/httpd/conf/httpd.conf

If Statement
Added On: Sun, 06 Sep 2009 18:08:00 -050 | Visitor Rating: 27 Like It

If statements are the backbone of all scripting and programing and are very useful to know.
if [ '1' == '1' ] ; then echo "Success" ; fi

While Loops
Added On: Sun, 06 Sep 2009 18:08:00 -050 | Visitor Rating: 29 Like It

Using a while loop we can execute a command as long as the condition is true.
while [ '1' == '1' ] ; do echo "Never Ending Loop" ; done

For Loops
Added On: Sun, 06 Sep 2009 18:08:00 -050 | Visitor Rating: 28 Like It

Using loops we are able to achieve big goals with small commands.
for i in `ls` ; do echo $i ; done