Telnet commands through php?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Telnet commands through php?

Post by m0u53m4t »

Is it possible to make a php file run telnet commands???
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yes.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

use fsockopen. i think
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

ole wrote:use fsockopen. i think
make sure you read the user comments as well about telnet
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post by m0u53m4t »

Ok, so this is my php to connect to my school mail server:


Code: Select all

<?php

   $handle = smtp_connect("mail.cardiffhigh.cardiff.sch.uk", 25, 30, 1, 1, 1);
   echo smtp_command($handle, "EHLO mail.cardiffhigh.cardiff.sch.uk", 1, 1);
   echo smtp_command($handle, "MAIL FROM:<owen.campbell-moore@cardiffhigh.cardiff.sch.uk>\r\n", 1, 1);
   echo smtp_command($handle, "RCPT TO:<owen.campbell-moore@cardiffhigh.cardiff.sch.uk>\r\n", 1, 1);
   echo smtp_command($handle, "DATA\r\n", 1, 1);
   echo smtp_command($handle, "hello\r\n.\r\n", 1, 1);
   // don't do it like this - it will hang up
   // echo smtp_command($handle, "hello", 1, 1);
   // echo smtp_command($handle, "\r\n.\r\n", 1, 1);
   echo smtp_command($handle, "QUIT\r\n", 1, 1);
   smtp_close($handle);
  
  
   function smtp_connect($host, $port, $timeout=30, $echo_command=False, $echo_response=False, $nl2br=False)
   {
       $errno = 0;
       $errstr = 0;
       if($echo_command)
       {
           if($nl2br) { echo nl2br("CONNECTING TO $host\r\n"); }
           else { echo "CONNECTING TO $host\r\n"; }
       }
       $handle = fsockopen($host, $port, $errno, $errstr, $timeout);
       if(!$handle)
       {
           if($echo_command)
           {
               if($nl2br) { echo nl2br("CONNECTION FAILED\r\n"); }
               else { echo "CONNECTION FAILED\r\n"; }
           }
           return False;
       }
       if($echo_command)
       {
           if($nl2br) { echo nl2br("SUCCESS\r\n"); }
           else { echo "SUCCESS\r\n"; }
       }
       $response = fgets($handle,1);
       $bytes_left = socket_get_status($handle);
       if ($bytes_left > 0) { $response .= fread($handle, $bytes_left["unread_bytes"]); }
       if($echo_response)
       {
           if($nl2br) { echo nl2br($response); }
           else { echo $response; }
       }
       return $handle;
   }

   function smtp_command($handle, $command, $echo_command=False, $nl2br=False)
   {
       if($echo_command)
       {
           if($nl2br) { echo nl2br($command); }
           else { echo $command; }
       }
       fputs($handle, $command);
       $response = fgets($handle,1);
       $bytes_left = socket_get_status($handle);
       if ($bytes_left > 0) { $response .= fread($handle, $bytes_left["unread_bytes"]); }
       if($nl2br) { return nl2br($response); }
       else { return $response; }
   }
  
   function smtp_close($handle)
   {
       fclose($handle);
   }
?>
But when I run it I get this:

CONNECTING TO mail.cardiffhigh.cardiff.sch.uk
SUCCESS

Warning: fread(): Length parameter must be greater than 0. in /home/freehost/t35.com/j/u/juniorfiles/test.php on line 42
EHLO mail.cardiffhigh.cardiff.sch.uk
Warning: fread(): Length parameter must be greater than 0. in /home/freehost/t35.com/j/u/juniorfiles/test.php on line 61
MAIL FROM:

Warning: fread(): Length parameter must be greater than 0. in /home/freehost/t35.com/j/u/juniorfiles/test.php on line 61
RCPT TO:

Warning: fread(): Length parameter must be greater than 0. in /home/freehost/t35.com/j/u/juniorfiles/test.php on line 61
DATA

Warning: fread(): Length parameter must be greater than 0. in /home/freehost/t35.com/j/u/juniorfiles/test.php on line 61
hello
.

Warning: fread(): Length parameter must be greater than 0. in /home/freehost/t35.com/j/u/juniorfiles/test.php on line 61
QUIT

Warning: fread(): Length parameter must be greater than 0. in /home/freehost/t35.com/j/u/juniorfiles/test.php on line 61
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

You do a test on $bytes_left and then you use $bytes_left["unread_bytes"]... The latter seems to be 0 according to PHP.
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post by m0u53m4t »

So any idea how I can fix it?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Start with reading the manual better:

http://be2.php.net/manual/nl/function.s ... a-data.php (socket_get_status is an alias for this function)
unread_bytes (int) - the number of bytes currently contained in the PHP's own internal buffer.

Opmerking: You shouldn't use this value in a script.
Supposing you want to use [url]http://www.php.net/stream_get_line[url] instead (or was it _read_line?)
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post by m0u53m4t »

Im sorry, im a bit new to this. What exactly do I have to do to my php to get it to work?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you're wanting to use smtp to mail something, why not use an existing class like phpMailer?
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post by m0u53m4t »

My hosting doesn't allow the mail function, so im doing it with telnet instead.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

phpMailer doesn't require the mail function. It can send via SMTP.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I'd like to see and SSH client done with AJAX. I know there are java applets available for it (probably better suited too since correctly mimicking a terminal with JavaScript would be nightmare if you wanted it to work with full-screen apps like nano). Ignore me... I'm rambling again... where's my medicine?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

btw, i thought that phpmailer had a smtp server implementation to do all the nasty work for you..

<off-topic>
ssh client isn't that hard... Well, with basic user+password authentication and the php ssh2 extension.. (I never got pub/priv key to work...)
</off-topic>
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post by m0u53m4t »

feyd wrote:phpMailer doesn't require the mail function. It can send via SMTP.
So where is there an example of it? Or could you write out a quick example of how to use it?
Post Reply