在单独的 console/cmd window 中显示使用 JSch 执行的 SSH 命令的结果,而不是在 IDE 控制台中
Show results of SSH command executed with JSch in separate console/cmd window, not in IDE console
我正在使用 JSch jar 在 Java 中连接到 SSH。并且需要在 cmd 中而不是在 IDE 控制台视图中显示结果。对此有任何指导,可以打开带有结果的命令提示符吗?
为什么我们需要结果 cmd
意味着我们需要截取命令提示符 window 的屏幕截图并发送给客户端。
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class Putty1 {
public static String txt;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws JSchException, FileNotFoundException {
// TODO code application logic here
String host="HostName";
String user="xxxx";
String password="xxxx";
String command1="ls -ltr";
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();
byte[] tmp=new byte[1024];
while(true){
while(in.available()>0){
int i=in.read(tmp, 0, 1024);
if(i<0)break;
txt = new String(tmp, 0, i);
System.out.print(txt);
}
if(channel.isClosed()){
System.out.println("exit-status: "+channel.getExitStatus());
break;
}
try{Thread.sleep(1000);}catch(Exception ee){}
}
channel.disconnect();
session.disconnect();
System.out.println("DONE");
}catch(Exception e){
e.printStackTrace();
}
//System.out.println(txt);
try (PrintWriter out = new PrintWriter("C:\Documents and Settings\arahman9\My Documents\Downloads\jpp\prs72760.txt"))
{
out.println(txt);
}
}
}
输出:
Connected
total 12
drwxrwxrwt 2 arahman9 arahman9 4096 Jul 10 14:30 tmp
drwxr-xr-x 2 arahman9 arahman9 4096 Jul 10 14:30 public_html
drwxr-xr-x 2 arahman9 arahman9 4096 Jul 10 14:30 Documents
exit-status: 0
DONE
BUILD SUCCESSFUL (total time: 2 seconds)
当您在 IDE (Eclipse/Netbeans/etc) 中执行控制台应用程序时,IDE 会将控制台应用程序输出重定向到控制台视图。
但是,如果您 运行 您的应用程序在 cmd.exe
中,它将使用 cmd.exe
控制台。
如果您直接 运行 您的应用程序(通过 java.exe
),例如从 Windows 运行 框中,它将创建自己的控制台 window.
这就是控制台应用程序的行为方式。与您的代码无关。
此外,还有一些技巧可用于使 Eclipse 不将控制台应用程序输出重定向到控制台视图:
https://www.reddit.com/r/eclipse/comments/1bk7a1/how_do_i_run_programs_in_a_separate_console_window/
我正在使用 JSch jar 在 Java 中连接到 SSH。并且需要在 cmd 中而不是在 IDE 控制台视图中显示结果。对此有任何指导,可以打开带有结果的命令提示符吗?
为什么我们需要结果 cmd
意味着我们需要截取命令提示符 window 的屏幕截图并发送给客户端。
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class Putty1 {
public static String txt;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws JSchException, FileNotFoundException {
// TODO code application logic here
String host="HostName";
String user="xxxx";
String password="xxxx";
String command1="ls -ltr";
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();
byte[] tmp=new byte[1024];
while(true){
while(in.available()>0){
int i=in.read(tmp, 0, 1024);
if(i<0)break;
txt = new String(tmp, 0, i);
System.out.print(txt);
}
if(channel.isClosed()){
System.out.println("exit-status: "+channel.getExitStatus());
break;
}
try{Thread.sleep(1000);}catch(Exception ee){}
}
channel.disconnect();
session.disconnect();
System.out.println("DONE");
}catch(Exception e){
e.printStackTrace();
}
//System.out.println(txt);
try (PrintWriter out = new PrintWriter("C:\Documents and Settings\arahman9\My Documents\Downloads\jpp\prs72760.txt"))
{
out.println(txt);
}
}
}
输出:
Connected
total 12
drwxrwxrwt 2 arahman9 arahman9 4096 Jul 10 14:30 tmp
drwxr-xr-x 2 arahman9 arahman9 4096 Jul 10 14:30 public_html
drwxr-xr-x 2 arahman9 arahman9 4096 Jul 10 14:30 Documents
exit-status: 0
DONE
BUILD SUCCESSFUL (total time: 2 seconds)
当您在 IDE (Eclipse/Netbeans/etc) 中执行控制台应用程序时,IDE 会将控制台应用程序输出重定向到控制台视图。
但是,如果您 运行 您的应用程序在 cmd.exe
中,它将使用 cmd.exe
控制台。
如果您直接 运行 您的应用程序(通过 java.exe
),例如从 Windows 运行 框中,它将创建自己的控制台 window.
这就是控制台应用程序的行为方式。与您的代码无关。
此外,还有一些技巧可用于使 Eclipse 不将控制台应用程序输出重定向到控制台视图: https://www.reddit.com/r/eclipse/comments/1bk7a1/how_do_i_run_programs_in_a_separate_console_window/