<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>John Roach &#187; telnet</title>
	<atom:link href="http://johnroach.info/tag/telnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://johnroach.info</link>
	<description>Coding for life</description>
	<lastBuildDate>Wed, 25 Apr 2012 07:48:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Knowing when to fold ( i.e. SSH is better than Telnet )</title>
		<link>http://johnroach.info/2009/10/28/knowing-when-to-fold-i-e-ssh-is-better-than-telnet/</link>
		<comments>http://johnroach.info/2009/10/28/knowing-when-to-fold-i-e-ssh-is-better-than-telnet/#comments</comments>
		<pubDate>Wed, 28 Oct 2009 16:08:45 +0000</pubDate>
		<dc:creator>John Roach</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[ip home automation project]]></category>
		<category><![CDATA[embedded device]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php ssh connection]]></category>
		<category><![CDATA[php telnet connection]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[telnet]]></category>

		<guid isPermaLink="false">http://johnroach.info/?p=93</guid>
		<description><![CDATA[You have probably seen my  previous post on connecting to Telnet through PHP although it was a novel idea I have just learned that (from a Novell site. No pun intended.) Telnet is very insecure!! To make things clearer Telnet &#8230; <a href="http://johnroach.info/2009/10/28/knowing-when-to-fold-i-e-ssh-is-better-than-telnet/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>You have probably seen my  previous post on connecting to Telnet through PHP although it was a novel idea I have just learned that (from a Novell site. No pun intended.) Telnet is very insecure!! To make things clearer Telnet usually creates connections as below;</p>
<p style="text-align: center;"><a href="http://johnroach.info/wp-content/uploads/2009/10/Telnet-Client-server-unencrypted.png"><img class="aligncenter size-full wp-image-111" title="Telnet-Client-server-unencrypted" src="http://johnroach.info/wp-content/uploads/2009/10/Telnet-Client-server-unencrypted.png" alt="Telnet-Client-server-unencrypted" width="305" height="209" /></a></p>
<p style="text-align: left;">Up here you can see a hacker can sniff the client-server connection and actually steal the user name and password sent by client. This really won&#8217;t do since if someone can spoof/hack/attack our embedded system which we will be adding to home appliances things may go awry. i.e. you will start seeing automated vacuum cleaners attacking your cat. ( Statistics show that H4X0Rz hate cats. )</p>
<p style="text-align: left;">Hence we can clearly see we need another way for the server and client to talk to each other. After some short Googling I am thinking of using SSH which is native to Linux/Unix based systems. Where the connection will be as below ;</p>
<p style="text-align: left;"><a href="http://johnroach.info/wp-content/uploads/2009/10/SSH-client-server-encrypted.png"><img class="aligncenter size-full wp-image-112" title="SSH-client-server-encrypted" src="http://johnroach.info/wp-content/uploads/2009/10/SSH-client-server-encrypted.png" alt="SSH-client-server-encrypted" width="305" height="209" /></a>As you can see the SSH encryption creates a safer environment for the client and the server plus any DNS attacks and eavesdropping is clearly impossible. (if not probable.)</p>
<p style="text-align: left;">Henceforth a new PHP and Python (this time I will write the server+client in Python) shall be written. Please expect updates.</p>
]]></content:encoded>
			<wfw:commentRss>http://johnroach.info/2009/10/28/knowing-when-to-fold-i-e-ssh-is-better-than-telnet/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Connecting to PHP using telnet and sending data to the same port using Java</title>
		<link>http://johnroach.info/2009/10/25/connecting-php-using-telnet-and-sending-data-to-the-same-port-using-java/</link>
		<comments>http://johnroach.info/2009/10/25/connecting-php-using-telnet-and-sending-data-to-the-same-port-using-java/#comments</comments>
		<pubDate>Sun, 25 Oct 2009 10:13:26 +0000</pubDate>
		<dc:creator>John Roach</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[ip home automation project]]></category>
		<category><![CDATA[embedded device]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[languages]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php telnet connection]]></category>
		<category><![CDATA[port]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[socket]]></category>
		<category><![CDATA[telnet]]></category>
		<category><![CDATA[thread]]></category>

		<guid isPermaLink="false">http://johnroach.info/?p=71</guid>
		<description><![CDATA[Hi there! Have been busy busy busy. What I needed was a way to the following; Above you are looking at a very simple layout. ( Almost childishly simple ) The embedded device has Linux installed with a supposedly very &#8230; <a href="http://johnroach.info/2009/10/25/connecting-php-using-telnet-and-sending-data-to-the-same-port-using-java/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Hi there! Have been busy busy busy. What I needed was a way to the following;<br />
<a href="http://johnroach.info/wp-content/uploads/2009/10/graf.png"><img class="size-medium wp-image-72 aligncenter" title="Very Simple Layout" src="http://johnroach.info/wp-content/uploads/2009/10/graf-300x225.png" alt="Very Simple Layout" width="300" height="225" /></a>Above you are looking at a very simple layout. ( Almost childishly simple ) The embedded device has Linux installed with a supposedly very light JVM ( Java Virtual Machine ) So if I am to send data to and from the server and display it to the user I have to do some scripting both in Java and in PHP. Let us first take a look at our universal Java-Socket program which basically opens and listens to a certain port (socket );</p>
<p><span id="more-71"></span></p>
<pre name="code" class="java">
import java.io.*;
import java.net.*;

//This program will listen to port 8189

public class ThreadedEchoServer{

 public static void main (String[] args){

 try{

 int i =1;
 ServerSocket s = new ServerSocket(8189);

 for (;;)
 {  
 Socket incoming = s.accept( );
 System.out.println("Spawning " + i);
 Thread t = new ThreadedEchoHandler(incoming, i);
 t.start();
 i++;
 }

 }catch (Exception e){

 e.printStackTrace();

 }

 }

}
/**
This class handles the client input for one server socket
connection.
*/
class ThreadedEchoHandler extends Thread
{
/**
 Constructs a handler.
 @param i the incoming socket
 @param c the counter for the handlers (used in prompts)
*/
public ThreadedEchoHandler(Socket i, int c)
{
 incoming = i; counter = c;
}

public void run()
{  
 try
 {  
 BufferedReader in = new BufferedReader
 (new InputStreamReader(incoming.getInputStream()));
 PrintWriter out = new PrintWriter
 (incoming.getOutputStream(), true /* autoFlush */);

 out.println( "Hello! Enter BYE to exit." );

 boolean done = false;
 while (!done)
 {  
 String str = in.readLine();
 if (str == null) done = true;
 else
 {  
 // out.println("Echo (" + counter + "): " + str);
 out.println("Connected to : " + str + "&lt;br/&gt;");
 //this is where you get the dang str!!! that is sent by embedded  device.
 //OR this is where you get the configurations from php server on the embedded device.
 System.out.println("Php server sent the following string :"+str);

 if (str.trim().equals("BYE"))
 done = true;
 }
 }
 incoming.close();
 }
 catch (Exception e)
 {  
 e.printStackTrace();
 }
}

private Socket incoming;
private int counter;
}
</pre>
<p>So we have finished with the Java code. Which both can be installed in the server or the embedded device. Notice that the port is  8189. Let us assume that this nice piece of work is on the embedded device. Now we have to send commands to and from the device via PHP using a telnet like protocol. That is when I started searching Google. Came up with a nice site <a href="http://www.geckotribe.com/php-telnet/" target="_blank">http://www.geckotribe.com/php-telnet/</a></p>
<p>A nice site if you want to send data to and from telnet. And indeed it was what I needed. However I needed to do some modifications to the code they provided. The code below belongs to them;</p>
<p>(PHPTelnet.php)</p>
<pre name="code" class="php">
&lt;?php
/*
PHPTelnet
*/

class PHPTelnet {
 var $show_connect_error=1;

 var $use_usleep=1;    // change to 1 for faster execution default:0
 // don't change to 1 on Windows servers unless you have PHP 5
 var $sleeptime=125000;
 var $loginsleeptime=1000000;

 var $fp=NULL;
 var $loginprompt;

 var $conn1;
 var $conn2;

 /*
 0 = success
 1 = couldn't open network connection
 2 = unknown host
 3 = login failed
 4 = PHP version too low
 */
 function Connect($server,$user,$pass) {
 $rv=0;
 $vers=explode('.',PHP_VERSION);
 $needvers=array(4,3,0);
 $j=count($vers);
 $k=count($needvers);
 if ($k&lt;$j) $j=$k;
 for ($i=0;$i&lt;$j;$i++) {
 if (($vers[$i]+0)&gt;$needvers[$i]) break;
 if (($vers[$i]+0)&lt;$needvers[$i]) {
 $this-&gt;ConnectError(4);
 return 4;
 }
 }

 $this-&gt;Disconnect();

 if (strlen($server)) {
 if (preg_match('/[^0-9.]/',$server)) {
 $ip=gethostbyname($server);
 if ($ip==$server) {
 $ip='';
 $rv=2;
 }
 } else $ip=$server;
 } else $ip='127.0.0.1';

 if (strlen($ip)) {
 if ($this-&gt;fp=fsockopen($ip,8189))
 {

 if( ($user!="")&amp;&amp; ($pass!=""))
 {
 fputs($this-&gt;fp,$this-&gt;conn1);
 $this-&gt;Sleep();

 fputs($this-&gt;fp,$this-&gt;conn2);
 $this-&gt;Sleep();
 $this-&gt;GetResponse($r);
 $r=explode("\n",$r);
 $this-&gt;loginprompt=$r[count($r)-1];

 fputs($this-&gt;fp,"$user\r");
 $this-&gt;Sleep();

 fputs($this-&gt;fp,"$pass\r");
 if ($this-&gt;use_usleep) usleep($this-&gt;loginsleeptime);
 else sleep(1);
 $this-&gt;GetResponse($r);
 $r=explode("\n",$r);
 if (($r[count($r)-1]=='')||($this-&gt;loginprompt==$r[count($r)-1])) {
 $rv=3;
 $this-&gt;Disconnect();
 }

 }
 } else $rv=1;
 }

 if ($rv) $this-&gt;ConnectError($rv);
 return $rv;
 }

 function Disconnect($exit=1) {
 if ($this-&gt;fp) {
 if ($exit) $this-&gt;DoCommand('exit',$junk);
 fclose($this-&gt;fp);
 $this-&gt;fp=NULL;
 }
 }

 function DoCommand($c,&amp;$r) {
 if ($this-&gt;fp) {
 fputs($this-&gt;fp,"$c\r");
 $this-&gt;Sleep();
 $this-&gt;GetResponse($r);
 $r=preg_replace("/^.*?\n(.*)\n[^\n]*$/","$1",$r);
 }
 return $this-&gt;fp?1:0;
 }

 function GetResponse(&amp;$r) {
 $r='';
 do {
 $r.=fread($this-&gt;fp,1000);
 $s=socket_get_status($this-&gt;fp);
 } while ($s['unread_bytes']);
 }

 function Sleep() {
 if ($this-&gt;use_usleep) usleep($this-&gt;sleeptime);
 else sleep(1);
 }

 function PHPTelnet() {
 $this-&gt;conn1=chr(0xFF).chr(0xFB).chr(0x1F).chr(0xFF).chr(0xFB).
 chr(0x20).chr(0xFF).chr(0xFB).chr(0x18).chr(0xFF).chr(0xFB).
 chr(0x27).chr(0xFF).chr(0xFD).chr(0x01).chr(0xFF).chr(0xFB).
 chr(0x03).chr(0xFF).chr(0xFD).chr(0x03).chr(0xFF).chr(0xFC).
 chr(0x23).chr(0xFF).chr(0xFC).chr(0x24).chr(0xFF).chr(0xFA).
 chr(0x1F).chr(0x00).chr(0x50).chr(0x00).chr(0x18).chr(0xFF).
 chr(0xF0).chr(0xFF).chr(0xFA).chr(0x20).chr(0x00).chr(0x33).
 chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0x2C).chr(0x33).
 chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0xFF).chr(0xF0).
 chr(0xFF).chr(0xFA).chr(0x27).chr(0x00).chr(0xFF).chr(0xF0).
 chr(0xFF).chr(0xFA).chr(0x18).chr(0x00).chr(0x58).chr(0x54).
 chr(0x45).chr(0x52).chr(0x4D).chr(0xFF).chr(0xF0);
 $this-&gt;conn2=chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).
 chr(0x22).chr(0xFF).chr(0xFE).chr(0x05).chr(0xFF).chr(0xFC).chr(0x21);
 }

 function ConnectError($num) {
 if ($this-&gt;show_connect_error) switch ($num) {
 case 1: echo '&lt;br /&gt;[PHP Telnet] &lt;a href="http://www.geckotribe.com/php-telnet/errors/fsockopen.php"&gt;Connect failed: Unable to open network connection&lt;/a&gt;&lt;br /&gt;'; break;
 case 2: echo '&lt;br /&gt;[PHP Telnet] &lt;a href="http://www.geckotribe.com/php-telnet/errors/unknown-host.php"&gt;Connect failed: Unknown host&lt;/a&gt;&lt;br /&gt;'; break;
 case 3: echo '&lt;br /&gt;[PHP Telnet] &lt;a href="http://www.geckotribe.com/php-telnet/errors/login.php"&gt;Connect failed: Login failed&lt;/a&gt;&lt;br /&gt;'; break;
 case 4: echo '&lt;br /&gt;[PHP Telnet] &lt;a href="http://www.geckotribe.com/php-telnet/errors/php-version.php"&gt;Connect failed: Your server\'s PHP version is too low for PHP Telnet&lt;/a&gt;&lt;br /&gt;'; break;
 }
 }
}

return;
?&gt;
</pre>
<p>However because I needed something that used a different port had to change the port number if you look at the comments in the form you will see where I edited the code.</p>
<p>Also because my Telnet was without a password ( i wanted to test it first) I added an &#8220;if&#8221; statement. Now to the index.php code;</p>
<pre name="code" class="php">
&lt;html&gt;&lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;/head&gt;&lt;body&gt;

&lt;?php

require_once "PHPTelnet.php";

$telnet = new PHPTelnet();

// if the first argument to Connect is blank,
// PHPTelnet will connect to the local host via 127.0.0.1
// the second blank is for the username (may be for embedded device)
// the third blank is for the password (may be for embedded device)
$result = $telnet-&gt;Connect('','','');

if ($result == 0) {
$telnet-&gt;DoCommand('The embedded device!!', $result);
// NOTE: $result may contain newlines
echo $result;
$telnet-&gt;DoCommand('BYE', $result);
echo $result;
// say Disconnect(0); to break the connection without explicitly logging out
$telnet-&gt;Disconnect();
}

?&gt;
&lt;/body&gt;&lt;/html&gt;</pre>
<p>Now to run a test; first run the Java program ( I hope you know how to compile and run a java program from console.) And than simply open index.php ( of course from Apache! ) And voilà!! you have a program that can connect directly to a php server page!!</p>
<p>Questions? Comments just send it in!</p>
]]></content:encoded>
			<wfw:commentRss>http://johnroach.info/2009/10/25/connecting-php-using-telnet-and-sending-data-to-the-same-port-using-java/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

