Executing multiple bash commands using a Java JSch program after sudo login

后端 未结 1 683
庸人自扰
庸人自扰 2020-12-20 07:25

I am trying to execute multiple bash commands through a Java program which connects to a SSH using JSch. But after sudo login, I am unable to execute a

1条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-20 08:00

    The sudo does not execute a new shell. But probably your start_shell.sh script does. You probably refer to sudo su. Maybe your script runs the su?

    Anyway, to provide commands to the shell, feed the commands using shell's standard input:

    ChannelExec channel = (ChannelExec) session.openChannel("exec");
    channel.setCommand("sudo su");
    channel.connect();
    OutputStream out = channel.getOutputStream();
    out.write(("command1\n").getBytes());
    out.write(("command2\n").getBytes());
    out.flush();
    

    The sudo/su are commands as any other, so it's actually the same as a very generic question:
    Providing input/subcommands to command executed over SSH with JSch

    Also see Running command after sudo login, which answers a more generic question on sudo su, without an unclear use of some unknown shell script.

    0 讨论(0)
提交回复
热议问题