Code snippet: Restrict access to a page in PHP based on remote hosts IP’s

<?php
//get the remote host IP
$szRemoteIP = $_SERVER['REMOTE_ADDR'];
//this is the array holding the allowed IP's list;
$arrAllowedIPs = array("192.168.0.1",
                       "192.168.2.3",
                       "192.168.63.22",
                       "127.0.0.1",
                       "10.10.192.102");
if (!in_array($szRemoteIP, $arrAllowedIPs))
{
    //if IP's is not in the list, deny access
    echo "You don't have the permissions to access this area from $szRemoteIP";
    exit(0);
}
//page code goes here
?>
Advertisements

Mac Address Javascript Validation in linux system

function isValidMacAddress() 
 {
	 macAdd=document.getElementById('mac').value;
  var RegExPattern = /^[0-9a-fA-F:]+$/;
 
  if (!(macAdd.match(RegExPattern)) || macAdd.length != 17) 
  {
   alert("Invalid Media Access Control Address");
  }
  else
  {
   alert(macAdd + " is a valid Media Access Control Address");
  }
 }
 

PHP Mac Address Formatting

I have been working on a small php app to perform a network scan and fill a database with the discovered information. The issue is that, depending on what you perform the scan with, the MAC addresses are in a number of different formats. This PHP functions validates a MAC and returns false if its not valid or a variety of formats depending on your needs! If anyone has any thoughts on improving this function feel free to let me know and I will post the updates.

function format_mac($mac, $format='linux'){

	$mac = preg_replace("/[^a-fA-F0-9]/",'',$mac);

	$mac = (str_split($mac,2));
	if(!(count($mac) == 6))
		return false;

	if($format == 'linux' || $format == ':'){
		return $mac[0]. ":" . $mac[1] . ":" . $mac[2]. ":" . $mac[3] . ":" . $mac[4]. ":" . $mac[5];
	}elseif($format == 'windows' || $format == '-'){
		return $mac[0]. "-" . $mac[1] . "-" . $mac[2]. "-" . $mac[3] . "-" . $mac[4]. "-" . $mac[5];
	}elseif($format == 'cisco'){
		return $mac[0] . $mac[1] . ":" . $mac[2] . $mac[3] . ":" . $mac[4] . $mac[5];
	}else{
		return $mac[0]. "$format" . $mac[1] . "$format" . $mac[2]. "$format"
             . $mac[3] . "$format" . $mac[4]. "$format" . $mac[5];
	}	
}

//some usage examples
$rmac = "00:12:23:56:78:90";

echo format_mac($rmac,'cisco');
//0012:2356:7890

echo format_mac($rmac,'-');
//00-12-23-56-78-90

echo format_mac($rmac,'linux');
//00:12:23:56:78:90

echo format_mac($rmac,'?');
//00?12?23?56?78?90

Wake-On-Lan (WOL) is not working when machine is in hibernate or in standby mode.

Problem

Wake-On-Lan (WOL) seems to be not working when a machine is hibernated, in standby or in sleep mode. Customer noticed that their computers fail to startup when Wake-On-LAN task is run after computers go into hibernation. However, Wake-On-LAN works when the computers are shutdown completely.

Environment

Notification Server 6.0.6074 SP3
Windows XP

Cause

In order for WOL to work when computer goes on Standby & Hibernation mode, NIC card Power Management configuration is required. By Default Windows turn this feature off to save power.
See Microsoft KB article “How to disable power management for a network adapter when you deploy Windows XP” (http://support.microsoft.com/kb/837058)

Solution

You will need to allow the computer to turn on rather than turn off this device to save power.
Use the information provided on Microsoft KB article “How to disable power management for a network adapter when you deploy Windows XP” (http://support.microsoft.com/kb/837058)Basically to use Registry Editor to disable the “Allow the computer to turn off this device to save power” network adapter setting for a single computer, follow these steps:1. Click Start, click Run, type regedit in the Open box, and then click OK.
2. Locate and then click the following registry subkey:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\DeviceNumber

Note: DeviceNumber is the network adapter number. If a single network adapter is installed on the computer, the DeviceNumber is 0001.

3. Click PnPCapabilities.
4. On the Edit menu, click Modify.
5. In the Value data box, type 38, and then click OK.

Note By default, a value of 20 indicates that power management of the network adapter is enabled. A value of 38 will prevent Windows XP from turning off the network adapter or let the network adapter wake the computer from standby.

6. On the File menu, click Exit.

Access Your Computer Anytime and Save Energy with Wake-on-LAN

You want access to your home computer wherever you are, whatever you’re doing, whether that’s via a remote desktop connection, SSH, FTP, web interface, or any other remote access you’ve set up. The catch is, you don’t like throwing money away to an always-on system. Luckily you can have your digital cake and eat it, too, and today I’ll show you how to boot and shut down your system remotely so that it’s ready for you when you need it and it’s not wasting energy when you don’t.

The cornerstone of this setup is a feature available to almost all newer computers known as Wake-on-LAN (or WOL), which—as the name suggests—turns on your PC through your local network. Wake-on-LAN is a breeze to set up and use on your local network, and with a little legwork you can set it up so you can wake your computer away from your home network, as well.

First I’ll show you how to enable Wake-on-LAN on your computer, starting with enabling the feature in the BIOS and finishing by choosing the correct system settings. Then I’ll show you several methods you can use to wake your computer using this feature, from waking your computer through your browser to different applications made specifically for this purpose.

NOTE: Wake-on-LAN only works with wired network connections.

Set Up Wake-on-LAN in Your BIOS and Operating System

The Wake-on-LAN feature can work in a couple of ways. First, it can boot your system from a completely shutdown state. Second, Wake-on-LAN can restore your system from a hibernated (Windows) or sleeping state (Mac). If you’re working in Windows, you may need to tweak your BIOS (the firmware your computer’s motherboard runs before it boots your operating system) before you start using Wake-on-LAN. Once your BIOS is set, you need to find and adjust a few system settings to allow you to wake up your computer using the Wake-on-LAN feature.

Enable Wake-on-LAN in Your BIOS

Access Your Computer Anytime and Save Energy with Wake-on-LANTo access your BIOS, restart your computer and press and hold the Delete key (or whatever key your BIOS prompts you to hold) to enter the BIOS setup. Once you’re in the BIOS, head to the Power management section and look for a Wake-on-LAN setting. If you find one, go ahead and make sure it’s enabled, then save and exit your BIOS and start up your computer. Not all BIOS will have a straight-up Wake-on-LAN option, and on some boards you may have to enable a “Power On By PCI Devices” setting. It varies from board to board, so a little trial and error may be in order.

Now you’re ready to enable WOL in your operating system.

Continue………………………….

 

 

continue……part3 "Access Your Computer Anytime and Save Energy with Wake-on-LAN"

Wake Your Computer from Anywhere with Your Web Browser

The easiest way to issue a Wake-on-LAN command from outside your local network is through your web browser, and the absolutely simplest way to do that is through your router.

If you’ve followed either of our methods for turning your cheap-o $60 router into a super-charged $600 router with either Tomato, DD-WRT, or even HyperWRT (which we haven’t covered), then you’ve got all the tools you need to turn on your computer from anywhere with an internet connection. Since I’m currently running Tomato, I’ll show you how to work it there, but if your router supports Wake-on-LAN, it shouldn’t vary much.

Access Your Computer Anytime and Save Energy with Wake-on-LANFirst point your browser to the Tomato admin interface and give it your username and password to get started. If you’re working outside of your local network, you’ll need to either know your external IP address or have set up a free domain with DynDNS. Once you’re in, click Tools -> WOL in the sidebar. All of your available devices are listed, and all you have to do to issue a Wake-on-LAN command is click on it. Simple, right?

If you don’t have a router that can handle WOL, you can still wake your computer from your browser using services like Wake On Lan from DSL Reports. In order to issue the Wake-on-LAN command from outside your network through a service like this, you’ll first need to set up port forwarding for UDP port 9 to the computer you want to wake up on your local network (here’s how that works).

Access Your Computer Anytime and Save Energy with Wake-on-LANNow you just need to know your external IP address or have assigned a domain name to your home computer, and the MAC address of the computer you want to wake. To find a computer’s MAC address in Windows, type ipconfig/all at the command prompt and find the series of 12 letters and numbers next to Physical Address. On a Mac, run the Network Utility application and write down the address next to Hardware Address. Give the webapp your IP address and the MAC address of the PC you want to wake up and voilà—the Wake-on-LAN command should be issued and your computer should start up or wake up straight away.

Wake Your Computer with Freeware

Access Your Computer Anytime and Save Energy with Wake-on-LANIf you don’t have a router supported by the Tomato or DD-WRT firmware, there are still other simple tools made to send wake-up commands to your computer. For Windows, one good looking option is called Magic Packet Sender. It can save your WOL profiles so sending that command is quick and simple.

Mac users should check out WakeOnLan, which offers both a regular app and a Dashboard widget. It scans your local network and grabs all of the local devices so it’s easy to find one and wake it up.

What To Do Now that Your Computer is On

Access Your Computer Anytime and Save Energy with Wake-on-LANWe’ve covered tons of remote access possibilities here at Lifehacker, and here are a few of my favorites that you may want to use now that you’ve started up your computer:

Remotely Shut Down Your Computer

When you’re finished accessing your home computer, you still may want to shut it down to save energy when you’re finished with it. Fact is, now that you’ve got remote access, there are lots of different ways you can go about shutting down your computer remotely. If you’re connected via SSH, you can shut down your computer from the command line. Likewise, you can easily shut down your computer graphically from a remote desktop or VNC connection the same way you would shut it down locally. If you feel like getting a bit more creative, here’s how to shut down Windows with a text message.

continue…"Access Your Computer Anytime and Save Energy with Wake-on-LAN"

Enable Wake-on-LAN in Windows

To enable Wake-on-LAN in Windows, right-click My Computer (or Computer in Vista), select Properties, then click on Device Manager (in XP that’s in the Hardware tab). Find your network card in the hardware list, right-click it and click Properties again. First go to the Power Management tab and tick the checkbox next to Allow this device to wake the computer.

Access Your Computer Anytime and Save Energy with Wake-on-LANNow head to the Advanced tab, which is full of options for your network adapter. We’re concerned with two options here. The first is the Wake From Shutdown entry near the end of the list. Scroll down to it and change the value to On. The next setting I tweaked was Wake-Up Capabilities (right below Wake From Shutdown), setting the value to Magic Packet. Hit OK and everything should be set. You’re ready to use the Wake-on-LAN feature using one of the methods described below.

I also had to update the drivers for my network adapter through the Device Manager for it to work, so for good measure you might want to do the same (right-click your network card and select Update drivers and let it download the latest from the internet).

Enable Wake-on-LAN in OS X

Access Your Computer Anytime and Save Energy with Wake-on-LANTo enable Wake-on-LAN for you Mac, open the Energy Saver Preference pane, click Options, and then tick the Wake for Ethernet network administrator access checkbox. That’s all there is to it.

Methods and Applications for Waking Your Computer with WOL

Now that you’ve enabled Wake-on-LAN in both your BIOS (if necessary) and your operating system, how do we go about actually waking up your computer? There are actually a number of methods for doing so, from issuing the wake command using your router to apps made specifically for this process. I’ll highlight a few of these methods below.