Converting Uppercase to Lowercase (and vice-versus)
Jul 6

I love bash and scripting!!!  There’s almost nothing you can’t do with a shell script that would take me forever if I had to write an app to do the same.  So here’s another quick tip.  To convert text in a file from uppercase to lowercase use the following:

cat FILENAME | tr “[:upper:]” “[:lower:]“

Or to go from lowercase to uppercase:

cat FILENAME | tr “[:lower:]” “[:upper:]“

Using seq to generate a list of numbers
Jun 30

Another quick hint.  While working on a server I needed to bring up a whole lot of IP addresses (200 to be exact).  If I really wanted to I could bring them all up like this:

ifconfig eth0:2 1.1.1.2 netmask 255.255.255.0 up
ifconfig eth0:3 1.1.1.3 netmask 255.255.255.0 up
and so on to…..
ifconfig eth0:200 1.1.1.200 netmask 255.255.255.0 up

Well, I’m always looking for an easier way, so I turned to my friend BASH and a tool called SEQ.  SEQ will give you a sequence of numbers.  For example if you just wanted 10 numbers you could do the following:

[matt@localhost ~]$ seq 5
1
2
3
4
5

So for this task I needed to bring up IP addresses from 2 through 254.  Running “seq 2 254″ will give me a sequence from 2 to 254, I need more than just to have a list of numbers, I actually need to use them, so here’s the syntax I used to use the numbers to bring up each of the interfaces

for i in $(seq 2 254)
do
ifconfig eth0:$i 1.1.1.$i netmask 255.255.255.0 up
done

Obviously in the above example, you would substitute the sequence you want to use and the IP subnet you want to use.  Also, this syntax would put 1.1.1.2 on sub-interface eth0:2, 1.1.1.3 on sub-interface eth0:3, etc.

Curl requests by binding to different IP address
Jun 29

From time to time I need to use Curl to test websites and sometimes I need to make the request come from a different IP address, other than the server’s default IP.  Note, the IP address you use must be active on your server.  To do this, use the following syntax:

curl --interface xxx.xxx.xxx.xxx -s http://www.sysadminvalley.com

Make sure that you substitute xxx.xxx.xxx.xxx with the IP address you want to use.

Setting up a Maintenance page with Apache and Cookies
May 16

There are times when you want to make changes to your website and you do not want your visitors to see the site before you have finished deploying and testing the website.  Here is an example using Apache, mod_rewrite and a cookie set by a PHP page.

First, create your maintenance webpage called maintenance.html.  Second, create a file called set_cookie.php with the following contents

<?php
setcookie(“testing”, “testing”, time()+36000);  /* expire in 600 minutes */
?>

Next, create a file called .htaccess in your main web directory with the following contents

RewriteEngine on
RewriteCond %{HTTP_COOKIE} !testing
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteCond %{REQUEST_URI} !/set_cookie.php$
RewriteCond %{REQUEST_URI} !/logo\.jpg$
RewriteRule ^(.*) /maintenance.html [NC,L]

Lastly, in your web browser, go to http://www.yourdomain.com/set_cookie.php.  From that point on, you will be able to browse your website, but your visitors will be redirected to your maintenance.html webpage

Creating a self-signed SSL Certificate
Feb 17

For this you will need the openssl package.  First we want to start by generating a private key.

root@localhost# openssl genrsa -out www.mydomain.com.key 1024
Generating RSA private key, 1024 bit long modulus
………….++++++
………………..++++++
e is 65537 (0×10001)

Then, we need to generate the certificate request and fill in the appropriate information.  Make sure that the “Common Name” matches the domain you want to protect via SSL, so if you domain was www.mydomain.com, use that.  If you wanted to protect mydomain.com (without the www.) then use that.

root@localhost# openssl req -new -key www.mydomain.com.key -out www.mydomain.com.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.’, the field will be left blank.
—–
Country Name (2 letter code) [GB]:
State or Province Name (full name) [Berkshire]:
Locality Name (eg, city) [Newbury]:
Organization Name (eg, company) [My Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server’s hostname) []:www.mydomain.com
Email Address []:email@mydomain.com

Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

Next, generate the self-signed certificate. You can specify the number of days the cert is valid for.

root@localhost# openssl x509 -req -days 365 -in www.mydomain.com.csr -signkey www.mydomain.com.key -out www.mydomain.com.crt
Signature ok
subject=/C=/ST=/L=/O=/CN=www.mydomain.com/emailAddress=email@mydomain.com
Getting Private key

Next, move the certificate and keyfile into apache’s SSL directory.

mv www.mydomain.com.key /etc/httpd/conf/ssl.key/
mv www.mydomain.com.crt /etc/httpd/conf/ssl.crt/

Finally, we configure our SSL virtual host in Apache.  The simplest way to do this is to copy the virtual host for the site you want to make SSL, then add/change the following bits.

<VirtualHost 192.168.1.100:443>

SSLEngine on
SSLCertificateFile /etc/httpd/conf/ssl.key/www.mydomain.com.key
SSLCertificateKeyFile /etc/httpd/conf/ssl.crt/www.mydomain.com.crt

</VirtualHost>

Search and Replace for multiple files
Feb 2

Here is a quick tip to help doing search and replace in multiple files.    You may need to change to fit your needs.

for file in `ls *.php`
do
sed -e ‘s/Copyright 2008/Copyright 2009/’ “$file” > tmp_file
mv -f tmp_file “$file”
done

What this does is get’s a list of all php files in the current directory, puts them in a loop with a variable called FILE, then does a sed search and replace calling the new file tmp_file and moving the temp file back in place.  If you wanted to do it for all files including in subdirectories, you could substitute:

ls *.php

with:

find ./ -name “*.php”

And now for something fun
Jan 30

Okay, so this isn’t sysadmin or networking related.  But who says everything has to be.  Besides this is just plain cool.  A company called Cyth Systems has developed a Guitar Hero robot called Cythbot.  Check out this video.

Another must have MySQL tool
Jan 6

As you probably gather I do quite a bit of MySQL work, in fact over the years I’ve sort of become the MySQL DBA where I work.  From time to time we’ve had issues where our MySQL server just aren’t performing as well as they could.  Sure we could hire an expensive consultant, but I’ve come across this tool called MySQLTuner which gives me the ability to see what settings I should tune.

It’s a simple perl script and to use it is really easy.  First, download the script to your linux server, change it to be executable, then run it

wget http://mysqltuner.com/mysqltuner.pl
chmod 0700 mysqltuner.pl
./mysqltuner.pl

You’ll get a great one page output which gives you some great detail about what’s going on, and what settings can be used in your my.cnf file.  One word of caution, make sure you backup your my.cnf before making changes, and you should also spend some time learning what the values mean.

Useful MySQL Queries
Jan 5

I recently had the need to do some troubleshooting on some of my larger MySQL databases.  It seems one of our developers turned off innodb_file_per_table = 1 in the /etc/my.cnf.  This feature tells MySQL to for every Innodb table to create a separate file, if it’s not turned on, it stores all the tables in 1 large file.  This can be extremely painful by itself to maintain as it grows.  Anyway, I had the need to do a little research on database and table sizes and came across a post by Peter from MySQLPerformanceBlog.com

In this first example, I was able to see which Storage Engines where using the most disk space, the number of tables, etc:

  1. mysql> SELECT engine,
  2. -> count(*) TABLES,
  3. -> concat(round(sum(table_rows)/1000000,2),’M') rows,
  4. -> concat(round(sum(data_length)/(1024*1024*1024),2),’G') DATA,
  5. -> concat(round(sum(index_length)/(1024*1024*1024),2),’G') idx,
  6. -> concat(round(sum(data_length+index_length)/(1024*1024*1024),2),’G') total_size,
  7. -> round(sum(index_length)/sum(data_length),2) idxfrac
  8. -> FROM information_schema.TABLES
  9. -> GROUP BY engine
  10. -> ORDER BY sum(data_length+index_length) DESC LIMIT 10;
  11. +————+——–+———+———+——–+————+———+
    | engine     | TABLES | rows    | DATA    | idx    | total_size | idxfrac |
    +————+——–+———+———+——–+————+———+
    | MyISAM     |   1243 | 941.06M | 244.09G | 4.37G  | 248.47G    |    0.02 |
    | InnoDB     |    280 | 682.82M | 63.91G  | 32.49G | 96.40G     |    0.51 |
    | MRG_MyISAM |      1 | 13.66M  | 6.01G   | 0.00G  | 6.01G      |    0.00 |
    | MEMORY     |     14 | 0.00M   | 0.00G   | 0.00G  | 0.00G      |    NULL |
    +————+——–+———+———+——–+————+———+
    4 rows IN SET (14.02 sec)

In this next example, I’m able to see which databases are consuming the most:

  1. SELECT
  2. -> count(*) TABLES,
  3. -> table_schema,concat(round(sum(table_rows)/1000000,2),’M') rows,
  4. -> concat(round(sum(data_length)/(1024*1024*1024),2),’G') DATA,
  5. -> concat(round(sum(index_length)/(1024*1024*1024),2),’G') idx,
  6. -> concat(round(sum(data_length+index_length)/(1024*1024*1024),2),’G') total_size,
  7. -> round(sum(index_length)/sum(data_length),2) idxfrac
  8. -> FROM information_schema.TABLES
  9. -> GROUP BY table_schema
  10. -> ORDER BY sum(data_length+index_length) DESC LIMIT 10;
  11. +——–+——————–+——-+——-+——-+————+———+
    | TABLES | table_schema       | rows  | DATA  | idx   | total_size | idxfrac |
    +——–+——————–+——-+——-+——-+————+———+
    |     48 | cacti              | 0.01M | 0.00G | 0.00G | 0.00G      |    0.72 |
    |     17 | mysql              | 0.00M | 0.00G | 0.00G | 0.00G      |    0.18 |
    |      4 | pdns               | 0.00M | 0.00G | 0.00G | 0.00G      |    1.00 |
    |      2 | test               | 0.00M | 0.00G | 0.00G | 0.00G      |    0.12 |
    |     16 | information_schema | NULL  | 0.00G | 0.00G | 0.00G      |    NULL |
    +——–+——————–+——-+——-+——-+————+———+
    5 rows IN SET (0.32 sec)

Useful tricks with linux date
Dec 16

Have you ever spent countless time writing a shell script trying to say you want all “data” between this date and that date. I’ve had numerous scripts like this.  Well, recently I got tired of doing this so I re-read the man page for “date” and came across this helpful time.

Normally when I use date I format it how I like, such as 2008-12-16.  To do this I could do

date +%Y-%m-%d

Or to do 12-16-2008

date +%m-%d-%Y

But here is the real gem.  I needed to find out what date it was 6 months ago, so this is what I did.

date +%Y-%m-%d –date=’6 months ago’

And it gave me the correct date of 2008-06-16

« Previous Entries Next Entries »