Archive for the ‘one-liners’ Category
Saturday, June 19th, 2010
I'm bad at counting, so when I'm using awk to print specific fields, I end up with greasy fingerprints on my screen as I manually count out each field. Thanks to my colleague James, here's a script that counts for you!
awk 'NR == 1 { for (i=1;i<=NF;i++) {printf i " "} print ""} {print}' | column -t
Works with STDIN as is, assuming default field separator (space):
[kale@superhappykittymeow log]# tail -n 1 xferlog |awk 'NR == 1 { for (i=1;i<=NF;i++) {printf i " "} print ""} {print}' | column -t
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Sat Jun 19 13:19:25 2010 1 127.0.0.1 220 /var/www/poop/wp-rss2.php b _ i r root ftp 0 * c
Or, if you're lazy like myself, encapsulate it in an alias:
alias count='awk 'NR == 1 { for (i=1;i<=NF;i++) {printf i " "} print ""} {print}' | column -t'
[kale@superhappykittymeow log]# tail -n 1 xferlog | count
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Sat Jun 19 13:19:25 2010 1 127.0.0.1 220 /var/www/poop/wp-rss2.php b _ i r root ftp 0 * c
Tags: awk, oneliners
Posted in one-liners | No Comments »
Saturday, May 29th, 2010
Can't fork but need to see what's going on? Hint: a box that can't fork can often `exec'.
Here are a pair of slick bash functions that can be lifesavers in dire situations:
`ls':
$ myls() { while [ $# -ne 0 ] ; do echo "$1" ; shift ; done ; }
$ myls /etc/s*
/etc/services
/etc/shells
/etc/syslog.conf
`cat':
$ mycat() { while IFS="" read l ; do echo "$l" ; done < $1 ; }
$ mycat /etc/shells
Tags: bash, fork
Posted in one-liners, tips and tricks | No Comments »
Saturday, January 2nd, 2010
There's no built-in way in Urchin to re-run the processing job for all domains (such as after fixing a problem). This can, however, be done on the command line with a while loop:
ls -alh ../usr/local/urchin/data/reports/ |awk '{print $NF}' |while read line ; do /usr/local/urchin/bin/urchin -p"$line" ; done
Tags: urchin
Posted in howto, one-liners | 1 Comment »
Monday, November 30th, 2009
I'm fond of WHOIS data for getting an idea who's visiting a site, though most WHOIS servers return data that's full of disclaimers and irrelevant data. Rather, I much prefer Team Cymru's batch WHOIS lookup server, whois.cymru.com.
First, extract your IPs:
F=ips.out ; echo "begin">>$F ; echo "verbose">>$F ; awk '{print $1}' tech-access_log |sort |uniq>>$F ; echo "end" >>$F
Now send them to Cymru for processing:
nc whois.cymru.com 43 < $F | sort > whois.out
Review whois.out at your leisure for detailed IP information. It's well-formatted, allowing for easily scripting against:
91 | 128.113.197.128 | 128.113.0.0/16 | US | arin | 1986-02-27 | RPI-AS - Rensselaer Polytechnic Institute
91 | 128.113.247.58 | 128.113.0.0/16 | US | arin | 1986-02-27 | RPI-AS - Rensselaer Polytechnic Institute
9121 | 88.232.9.77 | 88.232.0.0/17 | TR | ripencc | 2005-10-27 | TTNET TTnet Autonomous System
9 | 128.2.161.88 | 128.2.0.0/16 | US | arin | 1984-04-17 | CMU-ROUTER - Carnegie Mellon University
9136 | 91.186.50.28 | 91.186.32.0/19 | DE | ripencc | 2006-11-07 | WOBCOM WOBCOM GmbH - www.wobcom.de
9143 | 212.203.31.1 | 212.203.0.0/19 | NL | ripencc | 2000-08-08 | ZIGGO Ziggo - tv, internet, telefoon
Tags: one-liners, whois
Posted in howto, one-liners | No Comments »
Sunday, November 29th, 2009
mysqlshow --status db_name |sort -n -k10 |awk -F\| '($6 !~ /0/)' |awk -F\| '{print $2 " " $6 " " $7 " " $14}' |egrep -v "^ "
Creates a much easier-to-read view of the output of "show table status":
Name Rows Avg_row_length Update_time
wp_users 1 140 2009-08-08 04:13:07
wp_links 9 106 2009-10-16 12:57:32
wp_comments 14 464 2009-11-28 16:09:43
wp_usermeta 15 166 2009-11-29 06:41:19
wp_term_taxonomy 53 40 2009-11-20 14:06:21
wp_postmeta 141 46 2009-11-29 06:44:05
wp_options 172 4624 2009-11-29 06:40:59
wp_term_relationships 357 21 2009-11-21 02:35:42
Tags: mysql
Posted in one-liners, tips and tricks | No Comments »
Sunday, November 15th, 2009
Tags: stupid bash tricks
Posted in one-liners | No Comments »
Friday, October 30th, 2009
(echo "smtp: `(cat maillog maillog.processed && zcat maillog.processed.*) | grep bytes | grep qmail: | awk '{sum=sum+$11} END { print sum}'`" && (cat maillog maillog.processed && zcat maillog.processed.*) | grep pop3 | grep LOGOUT | awk '{print $13,$14}' | sed 's/,//g;s/….=//g' | awk '{sumrcvd=sumrcvd+$1; sumsent=sumsent+$2} END {print "rcvd: ",sumrcvd,"\n" "sent: ",sumsent}') | awk '{total=total+$2; print} END {print "total: ",total/1024/1024 "MB"}'
This ugly one-liner comes to us courtesy Chuck. Plesk calculates bandwidth statistics by literally reading the raw log files and performing math based on the byte totals noted in the log entries. This beast will run against the Plesk maillogs and give you a pretty summary of mail bandwidth:
smtp: 397852373
rcvd: 228219
sent: 211813204
total: 581.64MB
Tags: one-liners, plesk, qmail
Posted in one-liners | No Comments »
Saturday, August 29th, 2009
via Paul (lovepig.org):
netstat -npa --inet | grep :80 | sed 's/:/ /g' | awk '{print $6}' | sort | uniq -c | sort -n | while read line; do one=`echo $line | awk '{print $1}'`; two=`echo $line | awk '{print $2}'`; if [ $one -gt 100 ];
then iptables -I INPUT -s $two -j DROP; fi; done; iptables-save | grep -P '^-A INPUT' | sort | uniq -c | sort -n | while read line; do oneIp=`echo $line | awk '{print $1}'`; twoIp=`echo $line | awk '{print $5}'`; if [ $oneIp -gt 1 ]; then iptables -D INPUT -s $twoIp -j DROP; fi; done
This one-liner is quite effective when tossed into a file and run as a cronjob once per minute. Any IP with more than 100 concurrent connections — which, quite honestly, is far more than any one IP should ever have on a standard webserver — will be blocked via iptables. This script as a cronjob is extremely effective dealing with small-to-midsize DDoSes (too much traffic for Apache/whatever service to handle, but not saturating the pipe).
Tags: bash, iptables, one-liner
Posted in one-liners | 2 Comments »
Friday, June 19th, 2009
…for a list of domains, without digging through the database!
cat domains | sort |uniq |while read line ; do ls -ld /home/httpd/vhosts/$line/httpdocs |awk '{print $3}'
'domains', of course, is a text file with a list of domains hosted on the server. Can be populated in whatever way you need. Easily plugged into other Plesk utilities (such as changing Plesk FTP passwords).
Tags: one-liner, plesk
Posted in one-liners | No Comments »
Friday, June 19th, 2009
To combine two (or more) text files as individual columns in the same file, such as:
file1:
foo
foo1
foo2
foo3
file2:
foobar
foobar1
foobar2
foobar3
into:
foo foobar
foo1 foobar1
foo2 foobar2
foo3 foobar3
rather than using an ugly combination of sed and awk, you can use the `paste' command:
Tags: bash, linux, one-liner
Posted in one-liners | No Comments »
Saturday, April 11th, 2009
python -m SimpleHTTPServer
Runs in the foreground a simple, single-threaded web server on port 8000 as the current user. Logging is to stdout/stderr, and a ctrl-c will stop the server. Great for temporarily sharing a directory.
Tags: one-liner, python
Posted in one-liners, tips and tricks | No Comments »
Sunday, April 5th, 2009
Great for command-line logging into sites to pull content for whatever reason.
curl -c cookies.txt -d "username=username&password=password&action=login" -o /home/kale/outputfile.txt "http://www.domain.com/authenticated_page.php?foo=bar"
Of course, you'll have to look at the source for the target location's login page to see what variables it wants. I use it to grab a single Cacti-generated graph that is normally password protected, but I want to include a single graph on another site, so I cron'd a script to run a line similar to the above to log in and save it locally.
Tags: linux, one-liner
Posted in one-liners, tips and tricks | No Comments »
Wednesday, April 1st, 2009
perl -ne 'while (/([0-9]+\.){3}[0-9]+/g) {print "$&\n"};' file.txt
from command-line-fu
Tags: one-liner
Posted in one-liners | No Comments »
Saturday, March 28th, 2009
Spot DDoS's and the like quickly:
netstat -plan | grep :80 | awk '{print $5}' | sed 's/:.*$//' | sort | uniq -c | sort -rn |head
Tags: apache, one-liner
Posted in apache, one-liners | No Comments »
Sunday, March 22nd, 2009
for i in $(mysql -NB psa -uadmin -p`cat /etc/psa/.psa.shadow` -e 'select login from sys_users;'); do export PSA_PASSWD="$(openssl rand 6 -base64)"; /usr/local/psa/admin/bin/usermng --set-user-passwd --user=$i; echo "$i: $PSA_PASSWD" >> ftp_passwords; done
Thanks Geoff!
Tags: one-liner, plesk
Posted in one-liners | 1 Comment »
Thursday, March 19th, 2009
If you add an entry to crontab that is an interval, such as */3 (every 3 minutes), you can verify that it runs at the specified interval with a bit of awk:
cat /var/log/cron |grep cron-script |awk -F\: '{if ($2/3 == 0) print $0}' |grep -v ":00:"
This essentially checks to see that the minute field of the timestamp is divisible by three — the interval. It'll also run at 00 after the hour, not divisible by three, but expected.
Cron can "run late" at times due to high load situations, so if there are any irregularities in your intervals, you may wish to investigate deeper, looking for expensive processes that are chewing up precious cron time.
Tags: cron, linux, one-liner
Posted in one-liners | No Comments »
Monday, March 9th, 2009
Ever wish you knew what Apache was working on at any given moment, but kicking yourself because you forgot to enable a server-status directive? This snippet will help you diagnose timeouts and long-running scripts (for bad coders like myself):
for i in `ps -elf |grep http|awk '{print $4}'|sort|uniq`; do ls -la /proc/$i/cwd ; done|awk '{print $11}'|sort|uniq -c |sort -nr
Tags: apache, one-liner
Posted in apache, one-liners | No Comments »
Wednesday, March 4th, 2009
find /var/www/vhosts/*/statistics/logs -type d -exec du -sm {} \; | awk '{total+=$1} END {print total,"MB"}'
Find total sizes of files in all those logs directories
Tags: one-liner
Posted in one-liners | No Comments »
Wednesday, March 4th, 2009
To find files that do NOT contain a specific string you can do the following:
find -name "ifcfg-eth0:*" -type f ! -exec grep -q ONBOOT {} \; -exec ls {} \;
will list all files named ifcfg-eth0:* that do not contain the string ONBOOT.
You can script this up as such:
for i in `find -name "ifcfg-eth0:*" -type f ! -exec grep -q ONBOOT {} \; -exec ls {} \; |awk -F\/ '{print $2}'`; do echo ONBOOT=yes >> $i ; done
to add the required ONBOOT=yes line to the config.
Tags: linux, one-liner
Posted in one-liners | No Comments »
Wednesday, March 4th, 2009
What's eating up all your disk space?
find / -path /dev -prune -o -path /sys -prune -o -path /proc -prune -o -type f \
-size '+1024k' -printf "%s %h/%f\n" | sort -rn -k1 | head -n50 | \
awk '{ printf("%5dMB\t%s\n", $1/1048576, substr($0, index($0, " ")+1, length($0))) }'
Tags: disk space, linux, one-liner
Posted in one-liners | No Comments »
Wednesday, March 4th, 2009
The only caveat is that you have to know the table that comes after the one you're trying to extract. It's alphabetical, if you can get a list of tables, otherwise a quick search of the SQL file will get that info for you.
awk '/Table structure for table .table1./,/Table structure for table .table2./{print}' bigassdatabase.sql > table1.sql
Tags: linux, mysql, one-liner
Posted in one-liners | No Comments »
Wednesday, March 4th, 2009
Check to see if a perl module is installed:
(no output == success)
Check perl module versions:
perl -MMODULENAME -e'print "$MODULENAME::VERSION\n";'
Find out where a perl module is installed (docs, sources, etc):
perl -MExtUtils::Installed -e'$,="\n";print ExtUtils::Installed->new()->directories("MODULENAME")," "'
Tags: linux, one-liner, perl
Posted in one-liners | No Comments »
Wednesday, March 4th, 2009
Find all non-Red Hat-supplied packages:
rpm -qa --qf '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH} %{VENDOR}\n' | grep -v 'Red Hat, Inc\.' | sort
Handy for diagnosing issues where things seem a little "off".
Tags: linux, one-liner, red hat, rpm
Posted in one-liners | No Comments »
Wednesday, March 4th, 2009
ps -eo pid,tid,class,rtprio,ni,pri,pcpu,stat,wchan:14,comm,psr
The last number is the CPU the process is currently waiting on. Quite useful when used in conjunction with `top', as hitting the number 1 while in interactive mode will display the per-CPU usage. Helpful to find iowait.
Tags: cpu, iowait, linux, one-liner
Posted in one-liners | No Comments »