Hi,

I have read a lot of forums and have tried many options that were suggested but none of them seems to work for me.

I am using fsockopen to telnet a host server. The connection seems to work however, fputs and fgets doesn't seem to work.

When I try to telnet the machine from the command line it works. It prompts for a password (no username) and then I can submit other commands.

Here is my code:

  $fp=fsockopen($pshost, 23, $errno, $errstr, 30);
  if (!$fp) {
              echo "ERROR: $errno - $errstr<br />\n";
 }
 else
 {
               fputs($fp, "$pspasswd\r\n");
               fputs($fp, "show all\r\n");
               while(!feof($fp)) {  
                        echo fgets($fp,512);
              }
            fclose($fp);
  }

I simply get no output!

Any help will be appreciated.

Naureen

    I think you should be using socket_read() and socket_write() instead?

      According to your suggestion, I tried using socket functions but I get the following error:

      Fatal error: Call to undefined function: socket_create() in *****

      I am using php 4.2.2 and it should support socket functions according to the manual.

      Any ideas why its complaining about this?

        Sorry, my mistake, fsockopen() is not the same as socket_create() in that it doesn't require the sockets extension. You are using the correct functions however I'm still not sure why you are not passing any data. Have you checked that the variables being passed to fputs() do contain the required data?

          From the PHP manual:

          UDP sockets will sometimes appear to have opened without an 
          error, even if the remote host is unreachable. The error will only 
          become apparent when you read or write data to/from the 
          socket. The reason for this is because UDP is a "connectionless" 
          protocol, which means that the operating system does not try to 
          establish a link for the socket until it actually needs to send or 
          receive data.
          
          

          So it could be that your connection ISN'T opened.

            is the server you are trying to get into windows? i've run into complications with windows and php sockets.

            i know there was a bug a while ago, and i haven't tried since. just an idea, have no clue if it was ever fixed.

              Actually, now I am considering ssh into the remote server. It's not a windows box, its a Solaris machine.

              I am thinking about executing shell_exec comands in php. Is that a good idea?

              The only reason I am considering doing this is because I don't want to pass plain text passwords over the network. I have PHP 4.2.2 and it doesn't have SSL support for fsockopen.

              Any ideas?

                the reason its not working is because telnet requires a handshake sequence. you need to send it some special data before it will talk to you.

                here it is:

                $firstack[] = "\xFF\xFB\x1F\xFF\xFB\x20\xFF\xFB\x18\xFF\xFB"
                             ."\x27\xFF\xFD\x01\xFF\xFB\x03\xFF\xFD\x03\xFF\xF"
                	       ."\x23\xFF\xFC\x24\xFF\xFA\x1F\x00\x50\x00\x18\xFF"
                	       ."\xF0\xFF\xFA\x20\x00\x33\x38\x34\x30\x30\x2C\x33"
                	       ."\x38\x34\x30\x30\xFF\xF0\xFF\xFA\x27\x00\xFF\xF0"
                	       ."\xFF\xFA\x18\x00\x58\x54\x45\x52\x4D\xFF\xF0";
                
                $secondack[] = "\xFF\xFC\x01\xFF\xFC\x22\xFF\xFE\x05\xFF\xFC\x21";
                

                so right when you connect, do:
                fputs($fp, $firstack);
                sleep(1);
                fputs($fp, $secondack);

                then authenticate by sending username and password.

                for more see the telnet rfc (854)

                p.s. i see now you are thinking of ssh. that would be way more complex than telnet so you may need to think about other ways of doing this.

                  Thanks for the suggestion,

                  I tried sending the ack headers and now it looks like 'fputs' isn't writing anything to the host. The script just hangs and then timesout with 'Action Canceled' Error page.

                    yeah my mistake. its an array so you have to do

                    fputs($fp, $firstack[0]);
                    fputs($fp, $secondack[0]);

                      Originally posted by mmilano
                      is the server you are trying to get into windows? i've run into complications with windows and php sockets.

                      i know there was a bug a while ago, and i haven't tried since. just an idea, have no clue if it was ever fixed.

                      It's the socket_read function that's broken on the Windows platform.
                      I tried it a while ago on a 4.3.2 i think, and it 's still broken launched code on a Windows box didn't work, launched it on a Linux box and it worked like a charm.

                      But then again it isn't relevaind as the guy is using fgets instead of socket_read

                        Yeah! I changed to code to account for the arrays. Still no luck!
                        The page still hangs!

                          Write a Reply...