Java 从Linux shell读取输出并将其存储到JSON对象
我想从linux shell上的命令读取json输出 我使用jcraft来建立ssh连接,并使用channel来执行telnet命令Java 从Linux shell读取输出并将其存储到JSON对象,java,json,linux,ssh,telnet,Java,Json,Linux,Ssh,Telnet,我想从linux shell上的命令读取json输出 我使用jcraft来建立ssh连接,并使用channel来执行telnet命令 String host="<host>"; String user="<user>"; String password="<pass>"; String command1="telnet <ip>"; try{ java.ut
String host="<host>";
String user="<user>";
String password="<pass>";
String command1="telnet <ip>";
try{
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session=jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
System.out.println("Connected");
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command1);
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream in=channel.getInputStream();
channel.connect();
DataOutputStream dataOut = new DataOutputStream(channel.getOutputStream());
byte[] tmp=new byte[1024];
int count = 0;
int count1 = 0;
while(true){
while(in.available()>0){
int i=in.read(tmp, 0, 1024);
if(i<0)break;
System.out.println(new String(tmp, 0, i));
String asksForUsername = new String(tmp, 0, i);
if(asksForUsername.contains("login") && count <= 0) {
dataOut.writeBytes("<username>");
dataOut.writeBytes("\n");
dataOut.flush();
count++;
}
if(asksForUsername.contains("Password") && count1 <= 0) {
dataOut.writeBytes("<password>");
dataOut.writeBytes("\n");
dataOut.flush();
count1++;
}
if(count == 1 && count1 == 1) {
count++;
count1++;
dataOut.writeBytes("<shell command to get the json response>");
dataOut.writeBytes("\n");
dataOut.flush();
}
}
if(channel.isClosed()){
System.out.println("exit-status: "+channel.getExitStatus());
break;
}
try{Thread.sleep(1000);}catch(Exception ee){}
}
channel.disconnect();
session.disconnect();
count = 0;
count1 = 0;
}catch(Exception e){
e.printStackTrace();
}
String host=”“;
字符串user=“”;
字符串密码=”;
String command1=“telnet”;
试一试{
java.util.Properties config=new java.util.Properties();
配置放置(“检查”、“否”);
JSch JSch=新的JSch();
Session Session=jsch.getSession(用户,主机,22);
session.setPassword(密码);
session.setConfig(config);
session.connect();
System.out.println(“已连接”);
Channel=session.openChannel(“exec”);
((ChannelExec)channel.setCommand(command1);
channel.setInputStream(空);
((ChannelExec)channel.setErrStream(System.err);
InputStream in=channel.getInputStream();
channel.connect();
DataOutputStream dataOut=新的DataOutputStream(channel.getOutputStream());
字节[]tmp=新字节[1024];
整数计数=0;
int count1=0;
while(true){
while(in.available()>0){
inti=in.read(tmp,0,1024);
如果(i您需要添加以下代码行来获取和读取消息
//获取此通道的InputStream。作为消息从远程端到达的所有数据都可以从此流读取。
InputStream in=channelExec.getInputStream()
//从上面设置的输入流读取输出
BufferedReader=new BufferedReader(new InputStreamReader(in));您需要添加以下代码行以获取和读取消息
//获取此通道的InputStream。作为消息从远程端到达的所有数据都可以从此流读取。
InputStream in=channelExec.getInputStream()
//从上面设置的输入流读取输出
BufferedReader reader=新的BufferedReader(新的InputStreamReader(in))~我已经在使用输入流读取输出,问题是我需要提供一些控制台输入并读取它们的输出。我将得到的最终输出是JSON,我需要读取并分配给字符串或JSON对象以供进一步使用。~我已经在使用输入流读取输出,问题是我需要给出一些控制台输入并读取它们的输出。我将得到的最终输出是一个JSON,我需要读取并分配给一个字符串或JSON对象以供进一步使用。