我正在尝试远程登录到服务器,运行命令并将该命令的输出放入文件中。 我可以得到文件中的命令,但不能得到这个命令的结果。 我也看不到我的控制台上的输出,所以我假设它运行但我不确定。 有人知道吗?
public final static void main(String[] args) throws IOException, InterruptedException
{
FileOutputStream fout = null;
try
{
fout = new FileOutputStream ("spyfile.log");
}
catch (IOException e)
{
System.err.println(
"Exception while opening the spy file: "
+ e.getMessage());
}
TelnetClient telnet;
telnet = new TelnetClient();
try
{
telnet.connect("myserver", 23);
}
catch (IOException e)
{
e.printStackTrace();
System.exit(1);
}
telnet.registerSpyStream(fout);
PrintStream out = new PrintStream( telnet.getOutputStream() );
out.println( "mycommand" );
try
{
telnet.disconnect();
}
catch (IOException e)
{
e.printStackTrace();
System.exit(1);
}
fout.close();
System.exit(0);
}
最佳答案
我不确定 TelneClient 是什么,如果这是 Commons Net类,那么您就错过了实际读取在 telnet session 期间交换的数据的部分。请运行 this example看看它是如何工作的,一旦你得到它,你就可以根据你的需要进行切割。
关于Java 捕获文件中的 telnet 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15843310/