hi,

this seems to be a popular topic, yet i haven't seen any postings that answers my question. Here's what i'm looking to do.

I want to create a telnet connection from my web page to another machine. I want to be able to send commands such as user name and password to log on, and then execute various commands while connected. I also want to be able to receive the results of my commands.

eg:

telnet to xxx.xxx.xxx.xxx
pass in user name
pass in password
execute a command
post the results to my web page
close telnet connection

If anyone can share what they might know about this litte problem of mine it would be greatly appreciated.

cheers,

jamie

    You'd be better off using a Java Applet. There are several around including some that do SSH (which you should be using instead of telnet). I use one with Webmin to do remote administration via web browser on my servers.

      i figured it would be easier using another method, however, i've been told it has to be done this way 🙁 stupid i know, but what can you do. Secondly, the machine which i will be telneting to is older and has a one only OS running, and it's within out intranet, so no outside access will be allowed.

      the commands i will be running are all being done in the background with very little or no user interaction. I appreciate any suggestions and many thanks to those who are helping me out.

      jamie

        • [deleted]

        I guess that java applet is a java-version of the telnet client program?
        In that case you could simple call this as the URL in your browser :
        telnet:server.name
        and on 99% of the browsers that will start your local telnet client. without java.

        If you just want to execute some commands and print the result, you can use utilities like 'expect' or even SSH itself with a simple shell script on unix.

        Or you can use PHP's socket functions. But... you will have to write routines to send data and receive data, and you pretty much allways need to know what the other side is going to send back so you know when it's finished sending data.

        A security advice: never let the telnet script execute commands that you enter through the webpage. If you feel you must do that, put a GOOD filter on the commands, so hackers have a hard time messing with it.

          convince the Power That Be that SSH would be a better choice for this.
          you did mention that everything is behind a firewall, so i will ignore the security advantages of ssh for now 🙂
          the nice thing about ssh is that you can set up public/private key pairs so that no password is required, and you can give it an additional command to be run on the remote machine instead of opening a terminal session.
          so all you do in php is "$output = ssh somehost ls"
          simple, eh?

            I had this problem just a few weeks ago and my 'rhoids are still hurting. The specific problem I had was that there was no hard eof() to work with. A wonderful person made a suggestion that turned out to actually work.

            Consider this code: (note that I'm changing the IP to protect the innocent) and adding comments.

            <?php

            $fp = fsockopen("xxx.xx.xxx.xx", 8111, $errno, $errstr);
            if (!$fp) {
            echo "ERROR: $errno - $errstr<br>\n";

            } else {

            ****Note: this is the string that I'm building to send:

            $s_tni = "323555xxxx";
            $s_site = "BSA";
            $s_junk = "Retrieve#";
            $s_junk .= $s_tni;
            $s_junk .= $s_site;
            $s_junk .= "/n";

            sleep(1);

            ****Note: this is the login I'm sending. The sleep() is to give him time to respond.

            fwrite($fp,"TELExxxxxxx\n");
            sleep(2);

            ****Note: this is the command line:

            fwrite($fp,$s_junk);
            sleep(2);
            socket_set_blocking($fp, 0);

            ****Note: this is the telnet response:

            $j = fgets($fp,150);
            socket_set_blocking($fp, 1);

            fclose($fp);
            }

            ?>

            Now, the important thing is this. If you don't know how long the response string is, and you can't use the eof() function to learn how long it is, use the socket_set_blocking function to simply time-out the response. If your telnet connection is reasonably quick with its reply, time it out at 2 seconds. Whatever it's sending should be there by then.

            k

              Write a Reply...