2023年12月9日发(作者:伴玩)
java如何连续执行多条cmd命令
java连续执行多条cmd命令
命令之间用&连接
例如:
Process p = time().exec("cmd /c d: & cd bin/");
java ssh登录执行多条cmd命令
java登录ssh执行多条命令,解决出现more自动回车的问题
import ption;
import tream;
import Stream;
import ortedEncodingException;
import ager;
import ;
import l;
import ;
import ception;
import n;
import oardInteractive;
import fo;
public class JavaSsh implements Runnable {
protected Logger logger = ger();
/**
* 退格
*/
private static final String BACKSPACE = new String(new byte[] { 8 });
/**
* ESC
*/
private static final String ESC = new String(new byte[] { 27 });
/**
* 空格
*/
private static final String BLANKSPACE = new String(new byte[] { 32 });
/**
* 回车
*/
private static final String ENTER = new String(new byte[] { 13 });
/**
* 某些设备回显数据中的控制字符
*/
private static final String[] PREFIX_STRS = { BACKSPACE + "+" + BLANKSPACE + "+" + BACKSPACE + "+",
"(" + ESC + "[d+[A-Z]" + BLANKSPACE + "*)+" };
private int sleepTime = 200;
/**
* 连接超时(单次命令总耗时)
*/
private int timeout = 4000;
/**
* 保存当前命令的回显信息
*/
protected StringBuffer currEcho;
/**
* 保存所有的回显信息
*/
protected StringBuffer totalEcho;
private String ip; private int port; private String endEcho = "#,?,>,:"; private String moreEcho = "---- More ----"; private String moreCmd = BLANKSPACE; private JSch jsch = null; private Session ssion; private Channel channel;
@Override public void run() { InputStream is; try { is = utStream(); String echo = readOneEcho(is); while (echo != null) { (echo); String[] lineStr = ("n"); if (lineStr != null && > 0) { String lastLineStr = lineStr[ - 1]; if (lastLineStr != null && f(moreEcho) > 0) { (e(lastLineStr, "")); } el { (echo); } } echo = readOneEcho(is); } } catch (IOException e) { tackTrace(); } }
protected String readOneEcho(InputStream instr) { byte[] buff = new byte[1024]; int ret_read = 0; try { ret_read = (buff); } catch (IOException e) { return null; } if (ret_read > 0) { String result = new String(buff, 0, ret_read); for (String PREFIX_STR : PREFIX_STRS) { result = eFirst(PREFIX_STR, ""); } try { return new String(es(), "GBK"); } catch (UnsupportedEncodingException e) { tackTrace(); return null; } } el { return null; } }
public JavaSsh(String ip, int port, String endEcho, String moreEcho) { = ip; = port; if (endEcho != null) { o = endEcho; } if (moreEcho != null) { ho = moreEcho; } totalEcho = new StringBuffer(); currEcho = new StringBuffer(); }
private void clo() { if (ssion != null) { nect(); } if (channel != null) { nect(); } }
private boolean login(String[] cmds) { String ur = cmds[0]; String passWord = cmds[1]; jsch = new JSch(); try { ssion = sion(ur, , ); sword(passWord); UrInfo ui = new SSHUrInfo() { public void showMessage(String message) { }
public boolean promptYesNo(String message) { return true; } }; rInfo(ui); t(30000); channel = annel("shell"); t(3000); new Thread(this).start(); try { (sleepTime); } catch (Exception e) { } return true; } catch (JSchException e) { return fal; } }
protected void ndCommand(String command, boolean ndEnter) { try { OutputStream os = putStream(); (es()); (); if (ndEnter) { currEcho = new StringBuffer(); (es()); (); } } catch (IOException e) { tackTrace(); } }
protected boolean containsEchoEnd(String echo) { boolean contains = fal; if (endEcho == null || ().equals("")) { return contains; } String[] eds = (","); for (String ed : eds) { if (().endsWith(ed)) { contains = true; break; } } return contains; }
private String runCommand(String command, boolean ifEnter) { currEcho = new StringBuffer(); ndCommand(command, ifEnter); int time = 0; if (endEcho == null || ("")) { while (ng().equals("")) { try { (sleepTime); time += sleepTime; if (time >= timeout) { break; } } catch (InterruptedException e) { tackTrace(); } } } el { while (!containsEchoEnd(ng())) { try { (sleepTime); } catch (InterruptedException e) { tackTrace(); } time += sleepTime; if (time >= timeout) { break; } String[] lineStrs = ng().split("n"); if (lineStrs != null && > 0) { if (moreEcho != null && lineStrs[ - 1] != null && lineStrs[ - 1].contains(moreEcho)) { ndCommand(moreCmd, fal); ("n"); time = 0; continue; } } } } return ng(); }
private String batchCommand(String[] cmds, int[] othernEenterCmds) { StringBuffer sb = new StringBuffer(); for (int i = 2; i < ; i++) { String cmd = cmds[i]; if (("")) { continue; } boolean ifInputEnter = fal; if (othernEenterCmds != null) { for (int c : othernEenterCmds) { if (c == i) { ifInputEnter = true; break; } } } cmd += (char) 10; String resultEcho = runCommand(cmd, ifInputEnter); (resultEcho); } clo(); return ng(); }
public String executive(String[] cmds, int[] othernEenterCmds) { if (cmds == null || < 3) { ("{} ssh cmds is null", ); return null; } if (login(cmds)) { return batchCommand(cmds, othernEenterCmds); } ("{} ssh login error", ); return null; }
private abstract class SSHUrInfo implements UrInfo, UIKeyboardInteractive { public String getPassword() { return null; }
public boolean promptYesNo(String str) { return true; }
public String getPassphra() { return null; }
public boolean promptPassphra(String message) {
return true;
}
public boolean promptPassword(String message) {
return true;
}
public void showMessage(String message) {
}
public String[] promptKeyboardInteractive(String destination, String name, String instruction, String[] prompt,
boolean[] echo) {
return null;
}
}
public static void main(String[] args) {
String ip = "192.168.0.238";
int port = 22;
JavaSsh JavaSsh2 = new JavaSsh(ip, port, null, null);
String urname = "ssh";
String password = "";
String[] cmds = { urname, password, "display mac-address", "display mac-address" };
String executive = ive(cmds, null);
n(executive);
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
本文发布于:2023-12-09 21:20:44,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/zhishi/a/170212804440123.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:java如何连续执行多条cmd命令.doc
本文 PDF 下载地址:java如何连续执行多条cmd命令.pdf
留言与评论(共有 0 条评论) |