www.pokeroconnor.com

Using CURL to access certain IP of a Domain

July23

Something I needed to do recently was get the contents of a certain url of a certain domain name, for each of the IP addresses associated with that domain name. In other words, there was a domain we shall call www.test-domain.com, which was hosted on 5 different servers, with IPs 1.2.3.100 through 1.2.3.104. I wanted to automate the getting of the contents of www.test-domain.com/file-name.php on each of these servers. I could set my hosts file of course, and point at each server, blah blah blah, but this had to be automated, for sanity.

The php curl library is fully awesome, but personally I find this page to be the most useful. There are surprisingly few examples of this anywhere on the web…just try googling this topic and you get nothing at all relevant!

The solution is simple enough, but believe me it won’t be easy to find on the web – thanks to a certain work colleague for the expert advice on this! Basically you need to inject a header using CURLOPT_HTTPHEADER, and then use CURLOPT_URL with the actual IP address.

For example:

$ch = curl_init();
$headers = array("Host: www.test-domain.com");

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "http://1.2.3.100/file-name.php");

curl_exec($ch);

That’s the engine of it, a very neat little solution!

//

Tarball Options and Uses

July20

Here’s a few tidbits on tarballs that are quite useful. In case you’re wondering how to create a tarball in the first place, do this:

tar -zcvf my_first_tarball.tar.gz /home/my_code

where my_first_tarball.tar.gz is the name of the tarball that will be created, and /home/my_code the dir that is being compressed.

But you really need to know how to exclude files and/or directories when creating tarballs. For example, say you want to backup your codebase, but exclude all the SVN crap, then this is essential.

So you simply add –exclude options, like so:

tar -zcvf /my_first_tarball.tar.gz –exclude=’svn’ –exclude=’*.svn’ /home/my_code

This blog has a great example too of how you can list all your exclude types in a file, and use the -X switch to exclude them all – very neat.

One of the most common uses of tarballs is of course backing stuff up. So here’s a really useful script, running on a cron, to backup your stuff. I won’t reproduce it here, just check out the link.

To finish off, to expand or decompress your tarball, do:

tar -zxvf my_first_tarball.tar.gz

Note: you are swapping the c (create) for an x (expand), in the options.

//