使用jsch以'sudo su-someuser'的身份运行远程脚本

时间:2019-12-16 14:17:43

标签: linux bash shell sudo jsch

我们正在尝试使用JSCH lib在远程服务器上运行脚本。基本上在Linux提示符下,我们按以下方式执行它:

sudo su - batchuser          // enter
<give batchuser password>    // enter
cd /app/local/folder        //enter
./bin/run JobName  // enter, runs the script with job as parameter

我们尝试按照此处CodeFlex的说明进行操作。出现错误,看来sudo不是'S'选项。也可以像此codesandscripts那样尝试,它可以在不需要sudo访问的脚本上正常运行。

我们找不到为此的示例或来源。很少看到堆栈溢出,它们看起来像this

对此将提供任何帮助。

我们尝试的代码:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;


public class codeflex {

    public static void runSudoCommand(String user, String password, String host, String command) {

        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        JSch jsch = new JSch();
        Session session;
        try {
            session = jsch.getSession(user, host, 22);
            session.setPassword(password);
            session.setConfig(config);
            session.connect();
            System.out.println("Connected to " + host);
            Channel channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand("sudo -S -p '' " + command);
            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").getBytes());
            out.flush();
            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()) {
                    System.out.println("Exit status: " + channel.getExitStatus());
                    break;
                }
            }
            channel.disconnect();
            session.disconnect();
            System.out.println("DONE");
        } catch (JSchException | IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] arg) {

        String user = "user";
        String host = "111.111.111.111";
        String password = "password";
        String command = "sudo su - batchuser ./app/local/folder/bin/run JobName";

        runSudoCommand(user, password, host, command);
    }
}

sudo详细信息:

Sudo version 1.8.23
Sudoers policy plugin version 1.8.23
Sudoers file grammar version 46
Sudoers I/O plugin version 1.8.23

如果我按以下方式更新命令,则会出现错误,似乎无法运行“ cd”。

String command = "cd /app/local/folder; ./bin/run JobName";

ERROR:
Sorry, user USER is not allowed to execute '/bin/cd /app/local/folder' as root on server01.
bash: ./bin/run: No such file or directory

0 个答案:

没有答案