Posts Tagged ‘script’

Arduino Project 5: Internet Control without Ethernet Shield

February 18, 2013 22 comments

I’m getting ready to start an Arduino home automation project, so I started looking at ways to interface with an Arduino across the internet. That way I’ll be able to control all of the lights, locks, etc. anywhere I have internet access.

Now, the obvious answer was to buy an Ethernet shield, but I already run a home web server so that seemed unnecessary. I saw a few solutions using Processing or Python scripts, but that seemed unnecessarily complicated. It took a fair amount of digging and brainstorming, but I’ve found an ultra-easy, ultra-flexible, and ultra-fast solution. Ultra.

There are a few major assumptions here.

  1. You have a computer running a web server that is accessible from the open internet, using a static IP address or a Dynamic DNS name. Your computer is either directly connected to your ISP and using a public IP address or your router is set up for port forwarding. If you don’t have this set up yet, just Google it – there are a lot of tutorials that explain how to set up a home web server.
  2. Your home web server has PHP. If not, this will still work, you’ll just have to re-write it in Java or whatever server-side language you’re using.
  3. The Arduino will be plugged into the web server via USB.

Here’s the basic concept: the Arduino can’t read files from the server via the USB serial connection, so the server will have to “push” the message. The server side code (PHP, Java, whatever you choose) cannot talk directly to the serial COM port, so we need a local script on the web server that can talk to the serial (USB) port. Last, the Arduino sketch has to be written so it can “catch” and process the message.

In this example, we’ll just create a barebones web page for controls that will turn an LED on and off via the Arduino. Let’s start with the sketch. The sketch is just listening on the serial connection for a 1 or 0. If it receives a 1 it will turn the LED on, if it receives a 0 it will turn the LED off.

Alex Glover
February 2013

void setup() {
//set the LED pin to OUTPUT
pinMode(13, OUTPUT);

void loop() {
//wait until the serial connection is open
while (Serial.available() ==0);

//read from the serial connection; the - '0' is to cast the values as the int and not the ASCII code
int val = - '0';

//print to the console for testing

//if we've recieved a '1', turn on the LED and print a message
Serial.println("Received a 1");
digitalWrite(13, HIGH);
//if we've recieved a '0', turn off the LED and print a message
Serial.println("Received a 0");
digitalWrite(13, LOW);

Pretty straightforward, right? OK, now we need two scripts, one to send a ‘1’ and one to send a ‘0’. In Windows, simply create a text file (call it whatever you want), and give it a .bat extension. In my setup, my files are called serial_out_0.bat and serial_out_1.bat. Each script has only one line of code.

ECHO 0 > COM3:


ECHO 1 > COM3:

Note that you might have to change the COM designation. You can check which COM your Arduino is connected to by looking in the Arduino IDE under Tools –> Serial Port. If you’re not using Windows, you should be able to do this pretty easily in a shell script. At this point you can test to see if the batch scripts will turn the LED off and on. Also ensure that the web server will be able to execute these scripts (don’t assign user-specific privileges or save them in protected directories). If you’re unsure, just put these scripts in the same web root directory where you’ll host your web page.

Easy right? Alright, the last piece is the web form. All you need are buttons within a form that will then execute the batch scripts we wrote earlier. Easiest solution is to use ‘submit’ buttons and then check for which post variables are set. The rest of the code is very straightforward so I’ll let it speak for itself.

if(isset($_POST[‘submitOn’])) {
else if(isset($_POST[‘submitOff’])) {
<form action=”control.php” method=”post”>
<input type=”submit” name=”submitOn” value=”Submit On”>
<input type=”submit” name=”submitOff” value=”Submit Off”>


You’ll have to change the paths to correspond with the location of your scripts, but otherwise that’s it. Add some CSS if you want to add some polish to your controls. This is a very simple example, but you should be able to adapt this code to any project.


Automate remote password changes with BASH and ‘Expect’

September 15, 2012 5 comments

I think this topic has been covered, in varying levels, by many people on the internet. Still, I felt like I was bashing my head against the wall trying to figure this out. To prevent head trauma to some other sys admins, I thought I’d post this.

First, some assumptions. Let’s assume you’re a sysadmin, working mainly in a LAMP environment (or at least Linux and MySQL). Let’s also assume you have a reasonable number of hosts, between 5 and 50. You don’t really have the right scale to warrant setting up RADIUS or TACACS authentication systems, but it is a real pain in the ass to reset passwords on a regular basis. Last, let’s assume that for some good reason, you use the same password across many machines, both for the OS and DB authentication. To change all of those passwords, you would have to SSH into each box, run a couple password change commands, and exit each host.

Using BASH and ‘Expect,’ I’ve automated that process. For my experiment, I assumed that I wanted to change OS passwords for users root, oracle, and asm and DB passwords for root and oracle. I provide a list of hosts that are going to be updated in a control file titled ‘password_change_list’ There is a single shell script, ‘change_password’ (to invoke, just cd to the appropriate directory and type ./change_password’).  Instead of taking a bunch of screenshots, I’ve included a quick screencapture of the script in action!

In the demo, I show the contents of the password_change_list control file, 2 hosts – localhost and my dev box. Add more hosts to this file as necessary. I start the script, which first asks you to confirm that you updated the control file. Next it asks for the current password, and then for the new password. The script will then SSH to the first host and run the password change commands, then exit. The script then loops and SSH’s to the next host, and so on (in the demo, you’ll see it fails to log on to my dev box via SSH, incorrect password). Now, since we’re entering passwords to the command line and logging the commands, the passwords will be in clear text. To address that, the script will ask if you want to obfuscate all of the clear text passwords in the log. If you choose no, it will warn you again that the passwords are in clear text. Alternatively, if you leave clear text passwords in the log file, you can run the ‘obfuscate_passwords’ script, provide the new password, and it will replace all instances of the password with asterisks.

Files can be downloaded here (it’s a tar file with no extension).

Obviously, the script will need some tweaking to suit your purpose, but I hope this helps you get started.

%d bloggers like this: