通过我的java程序,我能够登录到shell并执行cmds。要登录,我提供主机名,用户名,密码,端口。
我的问题是,我只需要通过发出su命令(以root用户身份登录)更改为root用户,然后在密码提示符下提供rootpassword,如下所示。
support@mygateway:/tmp$ su
Password:
BusyBox v1.17.3 (2020-10-08 06:33:26 CEST) built-in shell (ash)
Enter 'help' for a list of built-in commands.
root@mygateway:~#我可以使用以下方法(已经可用)成功创建SSHShell并执行cmds,而无需根用户访问权限。但是,我如何使用root访问权限登录?
/**
* Creates the SSHShell
*/
private SSHShellConnectionManager(String host, int port, String userName, String password)
throws JSchException, IOException {
Closure expectClosure = getExpectClosure();
for (String linuxPromptPattern : new String[] { "\\>", "#", "~#", "~\\$",">"}) {
try {
Match match = new RegExpMatch(linuxPromptPattern, expectClosure);
linuxPromptMatches.add(match);
} catch (MalformedPatternException malformedEx) {
throw new RuntimeException(malformedEx);
}
}
JSch jsch = new JSch();
this.session = jsch.getSession(userName, host, port);
session.setPassword(password);
Hashtable<String, String> config = new Hashtable<>();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect(60000);
this.channel = (ChannelShell) session.openChannel("shell");
//this.expect = new Expect4j(channel.getInputStream(), channel.getOutputStream());
this.expect = new Expect4j(channel.getInputStream(), channel.getOutputStream());
channel.connect();
}
/**
* Executes a command on the remote host.
*
* @param command
* the command to be executed
* @param getExitStatus
* return the exit status captured in the stdout
* @param withErr
* capture the stderr as part of the output
* @return the content of the remote output from executing the command
* @throws Exception
* exception thrown
*/
public String executeCommand(String command, Boolean getExitStatus, Boolean withErr) throws Exception {
String result = "";
String resultErr = "";
Channel channel = this.session.openChannel("exec");
System.out.println("COMMAND TO EXECUTE ON ROUTER " + command);
((ChannelExec) channel).setCommand(command);
InputStream commandOutput = channel.getInputStream();
InputStream commandErr = ((ChannelExec) channel).getErrStream();
channel.connect();
byte[] tmp = new byte[4096];
while (true) {
while (commandOutput.available() > 0) {
int i = commandOutput.read(tmp, 0, 4096);
if (i < 0) {
break;
}
result += new String(tmp, 0, i);
}
while (commandErr.available() > 0) {
int i = commandErr.read(tmp, 0, 4096);
if (i < 0) {
break;
}
resultErr += new String(tmp, 0, i);
}
if (channel.isClosed()) {
if (commandOutput.available() > 0) {
continue;
}
if (getExitStatus) {
result += "exit-status: " + channel.getExitStatus();
if (withErr) {
result += "\n With error:\n" + resultErr;
}
}
break;
}
try {
Thread.sleep(100);
} catch (Exception ee) {
}
}
channel.disconnect();
System.out.println("COMMAND RESPONSE :" + result);
return result;
}
/**
* Closes shell.
*/
public void close() {
if (expect != null) {
expect.close();
}
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
}发布于 2020-11-03 06:04:52
尝试如下所示:
Channel channel = this.session.openChannel("exec");
((ChannelExec) channel).setCommand("su <<!"); // updated this line
channel.setInputStream(null);
OutputStream out = channel.getOutputStream();
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
((ChannelExec) channel).setPty(true);
channel.connect();
out.write((password + "\n!\n").getBytes()); // updated this line
out.flush();https://stackoverflow.com/questions/64653447
复制相似问题