gpt4 book ai didi

java - 使用 apache-commons-net TelnetClient 发送终端命令时如何禁用回显

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:14:34 29 4
gpt4 key购买 nike

所以,我有一个使用 org.apache.commons.net.telnet.TelnetClient 的类类(class)。它尝试发送命令并读取响应。

public class AutomatedTelnetClient
{
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private String prompt = "$";

public AutomatedTelnetClient(String server, String user, String password)
{
try
{
EchoOptionHandler echoopt = new EchoOptionHandler(false, false, false, false);
telnet.addOptionHandler(echoopt);

// Connect to the specified server
telnet.connect(server, 23);

// Get input and output stream references
in = telnet.getInputStream();
out = new PrintStream(telnet.getOutputStream());

// Log the user on
readUntil("login: ");
write(user);
readUntil("Password: ");
write(password);

// Advance to a prompt
readUntil(prompt + " ");
}
catch (Exception e)
{
e.printStackTrace();
}
}

public void su(String password)
{
try
{
write("su");
readUntil("Password: ");
write(password);
prompt = "#";
readUntil(prompt + " ");
}
catch (Exception e)
{
e.printStackTrace();
}
}

public String readUntil(String pattern)
{
try
{
char lastChar = pattern.charAt(pattern.length() - 1);
StringBuffer sb = new StringBuffer();
boolean found = false;
char ch = (char) in.read();
while (true)
{
System.out.print(ch);
sb.append(ch);
if (ch == lastChar)
{
if (sb.toString().endsWith(pattern))
{
return sb.toString();
}
}
ch = (char) in.read();
}
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}

public void write(String value)
{
try
{
out.println(value);
out.flush();
System.out.println(value);
}
catch (Exception e)
{
e.printStackTrace();
}
}

public String sendCommand(String command)
{
try
{
write(command);
return readUntil(prompt + " ");
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}

public void disconnect()
{
try
{
telnet.disconnect();
}
catch (Exception e)
{
e.printStackTrace();
}
}

public static void main(String[] args)
{
try
{
AutomatedTelnetClient telnet = new AutomatedTelnetClient(
"...", "...", "...");

System.out.println("Got Connection...");

System.out.println("run command");
telnet.sendCommand("ls ");
telnet.sendCommand("cd netConf");
telnet.sendCommand("ls ");
telnet.sendCommand("cd lanSetup");
telnet.sendCommand("ls ");
telnet.sendCommand("cd dhcpd");
telnet.sendCommand("show interface 2");

telnet.disconnect();
System.out.println("DONE");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

问题是,当我发送命令时,响应以某种回声开头。例如,当我发送 ls 命令时,这是我读取的响应

[mls 
....

我试过在 how to disable echo 上搜索,但似乎没有人知道答案。所以,我决定询问这个社区。有谁知道如何禁用此回声?

编辑

查看EchoOptionHandler的源代码让我感到困惑。为什么子协商方法只返回 null

最佳答案

有趣的问题。总结一下我的努力:我没有让它正常工作但这里有一些我的发现:

您不能将 IAC DON'T ECHO 直接写入数据通道,这是通过此示例中的命令和选项完成的

int[] msg = {TelnetCommand.DONT,TelnetOption.ECHO};
telnet.sendSubnegotiation(msg);

您可以注册 telnet.registerNotifHandler(new MyNotificationHandler()); 以在连接设置期间调试命令

public class MyNotificationHandler implements TelnetNotificationHandler
{
@Override
public void receivedNegotiation(int negotiation_code, int option_code)
{
System.out.println("--->"+get(negotiation_code)+" "
+TelnetOption.getOption(option_code));
}

private String get(int i)
{
if(i==TelnetNotificationHandler.RECEIVED_DO){return "RECEIVED_DO";}
else if(i==TelnetNotificationHandler.RECEIVED_DONT){return "RECEIVED_DONT";}
else if(i==TelnetNotificationHandler.RECEIVED_WILL){return "RECEIVED_WILL";}
else if(i==TelnetNotificationHandler.RECEIVED_WONT){return "RECEIVED_WONT";}
else if(i==TelnetNotificationHandler.RECEIVED_COMMAND)
{return "RECEIVED_COMMAND";}
return "UNKNOWN";
}
}

关于java - 使用 apache-commons-net TelnetClient 发送终端命令时如何禁用回显,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6410579/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com