Posts Tagged “Bash Script”

Plesk has a lot of short comings, in my opinion. One of them is they way Plesk stores passwords. I will not go into the best practices for passwords, but instead offer this one-liner that will allow you to change all the FTP passwords for Plesk in one fell swoop. This is particularly useful if you think one or more passwords have been compromised.

# 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

If for some odd reason you have a password for a user that is set to NULL, this will not update it. You shouldn’t have any, unless you are editing the psa database directly.

Comments 1 Comment »

Given a common log format, the following one-liner calculates the bandwidth used per IP from the Apache logs. and does it pretty darn quickly – under 2 seconds for a 200-MB file.

# awk ‘{array[$1]+=$10} END { for ( item in array ) print
array[item]/1048576 “MB”, item }’ access_log.processed | sort -n

Omit the -n argument to sort if you want the largest numbers to appear at the top of the output.

It generates output that looks like the following:


Comments No Comments »

Here is a nice little tool that I found in my travels, but has since become obscure due to the creator moving on to other endeavours. So what is a snapshot script? It is a bash script that runs every 10 minutes or so and records which processes are running. In the event of a system crash, you can look back at these logs and see what was running just prior to the crash and hopefully gain some insight as to what is happening with your system.

Since the original location of the script was here:

I have made a copy of it here:

Here is an example of what real-world use might look like:

[root@8bitpipe ~]# wget
Connecting to||:80… connected.
HTTP request sent, awaiting response… 200 OK
Length: 3160 (3.1K) [application/x-sh]
Saving to:

100%[=======================================>] 3,160 –.-K/s in 0s

19:52:15 (1.51 MB/s) – saved [3160/3160]

[root@8bitpipe ~]# chmod +x
[root@8bitpipe ~]# mv /usr/local/bin
[root@8bitpipe ~]# crontab -e

*/10 * * * * /usr/local/bin/
crontab: installing new crontab

This will set the script to run once per 10 minutes, and archive the last hours worth of snapshots. The current snapshots will be stored in ~/.snapshot.cur

After a crash, this directory will be moved to ~/ (the date will be the first execution of the script after the server comes back up), and a symlink will be created from ~/.snapshot.last to the most recent set of snapshots.

As a note, if you need to change the commands executed by the script, just edit the section at the bottom (it’s marked within the script), and make sure any output is redirected to ~/.snapshot.cur/snapshot.1

This script is also useful if you need to back track and see what is utilizing resources even if the server isn’t crashing.

Happy hunting.

Comments 1 Comment »

My buddy came up with this one-liner for checking the access log for the amount of data being served by a website (or multiple if their all dumping to the same log)

awk ‘{print $10,$7}’ /path/to/access_log | sort | uniq -c | sort -n | awk ‘{ this_bytes_total = $1 * $2; print ((this_bytes_total/1024)/1024) ” MiB total: ” $2 ” bytes sent ” $1 ” times for ” $3 }’ | sort -n > results.txt

Let me know what you think.

Comments No Comments »