Monday, March 26, 2012

How to Remotely Shut Down or Restart Windows PCs



image
Windows includes Shutdown.exe, a simple utility for remotely shutting down or restarting Windows computers on your local network. To use Shutdown.exe, you must first configure the PCs you want to shut down or restart remotely.
Once you’ve configured the PCs, you can use a graphical user interface or command to restart the PCs from another Windows system. You can even remotely shut down or restart the PCs from a Linux system.
Configuration
The remote registry service must be enabled on each computer you want to shut down remotely – it’s disabled by default.
To enable it, first launch the Services control panel on the computer you want to shut down remotely. To do this, click the Start button, type services.msc into the Start menu and press Enter.
Locate the “Remote Registry” service in the list, right-click it and select Properties.

From the properties window, set the Startup type to Automatic and click the Start button to launch the service.

Next, you’ll have to open the required port in the computer’s firewall. Click Start, type “Allow a program” and press Enter. In the window that appears, click the “Change settings” button. Scroll down in the list and enable the “Windows Management Instrumentation (WMI)” exception.

Your user account must also have administrator permissions on the remote computer. If it doesn’t, the shutdown command will fail due to lack of permissions.
Remote Shut Down
To shut down the computer, launch a Command Prompt window on another computer (click Start, type Command Prompt, and press Enter). Type the following command into the command prompt window for a graphical interface:
shutdown /i
From the remote shutdown dialog window, you can add one or more computer names and specify whether you want to shut down or restart the system. You can optionally warn users and log a message to the system’s event log.

Not sure what the name of the remote computer is? Click Start on the remote computer, right-click Computer in the Start menu, and select Properties. You’ll see the computer’s name.

You can also use a command instead of the graphical interface. Here’s the equivalent command:
shutdown /s /m \\chris-laptop /t 30 /c “Shutting down for maintenance.” /d P:1:1

Shut Down From Linux
Once you’ve set up the computer, you can also shut it down from a Linux system. This requires the samba-common package installed – you can install it on Ubuntu with the following command:
sudo apt-get install samba-common
Once you have, use the following command from a terminal:
net rpc shutdown -I ip.address -U user%password
Replace “ip.address” with the numerical address of the Windows computer, “user” with the username of an account that has administrator privileges on the remote computer, and “password” with the user account’s password. You can add a “-r” option to the command if you want the computer to restart instead of shutting down.

If you have remote desktop access, you can also access the desktop and shut down or restart that way. The shutdown.exe command is a faster way of doing the same thing designed for system administrators – you can shut down or reboot multiple computers much faster than you could by logging into them one-by-one.



Saturday, March 10, 2012

How to Make Simple Graphical Shell Scripts with Zenity on Linux



Zenity adds graphical interfaces to shell scripts with a single command. Shell scripts are a great way to automate repetitive tasks, but they’re normally confined to the terminal — Zenity brings them out of the terminal and onto your desktop.
We’ve given an introduction to shell scripting in the past. You don’t have to be a programmer to get started with shell scripts — they require little more than knowledge of Linux terminal commands.

Getting Zenity

Zenity comes with Ubuntu by default. If you use an Ubuntu derivative, Such as Kubuntu, you may have to install it manually with the following command:
sudo apt-get install zenity
Zenity is a part of GNOME, so it should already be included on Linux distributions that use the GNOME desktop. Check your package manager for the zenity package if you don’t have it.

Using Zenity

You can play around with Zenity from the terminal. Let’s say you want to create an error window when a problem occurs with your shell script. Here’s an example command you could use:
zenity –error –title=”An Error Occurred” –text=”A problem occurred while running the shell script.”
Run the command and you’ll see a window with the message.
Put this single command into your shell script at the correct place and you’ll have a graphical error message. You could also use variables to include more information about the error.
Let’s say you want to ask a yes or no question. You could use a command like this one:
zenity –question –title=”Query” –text=”Would you like to run the script?”
You can catch the yes or no response in your shell script and perform different commands based on which button the user clicks.
There’s also a text entry dialog:
zenity –entry –title=”Favorite Website” –text=”What is your favorite website?”
Catch the user’s input in a shell script and you could store it as a variable.
There’s also a file picker, calendar, and other types of dialogs. For a full list of dialog types and their options, consult Zenity’s manual page.

An Example Script

Let’s try using Zenity to create a simple graphical shell script. With just three commands, we can create a graphical timer program:
#!/bin/bash
# This script asks the user for a time, waits the specified amount
# of time, and shows an alert dialog.
TIME=$(zenity –entry –title=”Timer” –text=”Enter a duration for the timer.\n\n Use 5s for 5 seconds, 10m for 10 minutes, or 2h for 2 hours.”)
sleep $TIME
zenity –info –title=”Timer Complete” –text=”The timer is over.\n\n It has been $TIME.”
We’re using some extra tricks here. We get the value of the TIME variable from the first zenity command and feed it to the sleep command. We’re also using /n to create new lines of text in the zenity dialogs.
After saving the shell script and running the chmod +x command on it to give it executable permissions, we can launch it.
Enter a duration and the script will use the standard sleep command to count down in the background. When the sleep command’s timer finishes, the script will display the zenity info message.
You could create a desktop or panel shortcut for this script and run it without even touching the terminal.

Extract the first paragraph text from a web page with PHP


Using strpos and substr

Assuming the content to extract the paragraph from is in the variable $html (which may have come from a file, database, template or downloaded from an external website), use the following code to work out the position of the first <p> tag, the first </p> tag after that tag, and then get all the HTML between them including the opening and closing tags:
1$start strpos($html'<p>');
2$end strpos($html'</p>'$start);
3$paragraph substr($html$start$end-$start+4);
Line 1 gets the position of the first opening <p> tag
Line 2 gets the position of the first </p> after the first opening <p>
Line 3 then uses substr to get the HTML. The third parameter is the number of characters to copy and is calculated by subtracting $start from $end and adding on the length of "</p>" so it is included in the extracted HTML.

Converting to plain text

If the extracted paragraph needs to be in plain text rather than HTML, use the following to remove the HTML tags and convert HTML entities into normal plain text:
1$paragraph = html_entity_decode(strip_tags($paragraph));