[英]Executing sudo using SSH "exec" channel in JSch
I am using file in which I passed below commands:我正在使用我在其中传递以下命令的文件:
hostname
pwd
pbrun su - fclaim
whoami
cd ..
pwd
Ad the Java code below:在下面添加 Java 代码:
for (String command1 : commands) {
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command1);
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;
System.out.println(new String(tmp, 0, i));
}
if(channel.isClosed()){
break;
}
}
channel.setInputStream(null);
channel.disconnect();
}
But I'm getting this output:但我得到这个输出:
/home/imam
imam
/home/imam
Your code executes each command in an isolated environment.您的代码在隔离环境中执行每个命令。 So your second
whoami
does not run within pbrun su
, as you probably hoped for.所以你的第二个
whoami
不会像你希望的那样在pbrun su
运行。
The pbrun su
executes a new shell. pbrun su
执行一个新的 shell。
To provide a command to the shell you either:要向 shell 提供命令,您可以:
specify the command on su
command-line, like the official JSch Sudo.java
example shows:在
su
命令行上指定命令,如官方 JSch Sudo.java
示例所示:
((ChannelExec)channel).setCommand("pbrun su - fclaim -c whoami");
or feed the command to the shell using its standard input:或使用其标准输入将命令提供给 shell:
OutputStream out = channel.getOutputStream(); out.write(("whoami\\n").getBytes());
See also:也可以看看:
Executing multiple bash commands using a Java JSch program after sudo login and在 sudo 登录后使用 Java JSch 程序执行多个 bash 命令并
Running command after sudo login . sudo login 后运行命令。
In general, I recommend the first approach as it uses a better defined API (command-line argument).一般来说,我推荐第一种方法,因为它使用更好定义的 API(命令行参数)。
I had a similar issue and got it resolved with some help from @Martin
, lot of R&D and scanning through SO links.我遇到了类似的问题,并在
@Martin
帮助下解决了这个问题,通过 SO 链接进行了大量的研发和扫描。 Here is the program that worked for me.这是对我有用的程序。
public class SSHConn {
static Session session;
static String suCmds = "su - simba -c \"whoami ; pwd\"";
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("su -")) {
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");
}
}
Output:输出:
Connecting SSH to my-unix-box.net - Please wait for few seconds...
Connected!
Executing command: whoami
Capturing cmdOutput now...
john
Executing command: su - simba -c "whoami ; pwd"
Setting suPasswd now....
Flushed suPasswd to cli...
Capturing cmdOutput now...
simba
/home/simba
Disconnected channel and session
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.