PHP and $argv

Posted by | Posted in Code, Command-Line, Functions, Guides, Linux, OS X, PHP, Server, Ubuntu | Posted on 31-01-2011

I’ve been adding $argv functionality to all of my PHP maintenance scripts. I am doing this, so I don’t have to edit a variable each time I want to execute it.

A PHP $argv example.

 
#!/usr/bin/php
< ? PHP
 
#	test.php
#	demonstration of $argv
#	Dan McCoy
#	January 31 2011
 
$command = $argv[1];
 
$GLOBALS['script'] = basename($argv[0]);
$GLOBALS['ver'] = "1.0";
$GLOBALS['file'] = $argv[2];
 
function help() {
	echo "Help for ". $GLOBALS['script'] ."\n";
 
	echo "-h \t Help prompt \n";
	echo "-v \t Version ". $GLOBALS['ver'] ."\n";
 
	echo "-g \t Grab file from X \n";
	echo "-f \t Reformat data from X \n";
	echo "-l \t Load data from X \n";
 
}// end help
 
function grab(){
	@exec('/opt/local/bin/wget http://webserver.com/page.php -O '. $GLOBALS['file'],$retval);
	return "Grab completed ". $retval[0] ." \n";
}// end grab
 
function reformat() {
	$fp = $GLOBALS['file'];
	$file = fopen($fp,'r');
	$data = fread($file,filesize($fp));
	fclose($file);
 
	$data = str_replace("<br /><br />","\n\n",$data);
	$data = str_replace("<br />","\n",$data);
	$data = strip_tags($data);
	$data = str_replace("\t","",$data);
 
	$fp = $GLOBALS['file'];
	$file = fopen($fp,'w');
	fwrite($file,$data);
	fclose($file);
 
	return "Reformating completed \n";
}// end reformat
 
function load() {
        $fp = $GLOBALS['file'];
        $file = fopen($fp,'r');
        $data = fread($file,filesize($fp));
        fclose($file);
	return $data;
}
 
function delete() {
	$ret = @unlink($GLOBALS['file']);
 
	if ($ret==TRUE){
		return "File has been deleted \n";
	}else{
		return "There was an ERROR deleting ". $GLOBALS['file']."\n";        
	}
}// end delete
 
function switch_default() {
	echo "Command not found \n";
	help();
}
 
switch($command) {
	case "-h":
	        echo help();
	break;
 
	case "-g":
        	echo grab();
	break;
 
	case "-f":
		echo reformat();
	break;
 
	case "-l":
		echo load();
	break;
 
	case "-d":
		echo delete();
	break;
 
	default:
		switch_default();
	break;	
}// end switch
 
? >

Executing more then 1 command at once

Posted by | Posted in Bash, Code, Command-Line, Cron, Linux, OS X, Server, Ubuntu | Posted on 30-01-2011

Many times, I have to execute sequential commands inorder to complete a task. Below is an example of how to do this.

/path/to/command argv1 && /path/to/command argv2

I normally use this sequential order when pulling raw data from a source and then reformatting it for my needs.

15 * * * * /usr/local/bin/pullsource grab && /usr/local/bin/pullsource reformat

Change the default crontab editor – Bash

Posted by | Posted in Bash, Code, Command-Line, Cron, Guides, Linux, OS X | Posted on 30-01-2011

You can change your default crontab (crontab -e) by setting the EDITOR environment variable in BASH:

hostname# which vi
/usr/bin/vi
hostname# export EDITOR=/usr/bin/vi
hostname# set | grep EDITOR
EDITOR=/usr/bin/vi
_=EDITOR
hostname#

Applescript – folder and file counter

Posted by | Posted in Applescript, Code, OS X | Posted on 16-09-2010

set theFolder to (choose folder)
set thePath to POSIX path of theFolder
set fileCount to 0
set folderCount to 0
tell application "Finder"
	set theFileList to every item in folder theFolder
	repeat with x in theFileList
		if not character 1 of ((name of x) as string) is equal to "." then
			if kind of x is equal to "Folder" then -- "folder" depends fromĀ  the finder language 
				set folderCount to folderCount + 1
			else
				set fileCount to fileCount + 1
			end if
		end if
	end repeat
end tell
 
display dialog "There are " & folderCount & " folders andĀ  " & fileCount & " files in " & thePath

Applescript – Change folder color based on size

Posted by | Posted in Applescript, Code, OS X | Posted on 16-09-2010

property theFile : missing value
property theFolder : "test"
property thePosixPath : "/Users/user/Desktop/"
property theAliasPath : "Users:USER:Desktop:"
property theSize : missing value
 
tell application "Finder"
	set theSize to do shell script "du -s -k " & thePosixPath & theFolder & " |awk '{print $1}'"
	if theSize > 700000 then
		set label index of (folder theAliasPath & theFolder) to 2
	end if
end tell

Randomly Choose A Buddies’ Status Message As Your Own – OS X – Applescript

Posted by | Posted in Applescript, Code, OS X | Posted on 10-11-2009

tell application "iChat"
tell application "iChat" to set messagelist to get status message of every buddy
where status is not offline
set msglistcount to count messagelist
set theMessageList to {}
repeat with x from 1 to msglistcount
if item x of messagelist is not "" then
copy item x of messagelist to end of theMessageList
end if
msglistcount = msglistcount - 1
end repeat
set newStatus to some item of theMessageList
set status message to "i'm in your internets stealing your status messages : " & newStatus
end tell

Using PHP to return an IP address

Posted by | Posted in Code, Command-Line, Functions, OS X, PHP | Posted on 19-07-2009

After I’ve determine which interfaces are active on my computer(s), I can use this script to report back the IP address of said interface.

# input String ("en0")
# output String IP
function get_network_interface_ip($interface) {
  @exec('/usr/sbin/ipconfig getifaddr '. $interface, $retval);
 return $retval[0];
}// end get_network_iterface_ip
echo get_network_interface_ip($interface);

Using PHP to report an active network interface

Posted by | Posted in Code, Command-Line, Functions, OS X, PHP | Posted on 19-07-2009

I use this script, after I determine what available interfaces the computer has, to report which interfaces are active.

# input String ("en0")
# output "NULL or "Active" 
function check_interface_activity($interface){
  @exec('ifconfig| awk \'/flags=|media|inet / {if (substr($2, 1, 6) == "flags=") printf("\n%s ", $1);
  else if ($1 == "inet") printf("%s ", $2);
  else if ($1 == "media:") printf(substr($0, 9))}\' | awk \'$1 ~ /^'.$interface.'/\' | awk \'{print $7}\'',$retval);
 return $retval[0];
} // end check_interface
echo check_interface($interface);

Using PHP to gather all active network interfaces

Posted by | Posted in Code, Command-Line, Functions, OS X, PHP | Posted on 19-07-2009

I use this script to gather all Ethernet based network activity to determine which network port is active and then determine what the hardware address and IP address are.

# input NONE
# output String ("en0, en1")
 
function get_all_active_network_interfaces() {
  $inclusion = array(en0,en1);
  @exec('/sbin/ifconfig -lu', $retval);
  $interfaces = explode(" ",$retval[0]);
  $ret=NULL;
  foreach($interfaces as $interface){
     if (in_array($interface,$inclusion)){
       $ret.=$interface ." \n \r";
     } // end if
  }// end foreach
return $ret;
} // end get_all_active_network_interfaces
echo get_all_active_network_interfaces();

This script is similar to the above script, but doesn’t include the constraints of the inclusion list.

# input NONE
# output String ("en0, en1, etc") 
function get_all_active_network_interfaces() {
  @exec('/sbin/ifconfig -lu', $retval);
  $interfaces = explode(" ",$retval[0]);
  $ret=NULL;
    foreach($interfaces as $interface){
      $ret .= $interface ." \n \r";
    }// end foreach
return $ret;
} // end get_all_active_network_interfaces
 
echo get_all_active_network_interfaces();

Using PHP to Connect to a MySQL server

Posted by | Posted in Code, MySQL, PHP | Posted on 18-07-2009

This is how I normally link PHP to my MySQL servers. I use this code normally for my shell scripting, but it also works for websites.

$mysql_server = "SERVER_ADDRESS";
$mysql_user = "SERVER_USER";
$mysql_password = "SERVER_PASSWORD";
$mysql_database = 'SERVER_DATABASE';
$mysql_table = 'SERVER_TABLE';
$mysql_connection = @mysql_connect($mysql_server,$mysql_user,$mysql_password);
 
if (!$mysql_connection)
  DIE ('ERROR 10: No Connection to '.$mysql_server.' '. mysql_error() ."\n");
 
$mysql_database_connection = @mysql_select_db($mysql_database,$mysql_connection);
 
if (!$mysql_database_connection)
  DIE ('ERROR 30 : Could not select database '. $mysql_database .' '. mysql_error() ."\n");
 
echo "MySQL : Connection Active \n";

Join the mailing list

Check your email and confirm the subscription