PHP Generate Random Passwords

This is a small script written in PHP which will help you to generate N passwords of N length in less than 1ms 😛

Make sure you have PHP installed, then copy this code to a file called randompass.php

#!/usr/bin/php -q
<?
// no i,l,o keep passwords easy
$chars = "abcdefghjkmnpqrstuvwxyz0123456789";
$passlen = (intval($argv[1])? intval($argv[1]):6);
$passnum = (intval($argv[2])? intval($argv[2]):1);

echo "Generating $passnum passwords of $passlen letters/numbers\n";
// feed the random God :P
srand(((int)((double)microtime()*1000003)));

for ($i=1;$i<=$passnum;$i++) _gen_pass();

function _gen_pass() {
  global $chars, $passlen;
  $cnt = 1;
  while ($cnt <= $passlen) {
    $myrand = rand() % 33;
    $tmp = substr($chars, $myrand, 1);
    if (rand(0,1)) $tmp = strtoupper($tmp);
    $pass = $pass . $tmp;
    $cnt++;
  }
  echo $pass . "\n";
}

?>

Now you have the file don’t forget to make it executable:

chmod +x randompass.php

How it works? See some examples:

Usage: ./randompass.php [password_length] [password_number]

# The default execution will drop 1 password of 6 letters/numbers
./randompass.php
Generating 1 passwords of 6 letters/numbers
9hBEq1

# want 5 passwords of 12 letters/numbers ?
./randompass.php 12 5
Generating 5 passwords of 12 letters/numbers
ug5Tj8fP3w26
Tn9fnSjy2PmJ
NRqC6m8J0svn
YFQ6g3WnHH8r
ds56SnQvsBEq

That’s all, use it for what you need, don’t try to break it or find stupid bugs 😛

Monitor open ports using PHP (snippet)

How can you really know if a port is open or closed? Most of the scripts around the web fail doing it’s job, not because they are wrong but because they are not doing their job as they should.

Am I on drugs? No, not now 😉 basicly, what all scripts do is:

[root@local]# telnet yourhost port

That will tell you if yourhost is open on that port but sometimes it will just hang there, why? there are many reasons why a server or service could hang (I’m not covering that part … at least not for free :P), but the only thing you really need to know is, IT HAPPENS… when? how? why? it will.

So, if you are in the middle of coding some script that let’s you monitor your servers / services without worrying about that “small particular issue”, you are in the right place, check out the code:

function check_port($ip="",$port="",$request="",$replies="") {
  if (!$ip || !$port) {
    echo "No ip/port to check";
	  return;
  }
	if (!($fp = @fsockopen($ip,$port,$junk,$junk,10)) {
	  echo "Connection DOWN!";
		return;
	}
	if (!$request && !$replies) {
	  echo "Connection UP! (open socket)";
		return;
	}
	if ($request) {
	  fputs($fp,$request,strlen($request));
		fputs($fp,"\r\n\r\n",8);
	}
	stream_set_timeout($fp,10);
	do {
		$response .= fgets($fp);
	} while (!feof($fp));
	@fclose($fp);
	$response = preg_replace("/\n|\r/","",$response);
	$result = false;
	$error = $response;
	$array_replies = explode(",",$replies);
	if (is_array($array_replies)) {
	  foreach ($array_replies as $v) {
		  if (!$v) continue;
			if (preg_match("/$v/i",$response)) {
			  $result = true;
                          break;
			}
		}
	}
	if ($result) {
	  echo "Connection UP!";
		return;
	}
	echo "Connection error: $response");
	return;
}

That’s a mess! Yes I know, it is dirty and uggly but it works. That function takes 4 arguments, $ip (server’s IP), $port (server’s port), $request and $replies (you can use comma delimited here in case you need to receive one or more answers).

How it works? Well copy that piece of code to any php file and call it this way:

// This first example will tell us if google.com is up ;)
// it sends the request "HEAD / HTTP/1.0" to the IP 72.14.207.99 on the port 80
// and expects 2 answers: "200" or "OK"
check_port("72.14.207.99",80,"HEAD / HTTP/1.0","200,OK");
// it sends the request "HEAD / HTTP/1.0" to the IP 72.14.207.99 on the port 80
// and expects 2 answers: "200" or "OK"

// another example?
check_port("148.235.52.179",110,"","\+OK");
// this one will check port 110 (pop3) on that IP, it won't send a request but
// it will sit till gets a "\+OK"

Remember, all requests and replies depends on the server’s side, be aware of that 😉