My Blog

contains PHP and other web related content. (Sometimes there are some off topic things - don't freak out!)

Archive for the ‘scripting’ Category

Block and Allow IP with iptables – simple script

Wednesday, November 25th, 2009

As most developers are lazy, I’m a huge fan of scripts. I’ve found myself lately having to add entries to iptables to block a single IP or a small subnet, so I made a quick script to make the job easier on myself.

Usage for both of these is of course really simple. Say 123.1.2.3 is the IP in question:

sudo ./blockip.sh 123.1.2.3
sudo ./allowip.sh 123.1.2.3

blockip.sh
Blocks the IP using iptables

1
2
3
4
5
6
7
#!/bin/bash
 
#blocking iptables
/sbin/iptables -A INPUT -s $1 -j DROP
 
#saving iptables
/sbin/iptables-save > /etc/sysconfig/iptables

allowip.sh
Removes the entry from iptables

1
2
3
4
5
6
7
#!/bin/bash
 
#allowing iptables
/sbin/iptables -D INPUT -s $1 -j DROP
 
#saving iptables
/sbin/iptables-save > /etc/sysconfig/iptables

When PHP’s dirname() saved the day

Thursday, April 23rd, 2009

Now, I won’t bore you with the actual details of how I came across this – lets just skip to the explanation and example:

First, even when it makes ’sense’, you should not be using relative paths in your command line PHP scripts. I am so used to writing web PHP that I fell into this bad habit.

Show Me Why dirname() is your hero

Imagine a directory structure on windows like this:

C:\DEVELOPMENT\local>dir
 Volume in drive C has no label.
 Volume Serial Number is 1122-45E1

 Directory of C:\DEVELOPMENT\local

04/23/2009  08:40 PM              .
04/23/2009  08:40 PM              ..
04/23/2009  08:37 PM              includes
04/23/2009  08:40 PM              testdirname
               0 File(s)                    0 bytes
               4 Dir(s)  67,484,995,584 bytes free

We have two files:
testdirname/script.php

1
2
require_once '../includes/include.php';
print "I've done ran, ya'll.";

includes/include.php

1
print "I'm an include!\n";

Now, let’s run the script as it is:

C:\DEVELOPMENT\local\testdirname>php script.php
I'm an include!
I've done ran, ya'll.

Not too bad – but note how we’re actually in the script.php’s working directory. What if we wanted to run it in a different directory?

C:\DEVELOPMENT\local>php testdirname\script.php

Warning: require_once(../includes/include.php): failed to open stream: No such file or directory in C:\DEVELOPMENT\local\testdirname\script.php on line 2

Well this makes sense because I programmed with that relative path.

Instead, change the require line in script.php to this:

1
require_once dirname(__FILE__) . '/../includes/include.php';

This way, it always gets the full directory of the file itself (__FILE__ constant) – and THEN you can path to the file any which way you’d like.

Let’s check the output:

C:\DEVELOPMENT\local>php testdirname\script.php
I'm an include!
I've done ran, ya'll.

Yep – I KNOW – Simple. Its embarrassing to say it bit me – but it did :)

Automatic Backup with SVN on Windows

Friday, September 12th, 2008

A while ago, I decided that I needed to have a better backup solution for my file server. After doing some research on various systems, I let my inner programmer take over – in addition to my desire to NEVER LOSE ANYTHING – and I defaulted to use SVN.

I was using a Windows machine as my file server – so I wrote some batch files. I also had SVN installed on the machine. The final touch was adding scheduled tasks.

The setup includes a computer that is always on with windows, svn command line, and 5 directories to monitor for backups.

First thing’s first, do an SVN Checkout

The very first thing I did was make an SVN checkout in all of the five parent directories. This way I can continue to use SVN add, svn commit without any other interaction. Don’t worry, we’ll use recursion!

Create the full list of backups

So, first thing’s first: Create the list of directories that need to be monitored. I made them in this txt file named ’svndirectories.txt’:

1
D:\pictures D:\storage\videos\misc D:\storage\files\art D:\storage\files\NeverAgain D:\storage\files\Therapee

Note, all of them are separated by a space. This becomes important in our next batch script.

Schedule the SVN Add

I added an SVN Add batch script at Midnight on sundays. Actually, there are two batch files. I made them separately so that I could invoke a scheduled task – but also run the “add” by hand if need be.

The first file, addsvn.bat:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@echo off

REM ------------------------------------------------------------------
REM - forces adds on all svn files
REM ------------------------------------------------------------------
 
 
:START
REM - Get file to process
set direct=%1
echo %direct%
echo.
 
:SVNADD
svn add --force %direct%\*
 
:NEXTFILE
shift
if "%1"=="" goto END
goto START
 
 
:END

That will force an add of each file passed in on the command line. Then, the batch file that I made to be ran from the scheduler will read in the folders from the text file, and run this script. Here is ’scheduled_addsvn.bat’:

1
2
3
4
5
6
7
8
@echo off
REM - This is what should be scheduled to add files to svn repos

REM - read in svndirectories.txt
for /f "tokens=*" %%a in ('type svndirectories.txt 2^>NUL') do set value=%%a

REM - call the addsvn program with all the directories
addsvn %value%

Theoretically, I could have called it with the entire line of files after it, but I wanted to call them separately to handle errors better.

After all of these have been added, lets move on…

Schedule SVN Commit

Just in case I made a huge addition of files, I let an hour pass between scheduled add and scheduled commits. Additionally, I ran the commit every day instead of every week. I figured I’d make more changes than I would make additions.

So first, read in all of the directories again and run the commit. Then, the file to schedule. These are pretty much similar, just different commands:

commitsvn.bat:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@echo off

REM ------------------------------------------------------------------
REM - commits all SVN changes
REM ------------------------------------------------------------------
 
 
:START
REM - Get file to process
set direct=%1
echo %direct%
echo.
 
:SVNCOMMIT
svn commit --message="Auto Backup" %direct%\*
 
:NEXTFILE
shift
if "%1"=="" goto END
goto START
 
 
:END

scheduled_commitsvn.bat:

1
2
3
4
5
6
7
8
@echo off
REM - This is what should be scheduled to commit files to svn repos

REM - read in svndirectories.txt
for /f "tokens=*" %%a in ('type svndirectories.txt 2^>NUL') do set value=%%a

REM - call the addsvn program with all the directories
commitsvn %value%

This has worked out pretty well for me. If you see anything I could do better, please let me know!

APD post processing wrapper

Saturday, November 17th, 2007

A while ago, I discovered the ‘joys’ of APD… and then moreso, the ‘joys’ of not being able to make heads or tails out of the output script. After digging deeper, I saw that the original directory already had some PHP scripts to parse the output. I ran those and wasn’t very impressed. Even more important, my boss wouldn’t be impressed. I needed to be able to make something that could be useful to integrate into a table (I finally used dojo to create a table…)

At any rate, I thought I might save anyone some time by posting the code here:

(more…)

Create an RSS feed of comments from myspace

Saturday, September 8th, 2007

Lately, I’ve been trying to find ways to reduce the amount of time I spend on stupid sites like myspace (nevermind the fact that the time it took to reduce this amount took me enough time to visit myspace 1x a day for another month – heh). At any rate, I’ve been using Google Reader alot more (I’m up to 180 or so feeds) and I thought: Why don’t I make an RSS feed o my comments – then I don’t have to go back to the site when someone sends me a comment. (Mind you, myspace does send you an e-mail when you receive a comment, but doesn’t include the content. JEMDiary does, however ;) ) I searched the internet and found a few sites that are doing that for a service, and one guy who was giving away a regular expression. So, I took his idea and wrote my own php script for cron. Check it out here:

(more…)

Live Combined Error Reporting for Apache and PHP during Development

Friday, July 20th, 2007

So many times during development, I’ve missed little PHP errors because they were 1) on a processing page that was redirected or 2) output inside of a html tag – and rendered invisible. From time to time, I have to go back to my file system and check the php error log to see what happened. The first step to solving this was implimenting a custom error handler – which we did at (“the triangle”). But I’m torn on this: should the error handler script function the exact same during development as it does in production, or should we write two different error handlers. To keep the code as simple as possible and allow for scenario regeneration, I opted to have the error handler work the exact same way in development. Some might disagree, but thats not the point here. The issue was that I needed to watch the error log closer (I’m notoriously bad at not checking errors – see my previous post about error reporting…).

Another thing I knew would be nice to see would be the apache error log. As I’m not combining my error logs with php, I don’t often check the apache one. However, local mistakes can cause errors on the production server too.

Luckily, I was able to find a utility that made life easier – and of course – integrates into eclipse. Lets configure:

(more…)

When your linkd causes you problems, you must convertd it!

Friday, July 6th, 2007

For the setup I use at (“the triangle”), I have alot of filesystem links – and these are made on win32 with the linkd.exe command (the version I got is from the windows 2000 resource cd). When I start a new project, I pass into my script which directories I’d like to make and checkout code into, and which I’d rather just linkd to. Well, every once in a while a link’d folder needs to be a real folder. So, since I’m a lazy programmer, I made a script called ‘convertd’ which will unlink the folder and then make the folder. Chalk one up to efficiency by batch programming? Lets see:

(more…)

  • twitter loader

Follow me on twitter: @aaronsaray

The views on this website are my own and do not reflect the opinions of my employer or clients.
Creative Commons License Home | Open Source | Book | Music | Art | Bio | Resume | Contact
My Baby