如何使用线程和tee从服务器记录telnet响应?

时间:2020-05-04 04:01:52

标签: java multithreading exec telnet tee

上下文:不同的telnet客户端之间有区别,当然也有那些客户端与Apache库之间的区别。在这种情况下,是的,可以为MUD使用实际的telnet客户端,其中的服务器可能非常精细,因此API或套接字将无法工作。


我如何go关于:

为每个管道命令产生一个新进程。

创建读取的线程 一个命令的输出并将其写入下一个命令的输入 命令。

现在没有output用于

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();
    }

}

所以首先我需要捕获结果(日志文件):

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$ 

从控制台:

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:~$ 

也许是在等待响应时以某种方式创建了delay,而不仅仅是运行命令并退出了吗?

0 个答案:

没有答案
相关问题