Skip to content

Instantly share code, notes, and snippets.

@pklaus
Created January 8, 2010 11:14
Show Gist options
  • Save pklaus/271987 to your computer and use it in GitHub Desktop.
Save pklaus/271987 to your computer and use it in GitHub Desktop.
Simple Koukaam NETIO-230A automation via Raw TCP using C++, Java, PHP, Ruby, expect, Bash-TCP and wget (HTTP)
// example for a raw TCP socket connection using C++ to interface the Koukaam NETIO-230A
// resources:
// <http://www.ecst.csuchico.edu/~beej/guide/ipc/usock.html>
// <http://cs.nmu.edu/~randy/Classes/CS228/Notes/making-a-client-socket.html>
#include <iostream>
#include <string>
#include <unistd.h> // gethostbyname()
#include <sys/socket.h> // socket(), connect()
#include <arpa/inet.h> // sockaddr_in
#include <strings.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/wait.h>
#include <signal.h>
int MakeSocket(char *host, int port) {
using namespace std;
int s;
int len;
struct sockaddr_in sa;
struct hostent *hp;
struct servent *sp;
int portnum;
int ret;
hp = gethostbyname(host);
bcopy((char *)hp->h_addr, (char *)&sa.sin_addr, hp->h_length);
sa.sin_family = hp->h_addrtype;
sa.sin_port = htons(port);
s = socket(hp->h_addrtype, SOCK_STREAM, 0);
ret = connect(s, (struct sockaddr *)&sa, sizeof(sa));
cout << "Connect to host " << host << " port " << port << endl;
return s;
}
int main()
{
using namespace std;
string msg, ans;
char str[1024];
int t;
int fd = MakeSocket("localhost", 23);
t = recv(fd, str, 1024, 0);
str[t-2] = '\0';
cout << "answer is " << str << endl;
msg = "login admin password\n";
send(fd, msg.c_str(), msg.length(),0);
t = recv(fd, str, 1024, 0);
str[t-2] = '\0';
cout << "answer is " << str << endl;
}
// Example using Java
// Author: Philipp Klaus
import java.net.*;
import java.io.*;
public class Netio230a {
Netio230a() throws IOException {
Socket netio = new Socket ( "192.168.1.2", 23 );
PrintWriter out = new PrintWriter(netio.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(netio.getInputStream()));
System.out.println(in.readLine());
out.println("login admin password\n");
System.out.println(in.readLine());
out.println("port list\n");
System.out.println(in.readLine());
out.println("quit\n");
System.out.println(in.readLine());
out.close();
in.close();
netio.close();
}
public static void main (String[] args) {
try {
Netio230a netio = new Netio230a();
} catch (IOException e) {
System.out.print(e);
System.exit(1);
}
}
}
<?php
// Example using a class for PHP
// Author: Philipp Klaus
class netio230a {
private $socket;
public $errorMessage;
public function connect($host,$user,$pw,$port=23) {
/* Get the IP address for the target host. */
$address = gethostbyname($host);
/* Create a TCP/IP socket. */
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($this->socket === false) {
$errorMessage = "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
return false;
}
$result = socket_connect($this->socket, $address, $port);
if ($result === false) {
$errorMessage = "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($this->socket)) . "\n";
return false;
}
$out = '';
$out = @socket_read($this->socket, 1024);
if (socket_last_error($this->socket) === 104) {
$errorMessage = "Connection unexpectedly reset by peer - reboot the Koukaam NETIO 230A";
return false;
}
// login:
$in = "login $user $pw\r\n";
socket_write($this->socket, $in, strlen($in));
// check response:
$out = socket_read($this->socket, 1024);
if ($out==="250 OK\r\n") {
return true;
} else {
$errorMessage = "login failed. wrong credentials?";
return false;
}
}
public function getPortStatus() {
return $this->queryNetio230a("port list");
}
private function queryNetio230a($request) {
$request .= "\r\n";
socket_write($this->socket, $request, strlen($request));
return str_replace("250 ","",str_replace("\r\n","",socket_read($this->socket, 1024)));
}
public function __destruct() {
// closing socket
socket_close($this->socket);
}
}
$netio = new netio230a();
if ($netio->connect("192.168.1.2","admin","your choosen password")){
$output = $netio->getPortStatus();
} else {
$output = "Error while trying to connect to the Koukaam NETIO 230A: ".$netio->errorMessage;
}
?>
<html>
<head>
<title>Control the Koukaam NETIO 230A</title>
</head>
<body>
<br>
The port status is currently: <?=$output?>
</body>
</html>
#!/usr/bin/env ruby
require "socket"
addr = "192.168.1.2"
port = 23
s = TCPSocket.new(addr, port)
puts s.recv(1024)
s.puts("login admin password")
puts s.recv(1024)
s.puts("port list")
puts s.recv(1024)
s.puts("quit")
puts s.recv(1024)
s.close
#!/usr/bin/expect
############################################################
# Script for Netio power controller
# Disables/enables watchdog on port $netio_port
#
# WS 20110317
# found on <http://koukaam.se/forum/viewthread.php?forum_id=33&thread_id=1752#3667>
############################################################
########### Configure here ###########
# Device address and port
set address "192.168.10.5"
set port "1234"
# Login data
set user "admin"
set pass "my_password"
# Controlled power port
set netio_port "1"
######### Configuration end ##########
# Check arguments, only "on" and "off" are valid
# Exit with errocode 1 if argument is empty or unknown
set action [lindex $argv 0]
if { "$action"=="" } { send_user "Error: No argument given\nValid arguments are \"on\" and \"off\"\nExample: $argv0 on\n"; exit 1}
if { $action==on } {
set action "enable"; send_user "Switching watchdog on port $netio_port on\n"
} elseif {$action==off} {
set action "disable"; send_user "Switching watchdog on port $netio_port off\n"
} else {
send_user "Error: Could not understand argument \"$action\"\nValid arguments are \"on\" and \"off\"\nExample: $argv0 on\n"; exit 1
}
## Here we go
# Start telnet
spawn -noecho /usr/bin/telnet $address $port
# Send login
expect "100 HELLO*\r" { send "login $user $pass\r" }
# Set watchdog
expect "250*\r" { send "port wd $netio_port $action\r" }
# Show watchdog settings
expect "250*\r" { send "port wd $netio_port\r" }
# Log out
expect "250*\r" { send "quit\r" }
expect eof
# EOF
#!/bin/bash
# NETIO-230A example using the builtin tcp support of the bash shell.
# Let's suppose we want to switch power socket 3 on and power socket 2 off:
TCP_HOST=192.168.1.2
TCP_PORT=1234
exec 5<>/dev/tcp/${TCP_HOST}/${TCP_PORT}
echo -e "login admin password\nport 3 1\nport 2 0\nquit" >&5
cat <&5
#!/bin/bash
# wget netio230a minimal HTTP example
wget --load-cookies cookies.txt --save-cookies cookies.txt http://192.168.1.2/tgi/control.tgi?login=p:admin:password
wget --load-cookies cookies.txt --save-cookies cookies.txt http://192.168.1.2/tgi/control.tgi?p=1001
wget --load-cookies cookies.txt --save-cookies cookies.txt http://192.168.1.2/tgi/control.tgi?q=q
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment