英文:
Running linux suu command from Java - After running the suu command not able to run any other commands
问题
以下是您提供的内容的翻译部分:
在我从Java运行下面的脚本之后 -
suu -t -r "reason" <user_name>
它提示我输入密码,我输入了密码。
之后,程序正在运行,但没有执行任何进一步的命令。
我想通过Java(Jsch库)在Linux机器上模拟身份。我没有收到任何错误消息,但在输入密码后,它成功登录,然后不执行任何命令。
这是我遵循的链接以编写代码 -
https://linuxconfig.org/executing-commands-on-a-remote-machine-from-java-with-jsch
在运行suu命令之后,我想读取一个文件。如果不运行suu,我将无法访问该路径。对于这个问题的任何帮助将不胜感激
这是我的代码片段-
[![enter image description here][1]][1]
提前感谢!
以下是我使用的代码 -
public class SSHConn {
static Session session;
static String suCmds = "suu -t -u \"reason\" simba";
static String[] commands = {"whoami", suCmds};
public static void main(String[] args) throws Exception {
open();
runCmd(commands);
close();
}
public static void runCmd(String[] commands) throws JSchException, IOException {
for (String cmd : commands) {
System.out.println("\n执行命令: " + cmd);
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(cmd);
InputStream in = channel.getInputStream();
OutputStream out = channel.getOutputStream();
channel.connect();
//仅在切换用户时传递凭据
if (cmd.startsWith("suu -")) {
System.out.println("现在设置suPasswd....");
out.write((Constants.suPasswd + "\n").getBytes());
out.flush();
System.out.println("将suPasswd刷新到cli...");
}
captureCmdOutput(in, channel);
channel.setInputStream(null);
channel.disconnect();
}
}
public static void captureCmdOutput(InputStream in, Channel channel) throws IOException {
System.out.println("现在捕获cmdOutput...");
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0) {
break;
}
System.out.print(new String(tmp, 0, i));
}
if (channel.isClosed()) {
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
System.out.println(ee.getMessage());
}
}
}
public static void open() throws JSchException {
JSch jSch = new JSch();
session = jSch.getSession(Constants.userId, Constants.host, 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword(Constants.userPasswd);
System.out.println("连接SSH到 " + Constants.host + " - 请等待几秒钟... ");
session.connect();
System.out.println("已连接!\n");
}
public static void close() {
session.disconnect();
System.out.println("\n关闭通道和会话");
}
}
希望这对您有所帮助。
英文:
After I run below script from java-
suu -t -r "reason" <user_name>
It prompted me for password and I entered it.
After that the program was running but not executing any further commands.
I want to impersonate on Linux machine through Java (Jsch library). I am not getting any errors, but after the password is given, Its successfully logging in and then no commands are being executed.
This is the link I followed to write code-
https://linuxconfig.org/executing-commands-on-a-remote-machine-from-java-with-jsch
After running suu command, I want to read a file. Without running suu, I will not have access to that path. Any help for this issue will be more appreciated
Here is my snippet-
[![enter image description here][1]][1]
Thanks in advance!
Below is code I used-
public class SSHConn {
static Session session;
static String suCmds = "suu -t -u \"reason\" simba";
static String[] commands = {"whoami", suCmds};
public static void main(String[] args) throws Exception {
open();
runCmd(commands);
close();
}
public static void runCmd(String[] commands) throws JSchException, IOException {
for (String cmd : commands) {
System.out.println("\nExecuting command: " + cmd);
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(cmd);
InputStream in = channel.getInputStream();
OutputStream out = channel.getOutputStream();
channel.connect();
//passing creds only when you switch user
if (cmd.startsWith("suu -")) {
System.out.println("Setting suPasswd now....");
out.write((Constants.suPasswd + "\n").getBytes());
out.flush();
System.out.println("Flushed suPasswd to cli...");
}
captureCmdOutput(in, channel);
channel.setInputStream(null);
channel.disconnect();
}
}
public static void captureCmdOutput(InputStream in, Channel channel) throws IOException {
System.out.println("Capturing cmdOutput now...");
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0) {
break;
}
System.out.print(new String(tmp, 0, i));
}
if (channel.isClosed()) {
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
System.out.println(ee.getMessage());
}
}
}
public static void open() throws JSchException {
JSch jSch = new JSch();
session = jSch.getSession(Constants.userId, Constants.host, 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword(Constants.userPasswd);
System.out.println("Connecting SSH to " + Constants.host + " - Please wait for few seconds... ");
session.connect();
System.out.println("Connected!\n");
}
public static void close() {
session.disconnect();
System.out.println("\nDisconnected channel and session");
}}
专注分享java语言的经验与见解,让所有开发者获益!
评论