Browse Category: one-liners

Who’s connecting to Apache?

Spot DDoS’s and the like quickly:

[code lang=”bash”] netstat -plan | grep :80 | awk ‘{print $5}’ | sed ‘s/:.*$//’ | sort | uniq -c | sort -rn |head [/code]

Change all of Plesk’s FTP passwords to random

[code lang=”bash”]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 [/code]

Thanks Geoff!

Make sure your crons run on time

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:

[code lang=”bash”]cat /var/log/cron |grep cron-script |awk -F\: ‘{if ($2/3 == 0) print $0}’ |grep -v “:00:”[/code]

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.

What is Apache doing?

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):

[code lang=”bash”]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 [/code]

Find total file sizes

[code lang=”bash”]find /var/www/vhosts/*/statistics/logs -type d -exec du -sm {} \; | awk ‘{total+=$1} END {print total,”MB”}’ [/code]
Find total sizes of files in all those logs directories

Find files that do not contain a string

To find files that do NOT contain a specific string you can do the following:
[code lang=”bash”]find -name “ifcfg-eth0:*” -type f ! -exec grep -q ONBOOT {} \; -exec ls {} \;[/code]

will list all files named ifcfg-eth0:* that do not contain the string ONBOOT.

You can script this up as such:

[code lang=”bash”]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[/code]

to add the required ONBOOT=yes line to the config.

Find the 50 largest files

What’s eating up all your disk space?
[code lang=”bash”]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))) }'[/code]

Extract a single table from a sql dump

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.
[code lang=”bash”]awk ‘/Table structure for table .table1./,/Table structure for table .table2./{print}’ bigassdatabase.sql > table1.sql[/code]

Perl module infos

Check to see if a perl module is installed:
[code lang=”perl”]perl -MMODULENAME -e1[/code]
(no output == success)

Check perl module versions:
[code lang=”perl”]perl -MMODULENAME -e’print “$MODULENAME::VERSION\n”;'[/code]

Find out where a perl module is installed (docs, sources, etc):
[code lang=”perl”]perl -MExtUtils::Installed -e’$,=”\n”;print ExtUtils::Installed->new()->directories(“MODULENAME”),” “‘[/code]

Silly RPM tricks

Find all non-Red Hat-supplied packages:
[code lang=”bash”]rpm -qa –qf ‘%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH} %{VENDOR}\n’ | grep -v ‘Red Hat, Inc\.’ | sort[/code]

Handy for diagnosing issues where things seem a little “off”.

CPU affinity-aware `ps’

[code lang=”bash”]ps -eo pid,tid,class,rtprio,ni,pri,pcpu,stat,wchan:14,comm,psr[/code]

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.

  • 1
  • 2