Note that there are some explanatory texts on larger screens.

plurals
  1. POphp jquery telnet CLI app
    primarykey
    data
    text
    <p>I am using the following class for my application and it is working absolutely fine:</p> <pre><code>class Telnet { /* (c) thies@thieso.net */ var $sock = null; function telnet($host, $port) { $this-&gt;sock = fsockopen($host, $port); socket_set_timeout($this-&gt;sock, 2, 0); } function close() { if ($this-&gt;sock) fclose($this-&gt;sock); $this-&gt;sock = null; return "Disconnected"; } function write($buffer) { $buffer = str_replace(chr(255), chr(255) . chr(255), $buffer); fwrite($this-&gt;sock, $buffer); } function getc() { return fgetc($this-&gt;sock); } function read_till($what) { $buf = ''; while (1) { $IAC = chr(255); $DONT = chr(254); $DO = chr(253); $WONT = chr(252); $WILL = chr(251); $theNULL = chr(0); $c = $this-&gt;getc(); if ($c === false) return nl2br($buf); if ($c == $theNULL) { continue; } if ($c == "\021") { continue; } if ($c != $IAC) { $buf .= $c; if ($what == (substr($buf, strlen($buf) - strlen($what)))) { return nl2br($buf); } else { continue; } } $c = $this-&gt;getc(); if ($c == $IAC) { $buf .= $c; } else if (($c == $DO) || ($c == $DONT)) { $opt = $this-&gt;getc(); //echo "we wont ".ord($opt)."\n"; fwrite($this-&gt;sock, $IAC . $WONT . $opt); } elseif (($c == $WILL) || ($c == $WONT)) { $opt = $this-&gt;getc(); //echo "we dont ".ord($opt)."\n"; fwrite($this-&gt;sock, $IAC . $DONT . $opt); } else { //echo "where are we? c=".ord($c)."\n"; } } } } </code></pre> <p>then i added another page for a CLI (which will interact through jquery with this class):</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Simple CLI&lt;/title&gt; &lt;script src="./../../script/jquery-1.7.1.min.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div&gt; User:&lt;input id="username" type="text" /&gt;Password:&lt;input id="password" type="text" /&gt; &lt;input id="connect" type="button" value="Connect" /&gt; &lt;div id="out"&gt;Welcome: Type 'connect' to connect to the terminal server.&lt;br /&gt;&lt;/div&gt; &lt;input id="in" type="text" /&gt; &lt;input id="disconnect" type="button" value="Disconnect" /&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; $('#connect').click(function(e){ var user = $('#username').val(); var pass = $('#password').val(); if( user != "" &amp;&amp; pass != "" ){ $.post('abc.php', {cmd:"connect",user:user,pass:pass}, function(data) { $('#out').append(data); }); } }); $('#disconnect').click(function(e){ $.post('abc.php', {cmd:"disconnect"}, function(data) { $('#out').append(data); }); }); $('#in').keyup(function(e) { if (e.which !== 13) return; var cmd = { cmd: $(this).val() }; $(this).val(''); $.post('abc.php', cmd, function(data) { $('#out').append(data); }); }); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>the class is used by:</p> <pre><code>if($_POST['cmd']){ $command = $_POST['cmd']; switch($command){ case 'connect': $output = ''; if( !empty($_POST['user']) &amp;&amp; !empty($_POST['pass']) ){ $tn = new telnet("127.0.0.1", 23); $output = $tn-&gt;read_till("ogin:"); $tn-&gt;write($_POST['user'] . "\r\n"); $output .= $tn-&gt;read_till("word: "); $tn-&gt;write($_POST['pass'] . "\r\n"); $output .= $tn-&gt;read_till(":&gt; "); echo $output; } break; case 'disconnect': echo $tn-&gt;close(); break; default: $tn-&gt;write($command."\r\n"); echo $tn-&gt;read_till(":&gt; "); break; } } </code></pre> <p>the question is that how and where can i save the class object, so that whenever i call a class object through jquery, it should have the same object and not that each command will first open a new connection with the telnet server.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload