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:


1.00981MB 66.249.71.109
1.13482MB 129.188.33.27
1.13523MB 95.108.150.241
1.1675MB 151.151.109.22
1.19112MB 12.213.224.36
1.27849MB 82.44.133.226
1.3791MB 66.249.71.39
1.45494MB 38.105.83.11
1.4975MB 207.200.116.6
1.68302MB 88.131.106.30


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:
http://partytime.wackyfunster.com/leet/snapshot.sh.

I have made a copy of it here:
http://www.8bitpipe.com/tools/snapshot.sh

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


[root@8bitpipe ~]# wget www.8bitpipe.com/tools/snapshot.sh
–19:52:15– http://www.8bitpipe.com/tools/snapshot.sh
Resolving www.8bitpipe.com… 98.129.220.88
Connecting to www.8bitpipe.com|98.129.220.88|:80… connected.
HTTP request sent, awaiting response… 200 OK
Length: 3160 (3.1K) [application/x-sh]
Saving to: snapshot.sh


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


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


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


*/10 * * * * /usr/local/bin/snapshot.sh
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 ~/.snapshot.date (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 »