PHP Source Code of /telnet.inc -------------------------------------------------------------------------------- Last Modified: 09:30 AM 07/06/1999 Print Date: 03:41 AM 07/12/1999 Size: 4386 -------------------------------------------------------------------------------- Date : 2/24/1998 Desc : Simple telnet session (port 23) to talk to a specific ip address. Designed to work with HP JetDirect Network Cards. $hostname can be set as an IP address or a host name found in /etc/hosts. Keep in mind it is best to set $hostname with the $ip variable in printer.php3 script file. Also : see printer.php3 (it calls this script) Updates: 2/24/98 Banner status can be changed */ class telnet_class { var $banner; var $port=23; var $hostname= ""; var $connection=0; var $state= "DISCONNECTED"; Function OpenConnection() { if($this->hostname== "") return( "2 it was not specified a valid hostname"); $this->connection=fsockopen($this->hostname,$this->port); switch($this->connection) { case 0: echo "File not opened"; return( "

File not created
".$this->hostname. " ".$this->port). "
"; case -3: return( "-3 socket could not be created"); case -4: return( "-4 dns lookup on hostname \"$hostname\" failed"); case -5: return( "-5 connection refused or timed out"); case -6: return( "-6 fdopen() call failed"); case -7: return( "-7 setvbuf() call failed"); default: return( ""); } } Function GetLine() { for($line= "";;) { if(feof($this->connection)) return(0); $line.=fgets($this->connection,100); $length=strlen($line); if($length>=2 && substr($line,$length-2,2)== "\r\n") return(substr($line,0,$length-2)); } } Function PutLine($line) { return(fputs($this->connection, "$line\r\n")); } Function CloseConnection() { if($this->connection!=0) { fclose($this->connection); $this->connection=0; } } /* ___________ Public Functions ______________ */ Function Open() { //OPEN CONNECTION TO HOST if(($error=$this->OpenConnection())!= "") return($error); //IF WE ARE CONNECTED, PERFORM COMMANDS if ($this->connection) { $this->PutLine( "\r\n"); $this->PutLine( "?"); $this->GetLine(); if (isset($this->banner)){ if (strval($this->banner)==0){ $this->PutLine( "BANNER:0"); } elseif (strval($this->banner)==1){ $this->PutLine( "BANNER:1"); } } //TELL HOST WE WISH TO QUIT $this->PutLine( "quit"); //READ WHAT THE HOST SENT US $message=0; $i=0; $jetdirect = "JETDIRECT PARAMETERS CONFIGURED"; $quitmsg = "USER QUITTING"; //LOOP THROUGH LINES UNTIL EOF OR 40 LINES ARE PROCESSED while (!feof($this->connection) && ($i<40)) { $currentline=$this->GetLine(); $bTripped=$bTripped || strstr(strtoupper($currentline),$jetdirect); //ONLY DISPLAY $jetdirect LINE AND BEYOND (do not show quit message) if (($bTripped) && (!strstr(strtoupper(chop($currentline)),$quitmsg))) { //SEE IF THIS LINE HAS THE BANNER STATUS if (strstr(strtoupper($currentline), "BANNER")){ //CREATE URL VARIABLES $banner= "hostname. "&banner="; $suffix= "\">Click to Change"; //SEE WHAT THE STATUS OF THE BANNER IS if (!strstr(strtoupper($currentline), "ENABLED")){ //BANNER IS DISABLE (see the ! above) MAKE URL FOR ENABLE // AND ADD THE MESSAGE TO THE ARRAY $messages[$message]=strtoupper($currentline).$banner. "1".$suffix; } elseif (strstr(strtoupper($currentline), "ENABLED")){ //BANNER IS ENABLED - MAKE URL FOR DISABLE // AND ADD THE MESSAGE TO THE ARRAY $messages[$message]=strtoupper($currentline).$banner. "0".$suffix; } } else { //ADD THE MESSAGE TO THE ARRAY $messages[$message]=strtoupper($currentline); $message++; } } $i++; } } else { $messages[0]= "-ERROR"; } //CLOSE THE CONNECTION $this->CloseConnection(); return($messages); } Function Close() { $this->CloseConnection(); $this->state= "DISCONNECTED"; return($error); } }; ?> -------------------------------------------------------------------------------- The syntax highlighted source is automatically generated by PHP from the plaintext script. If you want to see the source of source.php3, have a look here.