2013-06-27 67 views
5

我的要求如下:
我必须使用我的凭据登录到Unix机箱,并且一旦登录,我必须为不同的用户执行sudo。一旦sudo成功,我不得不在nohup中调用shell。完成执行后,关闭通道和会话。使用JSch的多个命令

我尝试了使用sudo命令连接的第一步,但我不知道如何在sudo命令后调用shell脚本。

在下面的代码中,我可以执行sudo命令,但是在获得sudo访问权后,如何在用户masteruser的nohup中执行shell。所以创建我的外壳所需的文件的所有者为masteruser

public class SSHUploader { 

    Session session = null; 

    public SSHUploader(){ 

    } 

    public void connect(){ 
    try { 

      JSch jsch = new JSch(); 
      session = jsch.getSession("user", "xxx.xxx.xx.xx", 22); 
      session.setPassword("test"); 
      java.util.Properties config = new java.util.Properties(); 
      config.put("StrictHostKeyChecking", "no"); 
      session.setConfig(config); 
      session.connect(); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 

    public void executeCommand(String script) throws JSchException, IOException{ 
     System.out.println("Execute sudo"); 
     String sudo_pass = "test"; 
     ChannelExec channel = (ChannelExec) session.openChannel("exec"); 
     ((ChannelExec) channel).setCommand(script); 

     InputStream in = channel.getInputStream(); 
     OutputStream out = channel.getOutputStream(); 
     ((ChannelExec) channel).setErrStream(System.err); 

     channel.connect(); 
     out.write((sudo_pass + "\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; 
      } 
      try { 
       Thread.sleep(1000); 
      } catch (Exception ee) { 
       System.out.println(ee); 
      } 
     } 
     channel.disconnect(); 
     System.out.println("Sudo disconnect"); 
    } 

    public void disconnect(){ 
     session.disconnect(); 
    } 


    public static void main(String... args) throws JSchException, IOException { 

     SSHUploader up = new SSHUploader(); 
     up.connect(); 

     up.executeCommand("sudo -u masteruser bash"); 

     up.disconnect(); 
    } 

} 
+0

[如何使用JSch执行多个操作](https://stackoverflow.com/questions/7419513/how-to-perform-multiple-operations-with-jsch) – Gregor

回答

12

按顺序执行多个命令,你可以像下面创建一个命令字符串:

String script ="pbrun su - user; cd /home/scripts;./sample_script.sh” 

执行它,并通过这个字符串上面的方法。

+0

太棒了。我浪费了很多时间来缓存SSH客户端/连接,只能在一分钟之内解决它,谢谢你。谢谢! – Buffalo

+0

此外,添加“echo Running script_X; ./script_X”似乎很适合使用多个命令。 – Buffalo

+0

请注意,这是* nix shell特定的解决方案 - 既不是SSH也不是JSch功能 - 因此在其他系统(如Windows)上,可能需要不同的语法。 +1 –

0

这篇文章可能比较陈旧,但我发现了另一种简单的方法,可以让您分别检索每个命令的输出。请注意,此代码具有一旦会话已经被打开将被执行,如实施例中所示(http://www.jcraft.com/jsch/examples/Exec.java.html):

for (String command : commands) { 
      ChannelExec channel = (ChannelExec) session.openChannel("exec"); 
      channel.setInputStream(null); 
      channel.setErrStream(System.err); 
      channel.setCommand(command); 
      channel.connect(); 
      printOutput(channel); 
      channel.disconnect(); 
     } 

printOutput使用channel.getInpuStream()读取命令的结果。