how to use threads and tee to log the telnet response from a server?

落花浮王杯 提交于 2020-05-27 09:25:31

问题


context: there are distinctions between different telnet clients, and certainly a distinction from those clients to, for example, the Apache library. In this case, yes, using an actual telnet client for MUD's, where the servers can be quite finnicky, so that API's or sockets just won't work.


How do I go about:

Spawn a new Process for every piped command.

Create Threads that read the output from one command and write it to the input of the next command.

right now there's no output for:

package mud;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;

public class App {

    private final static Logger log = Logger.getLogger(App.class.getName());

    private void telnet() throws IOException {
        String s = "rainmaker.wunderground.com";

        List<String> telnetCommand = Arrays.asList("sh", "-c", "rainmaker.wunderground.com | tee -a -i telnet.log");
        String foo = "foo";

        ProcessBuilder pb = new ProcessBuilder(telnetCommand);
        Process p = pb.start();
    }

    public static void main(String[] args) throws IOException {
        new App().telnet();
    }

}

so first I need to capture the result (the log file):

thufir@dur:~/NetBeansProjects/mud$ 
thufir@dur:~/NetBeansProjects/mud$ ll
total 96
drwxrwxr-x 7 thufir thufir  4096 May  3 21:00 ./
drwxrwxr-x 3 thufir thufir  4096 May  3 19:24 ../
drwxrwxr-x 5 thufir thufir  4096 May  3 20:50 build/
-rw-rw-r-- 1 thufir thufir  1034 May  3 19:20 build.gradle
drwxrwxr-x 8 thufir thufir  4096 May  3 21:00 .git/
-rw-rw-r-- 1 thufir thufir   154 May  3 19:20 .gitattributes
-rw-rw-r-- 1 thufir thufir   382 May  3 19:20 .gitignore
drwxrwxr-x 8 thufir thufir  4096 May  3 19:24 .gradle/
drwxrwxr-x 3 thufir thufir  4096 May  3 19:20 gradle/
-rwxrwxr-x 1 thufir thufir  5764 May  3 19:20 gradlew*
-rw-rw-r-- 1 thufir thufir  3056 May  3 19:20 gradlew.bat
-rw-rw-r-- 1 thufir thufir 35149 May  3 19:20 LICENSE
-rw-rw-r-- 1 thufir thufir    34 May  3 19:20 README.md
-rw-rw-r-- 1 thufir thufir   350 May  3 19:20 settings.gradle
drwxrwxr-x 4 thufir thufir  4096 May  3 19:44 src/
thufir@dur:~/NetBeansProjects/mud$ 
thufir@dur:~/NetBeansProjects/mud$ gradle clean run

BUILD SUCCESSFUL in 948ms
3 actionable tasks: 3 executed
thufir@dur:~/NetBeansProjects/mud$                                                                      
thufir@dur:~/NetBeansProjects/mud$ ll                                                                   
total 96
drwxrwxr-x 7 thufir thufir  4096 May  3 21:00 ./
drwxrwxr-x 3 thufir thufir  4096 May  3 19:24 ../
drwxrwxr-x 5 thufir thufir  4096 May  3 21:00 build/
-rw-rw-r-- 1 thufir thufir  1034 May  3 19:20 build.gradle
drwxrwxr-x 8 thufir thufir  4096 May  3 21:00 .git/
-rw-rw-r-- 1 thufir thufir   154 May  3 19:20 .gitattributes
-rw-rw-r-- 1 thufir thufir   382 May  3 19:20 .gitignore
drwxrwxr-x 8 thufir thufir  4096 May  3 19:24 .gradle/
drwxrwxr-x 3 thufir thufir  4096 May  3 19:20 gradle/
-rwxrwxr-x 1 thufir thufir  5764 May  3 19:20 gradlew*
-rw-rw-r-- 1 thufir thufir  3056 May  3 19:20 gradlew.bat
-rw-rw-r-- 1 thufir thufir 35149 May  3 19:20 LICENSE
-rw-rw-r-- 1 thufir thufir    34 May  3 19:20 README.md
-rw-rw-r-- 1 thufir thufir   350 May  3 19:20 settings.gradle
drwxrwxr-x 4 thufir thufir  4096 May  3 19:44 src/
-rw-rw-r-- 1 thufir thufir     0 May  3 21:00 telnet.log
thufir@dur:~/NetBeansProjects/mud$ 
thufir@dur:~/NetBeansProjects/mud$ cat telnet.log 
thufir@dur:~/NetBeansProjects/mud$ 

from the console:

thufir@dur:~$ 
thufir@dur:~$ telnet rainmaker.wunderground.com | tee -a -i telnet.log
Trying 35.160.169.47...
Connected to rainmaker.wunderground.com.
Escape character is '^]'.
------------------------------------------------------------------------------
*               Welcome to THE WEATHER UNDERGROUND telnet service!            *
------------------------------------------------------------------------------
*                                                                            *
*   National Weather Service information provided by Alden Electronics, Inc. *
*    and updated each minute as reports come in over our data feed.          *
*                                                                            *
*   **Note: If you cannot get past this opening screen, you must use a       *
*   different version of the "telnet" program--some of the ones for IBM      *
*   compatible PC's have a bug that prevents proper connection.              *
*                                                                            *
*           comments: jmasters@wunderground.com                              *
------------------------------------------------------------------------------

Press Return to continue:

Press Return for menu
or enter 3 letter forecast city code-- 

                 WEATHER UNDERGROUND MAIN MENU
                ******************************
                 1) U.S. forecasts and climate data
                 2) Canadian forecasts
                 3) Current weather observations
                 4) Ski conditions
                 5) Long-range forecasts
                 6) Latest earthquake reports
                 7) Severe weather
                 8) Hurricane advisories
                 9) Weather summary for the past month
                10) International data
                11) Marine forecasts and observations
                12) Ultraviolet light forecast
                 X) Exit program
                 C) Change scrolling to screen
                 H) Help and information for new users
                 ?) Answers to all your questions
                   Selection:x
Connection closed by foreign host.
thufir@dur:~$ 
thufir@dur:~$ cat telnet.log 
Trying 127.0.0.1...
Trying 35.160.169.47...
Connected to rainmaker.wunderground.com.
Escape character is '^]'.
------------------------------------------------------------------------------
*               Welcome to THE WEATHER UNDERGROUND telnet service!            *
------------------------------------------------------------------------------
*                                                                            *
*   National Weather Service information provided by Alden Electronics, Inc. *
*    and updated each minute as reports come in over our data feed.          *
*                                                                            *
*   **Note: If you cannot get past this opening screen, you must use a       *
*   different version of the "telnet" program--some of the ones for IBM      *
*   compatible PC's have a bug that prevents proper connection.              *
*                                                                            *
*           comments: jmasters@wunderground.com                              *
------------------------------------------------------------------------------

Press Return to continue:
Press Return for menu
or enter 3 letter forecast city code-- 
                 WEATHER UNDERGROUND MAIN MENU
                ******************************
                 1) U.S. forecasts and climate data
                 2) Canadian forecasts
                 3) Current weather observations
                 4) Ski conditions
                 5) Long-range forecasts
                 6) Latest earthquake reports
                 7) Severe weather
                 8) Hurricane advisories
                 9) Weather summary for the past month
                10) International data
                11) Marine forecasts and observations
                12) Ultraviolet light forecast
                 X) Exit program
                 C) Change scrolling to screen
                 H) Help and information for new users
                 ?) Answers to all your questions
                   Selection:thufir@dur:~$ 
thufir@dur:~$ 

perhaps somehow create a delay while waiting for a response rather than just running the command and exiting?

来源:https://stackoverflow.com/questions/61584982/how-to-use-threads-and-tee-to-log-the-telnet-response-from-a-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!