Я получаю вывод из соединения ssh, но я хочу отправить команду, используя текстовое поле, и получить последний результат в текстовом поле. Панель управления сервером (scp) должна принимать входные данные из текстового поля, а подтверждение должно поступать в диалоговое окно с панели управления сервером.
public class SCpCon {
private static Session session;
private static ChannelShell channel;
private static String username = "xxx";
private static String password = "xxx";
private static String hostname = "xxx";
private static Session getSession(){
if(session == null || !session.isConnected()){
session = connect(hostname,username,password);
}
return session;
}
private static ChannelShell getChannel(){
if(channel == null || !channel.isConnected()){
try{
channel = (ChannelShell)getSession().openChannel("shell");
channel.connect();
}catch(Exception e){
System.out.println("Error while opening channel: "+ e);
}
}
return channel;
}
private static Session connect(String hostname, String username, String password){
JSch jSch = new JSch();
try {
session = jSch.getSession(username, hostname, 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword(password);
System.out.println("Connecting SSH to " + hostname + " - Please wait for few seconds... ");
session.connect();
System.out.println("Connected!");
}catch(Exception e){
System.out.println("An error occurred while connecting to "+hostname+": "+e);
}
return session;
}
private static void executeCommands(List<String> commands){
try{
ChannelShell channel=getChannel();
System.out.println("Sending commands...");
sendCommands(channel, commands);
readChannelOutput(channel);
System.out.println("Finished sending commands!");
}catch(Exception e){
System.out.println("An error ocurred during executeCommands: "+e);
}
}
private static void sendCommands(ChannelShell channel2, List<String> commands){
try{
PrintStream out = new PrintStream(channel2.getOutputStream());
out.println("#!/bin/bas");
for(String command : commands){
out.println(command);
}
out.println("exit");
out.flush();
}catch(Exception e){
System.out.println("Error while sending commands: "+ e);
}
}
private static void readChannelOutput(ChannelShell channel2){
byte[] buffer = new byte[1024];
try{
InputStream in = channel2.getInputStream();
String line = "";
while (true){
while (in.available() > 0) {
int i = in.read(buffer, 0, 1024);
if (i < 0) {
break;
}
line = new String(buffer, 0, i);
System.out.println(line);
}
if(line.contains("logout")){
break;
}
if (channel2.isClosed()){
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee){}
}
}catch(Exception e){
System.out.println("Error while reading channel output: "+ e);
}
}
public static void close(){
channel.disconnect();
session.disconnect();
System.out.println("Disconnected channel and session");
}
public static void main(String[] args){
List<String> commands = new ArrayList<String>();
commands.add("scp");
executeCommands(commands);
close();
}
}
Connecting SSH to xxx - Please wait for few seconds...
Connected!
Sending commands...
#!/bin/bas
scp
exit
Environment 'xxx' set.
root:xxx@xxx:/root> #!/bin/bas
root:xxx@xxx:/root>
root:xxx@xxx:/root> scp
user:
Я ожидаю, что ввод, полученный пользователем, и вывод, т. Е. «Панель управления сервером», должен отображаться в диалоговом окне или текстовом поле.