mirror of
https://github.com/alibaba/arthas.git
synced 2026-12-01 15:37:07 +00:00
refactor TelnetConsole to support pure java telnet client. #272
This commit is contained in:
1 parent
1332beca14
commit
388854b81c
4 files changed
+388
-178
No files matched your search
+6
-1
@@ -56,6 +56,11 @@
|
|||||||
<groupId>com.alibaba.middleware</groupId>
|
<groupId>com.alibaba.middleware</groupId>
|
||||||
<artifactId>cli</artifactId>
|
<artifactId>cli</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>jline</groupId>
|
||||||
|
<artifactId>jline</artifactId>
|
||||||
|
<version>2.14.6</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package com.taobao.arthas.client;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.Writer;
|
||||||
|
|
||||||
|
/***
|
||||||
|
* This is a utility class providing a reader/writer capability required by the
|
||||||
|
* weatherTelnet, rexec, rshell, and rlogin example programs. The only point of
|
||||||
|
* the class is to hold the static method readWrite which spawns a reader thread
|
||||||
|
* and a writer thread. The reader thread reads from a local input source
|
||||||
|
* (presumably stdin) and writes the data to a remote output destination. The
|
||||||
|
* writer thread reads from a remote input source and writes to a local output
|
||||||
|
* destination. The threads terminate when the remote input source closes.
|
||||||
|
***/
|
||||||
|
|
||||||
|
public final class IOUtil {
|
||||||
|
|
||||||
|
public static final void readWrite(final InputStream remoteInput, final OutputStream remoteOutput,
|
||||||
|
final InputStream localInput, final Writer localOutput) {
|
||||||
|
Thread reader, writer;
|
||||||
|
|
||||||
|
reader = new Thread() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
int ch;
|
||||||
|
|
||||||
|
try {
|
||||||
|
while (!interrupted() && (ch = localInput.read()) != -1) {
|
||||||
|
remoteOutput.write(ch);
|
||||||
|
remoteOutput.flush();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
writer = new Thread() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
while (true) {
|
||||||
|
int singleByte = remoteInput.read();
|
||||||
|
if (singleByte < 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
localOutput.write(singleByte);
|
||||||
|
localOutput.flush();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
writer.setPriority(Thread.currentThread().getPriority() + 1);
|
||||||
|
|
||||||
|
writer.start();
|
||||||
|
reader.setDaemon(true);
|
||||||
|
reader.start();
|
||||||
|
|
||||||
|
try {
|
||||||
|
writer.join();
|
||||||
|
reader.interrupt();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// Ignored
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.taobao.arthas.client;
|
||||||
|
|
||||||
|
public class OSUtils {
|
||||||
|
|
||||||
|
public static boolean isWindowsOS() {
|
||||||
|
boolean isWindowsOS = false;
|
||||||
|
String osName = System.getProperty("os.name");
|
||||||
|
if (osName.toLowerCase().indexOf("windows") > -1) {
|
||||||
|
isWindowsOS = true;
|
||||||
|
}
|
||||||
|
return isWindowsOS;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,213 +1,333 @@
|
|||||||
package com.taobao.arthas.client;
|
package com.taobao.arthas.client;
|
||||||
|
|
||||||
import com.taobao.middleware.cli.Argument;
|
import java.awt.event.ActionEvent;
|
||||||
import com.taobao.middleware.cli.CLI;
|
import java.awt.event.ActionListener;
|
||||||
import com.taobao.middleware.cli.CLIs;
|
|
||||||
import com.taobao.middleware.cli.CommandLine;
|
|
||||||
import com.taobao.middleware.cli.Option;
|
|
||||||
import com.taobao.middleware.cli.TypedOption;
|
|
||||||
import org.apache.commons.net.telnet.TelnetClient;
|
|
||||||
import org.apache.commons.net.telnet.WindowSizeOptionHandler;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.OutputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.commons.net.telnet.InvalidTelnetOptionException;
|
||||||
|
import org.apache.commons.net.telnet.TelnetClient;
|
||||||
|
import org.apache.commons.net.telnet.TelnetOptionHandler;
|
||||||
|
import org.apache.commons.net.telnet.WindowSizeOptionHandler;
|
||||||
|
|
||||||
|
import com.taobao.middleware.cli.CLI;
|
||||||
|
import com.taobao.middleware.cli.CLIException;
|
||||||
|
import com.taobao.middleware.cli.CommandLine;
|
||||||
|
import com.taobao.middleware.cli.UsageMessageFormatter;
|
||||||
|
import com.taobao.middleware.cli.annotations.Argument;
|
||||||
|
import com.taobao.middleware.cli.annotations.CLIConfigurator;
|
||||||
|
import com.taobao.middleware.cli.annotations.Description;
|
||||||
|
import com.taobao.middleware.cli.annotations.Name;
|
||||||
|
import com.taobao.middleware.cli.annotations.Option;
|
||||||
|
import com.taobao.middleware.cli.annotations.Summary;
|
||||||
|
|
||||||
|
import jline.Terminal;
|
||||||
|
import jline.TerminalSupport;
|
||||||
|
import jline.UnixTerminal;
|
||||||
|
import jline.console.ConsoleReader;
|
||||||
|
import jline.console.KeyMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author ralf0131 2016-12-29 11:55.
|
* @author ralf0131 2016-12-29 11:55.
|
||||||
|
* @author hengyunabc 2018-11-01
|
||||||
*/
|
*/
|
||||||
public class TelnetConsole{
|
@Name("arthas-client")
|
||||||
|
@Summary("Arthas Telnet Client")
|
||||||
|
public class TelnetConsole {
|
||||||
|
private static final String PROMPT = "$";
|
||||||
|
private static final int DEFAULT_CONNECTION_TIMEOUT = 5000; // 5000 ms
|
||||||
|
private static final int DEFAULT_BUFFER_SIZE = 1024;
|
||||||
|
|
||||||
private static final String PROMPT = "$";
|
private static final byte CTRL_C = 0x03;
|
||||||
private static final String DEFAULT_TELNET_PORT = "3658";
|
|
||||||
private static final int DEFAULT_CONNECTION_TIMEOUT = 5000; // 5000 ms
|
|
||||||
private static final String DEFAULT_WINDOW_WIDTH = "120";
|
|
||||||
private static final String DEFAULT_WINDOW_HEIGHT = "40";
|
|
||||||
private static final int DEFAULT_BUFFER_SIZE = 1024;
|
|
||||||
|
|
||||||
private TelnetClient telnet;
|
private boolean help = false;
|
||||||
private String address;
|
|
||||||
private int port;
|
|
||||||
private InputStream in;
|
|
||||||
private PrintStream out;
|
|
||||||
|
|
||||||
public TelnetConsole(String address, int port, int width, int height) {
|
private String targetIp = "127.0.0.1";
|
||||||
this.telnet = new TelnetClient();
|
private int port = 3658;
|
||||||
this.address = address;
|
|
||||||
this.port = port;
|
|
||||||
try {
|
|
||||||
telnet.addOptionHandler(new WindowSizeOptionHandler(width, height, true, false, true, false));
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
telnet.setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void connect() {
|
private String command;
|
||||||
try {
|
private String batchFile;
|
||||||
// Connect to the specified server
|
|
||||||
telnet.connect(address, port);
|
|
||||||
// Get input and output stream references
|
|
||||||
in = telnet.getInputStream();
|
|
||||||
out = new PrintStream(telnet.getOutputStream());
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public String readUntil(String prompt) {
|
private Integer width = null;
|
||||||
try {
|
private Integer height = null;
|
||||||
StringBuilder sBuffer = new StringBuilder();
|
|
||||||
byte[] b = new byte[DEFAULT_BUFFER_SIZE];
|
|
||||||
while(true) {
|
|
||||||
int size = in.read(b);
|
|
||||||
if(-1 != size) {
|
|
||||||
sBuffer.append(new String(b,0,size));
|
|
||||||
String data = sBuffer.toString();
|
|
||||||
if(data.trim().endsWith(prompt)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return sBuffer.toString();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public String readUntilPrompt() {
|
@Argument(argName = "targetIp", index = 0, required = false)
|
||||||
return readUntil(PROMPT);
|
@Description("target-ip")
|
||||||
}
|
public void setTargetIp(String targetIp) {
|
||||||
|
this.targetIp = targetIp;
|
||||||
|
}
|
||||||
|
|
||||||
public void write(String value) {
|
@Option(longName = "help", flag = true)
|
||||||
try {
|
@Description("Print usage")
|
||||||
out.println(value);
|
public void setHelp(boolean help) {
|
||||||
out.flush();
|
this.help = help;
|
||||||
} catch (Exception e) {
|
}
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendCommand(String command) {
|
@Option(shortName = "p", longName = "port")
|
||||||
try {
|
@Description("The remote server port")
|
||||||
write(command);
|
public void setPort(int port) {
|
||||||
} catch (Exception e) {
|
this.port = port;
|
||||||
e.printStackTrace();
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void disconnect() {
|
@Option(shortName = "c", longName = "command")
|
||||||
try {
|
@Description("The command to execute, multiple commands separated by ;")
|
||||||
telnet.disconnect();
|
public void setCommand(String command) {
|
||||||
} catch (Exception e) {
|
this.command = command;
|
||||||
e.printStackTrace();
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
@Option(shortName = "f", longName = "batchFile")
|
||||||
* 批处理模式
|
@Description("The batch file to execute")
|
||||||
*/
|
public void setBatchFile(String batchFile) {
|
||||||
public void batchModeRun(File batchFile) {
|
this.batchFile = batchFile;
|
||||||
if (batchFile == null || !batchFile.exists()) {
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
batchModeRun(readLines(batchFile));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void batchModeRun(List<String> commands) {
|
@Option(shortName = "w", longName = "width")
|
||||||
for (String command: commands) {
|
@Description("The terminal width")
|
||||||
// send command to server
|
public void setWidth(int width) {
|
||||||
sendCommand(command + " | plaintext");
|
this.width = width;
|
||||||
// read result from server and output
|
}
|
||||||
String response = readUntilPrompt();
|
|
||||||
System.out.print(response);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<String> readLines(File batchFile) {
|
@Option(shortName = "h", longName = "height")
|
||||||
List<String> list = new ArrayList<String>();
|
@Description("The terminal height")
|
||||||
BufferedReader br = null;
|
public void setheight(int height) {
|
||||||
try {
|
this.height = height;
|
||||||
br = new BufferedReader(new FileReader(batchFile));
|
}
|
||||||
String line = br.readLine();
|
|
||||||
while (line != null) {
|
|
||||||
list.add(line);
|
|
||||||
line = br.readLine();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
if (br != null) {
|
|
||||||
try {
|
|
||||||
br.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public TelnetConsole() {
|
||||||
try {
|
}
|
||||||
if (args.length < 1) {
|
|
||||||
System.err.println("Usage: TelnetConsole <target-ip> [-p PORT] [-c COMMAND] [-f BATCH_FILE] [-w WIDTH] [-h HEIGHT]");
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
CommandLine commandLine = parseArguments(args);
|
private static String readUntil(InputStream in, String prompt) {
|
||||||
|
try {
|
||||||
|
StringBuilder sBuffer = new StringBuilder();
|
||||||
|
byte[] b = new byte[DEFAULT_BUFFER_SIZE];
|
||||||
|
while (true) {
|
||||||
|
int size = in.read(b);
|
||||||
|
if (-1 != size) {
|
||||||
|
sBuffer.append(new String(b, 0, size));
|
||||||
|
String data = sBuffer.toString();
|
||||||
|
if (data.trim().endsWith(prompt)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sBuffer.toString();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TelnetConsole console = new TelnetConsole(
|
private static List<String> readLines(File batchFile) {
|
||||||
(String)commandLine.getArgumentValue("target-ip"),
|
List<String> list = new ArrayList<String>();
|
||||||
(Integer)commandLine.getOptionValue("p"),
|
BufferedReader br = null;
|
||||||
(Integer)commandLine.getOptionValue("w"),
|
try {
|
||||||
(Integer)commandLine.getOptionValue("h"));
|
br = new BufferedReader(new FileReader(batchFile));
|
||||||
|
String line = br.readLine();
|
||||||
|
while (line != null) {
|
||||||
|
list.add(line);
|
||||||
|
line = br.readLine();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
if (br != null) {
|
||||||
|
try {
|
||||||
|
br.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
console.connect();
|
public static void main(String[] args) throws IOException {
|
||||||
String logo = console.readUntilPrompt();
|
TelnetConsole telnetConsole = new TelnetConsole();
|
||||||
System.out.print(logo);
|
|
||||||
|
|
||||||
String cmd = commandLine.getOptionValue("c");
|
CLI cli = CLIConfigurator.define(TelnetConsole.class);
|
||||||
if (cmd != null) {
|
CommandLine commandLine = cli.parse(Arrays.asList(args));
|
||||||
List<String> cmds = new ArrayList<String>();
|
|
||||||
for (String c: cmd.split(";")) {
|
|
||||||
cmds.add(c.trim());
|
|
||||||
}
|
|
||||||
console.batchModeRun(cmds);
|
|
||||||
}
|
|
||||||
|
|
||||||
String filePath = commandLine.getOptionValue("f");
|
try {
|
||||||
if (filePath != null) {
|
CLIConfigurator.inject(commandLine, telnetConsole);
|
||||||
File batchFile = new File(filePath);
|
} catch (CLIException e) {
|
||||||
console.batchModeRun(batchFile);
|
e.printStackTrace();
|
||||||
}
|
System.out.println(usage(cli));
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
console.disconnect();
|
if (telnetConsole.isHelp()) {
|
||||||
} catch (Exception e) {
|
System.out.println(usage(cli));
|
||||||
e.printStackTrace();
|
System.exit(0);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private static CommandLine parseArguments(String[] args) {
|
// Try to read cmds
|
||||||
Argument addr = new Argument().setArgName("target-ip").setIndex(0).setRequired(true);
|
List<String> cmds = new ArrayList<String>();
|
||||||
Option port = new TypedOption<Integer>().setType(Integer.class).setShortName("p")
|
if (telnetConsole.getCommand() != null) {
|
||||||
.setDefaultValue(DEFAULT_TELNET_PORT);
|
for (String c : telnetConsole.getCommand().split(";")) {
|
||||||
Option command = new TypedOption<String>().setType(String.class).setShortName("c");
|
cmds.add(c.trim());
|
||||||
Option batchFileOption = new TypedOption<String>().setType(String.class).setShortName("f");
|
}
|
||||||
Option width = new TypedOption<Integer>().setType(Integer.class).setShortName("w")
|
} else if (telnetConsole.getBatchFile() != null) {
|
||||||
.setDefaultValue(DEFAULT_WINDOW_WIDTH);
|
File file = new File(telnetConsole.getBatchFile());
|
||||||
Option height = new TypedOption<Integer>().setType(Integer.class).setShortName("h")
|
if (!file.exists()) {
|
||||||
.setDefaultValue(DEFAULT_WINDOW_HEIGHT);
|
throw new IllegalArgumentException("batch file do not exist: " + telnetConsole.getBatchFile());
|
||||||
CLI cli = CLIs.create("TelnetConsole").addArgument(addr).addOption(port)
|
} else {
|
||||||
.addOption(command).addOption(batchFileOption).addOption(width).addOption(height);
|
cmds.addAll(readLines(file));
|
||||||
return cli.parse(Arrays.asList(args));
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final ConsoleReader consoleReader = new ConsoleReader(System.in, System.out);
|
||||||
|
consoleReader.setHandleUserInterrupt(true);
|
||||||
|
Terminal terminal = consoleReader.getTerminal();
|
||||||
|
|
||||||
|
if (terminal instanceof TerminalSupport) {
|
||||||
|
((TerminalSupport) terminal).disableInterruptCharacter();
|
||||||
|
}
|
||||||
|
|
||||||
|
// support catch ctrl+c event
|
||||||
|
terminal.disableInterruptCharacter();
|
||||||
|
if (terminal instanceof UnixTerminal) {
|
||||||
|
((UnixTerminal) terminal).disableLitteralNextCharacter();
|
||||||
|
}
|
||||||
|
|
||||||
|
int width = TerminalSupport.DEFAULT_WIDTH;
|
||||||
|
int height = TerminalSupport.DEFAULT_HEIGHT;
|
||||||
|
|
||||||
|
// 如果cmds 非空,则说明是batch mode
|
||||||
|
// 如果是 batch mode,如果没有设置 window size,则用默认的。
|
||||||
|
if (!cmds.isEmpty()) {
|
||||||
|
if (telnetConsole.getWidth() != null) {
|
||||||
|
width = telnetConsole.getWidth();
|
||||||
|
}
|
||||||
|
if (telnetConsole.getheight() != null) {
|
||||||
|
height = telnetConsole.getheight();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (telnetConsole.getWidth() != null) {
|
||||||
|
width = telnetConsole.getWidth();
|
||||||
|
} else {
|
||||||
|
width = terminal.getWidth();
|
||||||
|
// hack for windows dos
|
||||||
|
if (OSUtils.isWindowsOS()) {
|
||||||
|
width--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (telnetConsole.getheight() != null) {
|
||||||
|
height = telnetConsole.getheight();
|
||||||
|
} else {
|
||||||
|
height = terminal.getHeight();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final TelnetClient telnet = new TelnetClient();
|
||||||
|
telnet.setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT);
|
||||||
|
|
||||||
|
// send init terminal size
|
||||||
|
TelnetOptionHandler sizeOpt = new WindowSizeOptionHandler(width, height, true, true, false, false);
|
||||||
|
try {
|
||||||
|
telnet.addOptionHandler(sizeOpt);
|
||||||
|
} catch (InvalidTelnetOptionException e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
|
||||||
|
// ctrl + c event callback
|
||||||
|
consoleReader.getKeys().bind(new Character((char) CTRL_C).toString(), new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
try {
|
||||||
|
consoleReader.getCursorBuffer().clear(); // clear current line
|
||||||
|
telnet.getOutputStream().write(CTRL_C);
|
||||||
|
telnet.getOutputStream().flush();
|
||||||
|
} catch (Exception e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
// ctrl + d event call back
|
||||||
|
consoleReader.getKeys().bind(new Character(KeyMap.CTRL_D).toString(), new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
telnet.connect(telnetConsole.getTargetIp(), telnetConsole.getPort());
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println(
|
||||||
|
"Connect to telnet server error: " + telnetConsole.getTargetIp() + " " + telnetConsole.getPort());
|
||||||
|
e.printStackTrace();
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cmds.isEmpty()) {
|
||||||
|
IOUtil.readWrite(telnet.getInputStream(), telnet.getOutputStream(), consoleReader.getInput(),
|
||||||
|
consoleReader.getOutput());
|
||||||
|
} else {
|
||||||
|
batchModeRun(telnet, cmds);
|
||||||
|
telnet.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void batchModeRun(TelnetClient telnet, List<String> commands) throws IOException {
|
||||||
|
InputStream inputStream = telnet.getInputStream();
|
||||||
|
OutputStream outputStream = telnet.getOutputStream();
|
||||||
|
|
||||||
|
for (String command : commands) {
|
||||||
|
if (command.trim().isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// send command to server
|
||||||
|
outputStream.write((command + " | plaintext\n").getBytes());
|
||||||
|
outputStream.flush();
|
||||||
|
// read result from server and output
|
||||||
|
String response = readUntil(inputStream, PROMPT);
|
||||||
|
System.out.print(response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String usage(CLI cli) {
|
||||||
|
StringBuilder usageStringBuilder = new StringBuilder();
|
||||||
|
UsageMessageFormatter usageMessageFormatter = new UsageMessageFormatter();
|
||||||
|
usageMessageFormatter.setOptionComparator(null);
|
||||||
|
cli.usage(usageStringBuilder, usageMessageFormatter);
|
||||||
|
return usageStringBuilder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTargetIp() {
|
||||||
|
return targetIp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPort() {
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCommand() {
|
||||||
|
return command;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBatchFile() {
|
||||||
|
return batchFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getWidth() {
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getheight() {
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isHelp() {
|
||||||
|
return help;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user