2013-01-08 29 views
7

Lệnh Java - Jsch sudo.Lệnh JSCH sudo su "tty" lỗi

Tôi đang sử dụng Jsch và nhiệm vụ của tôi là để đăng nhập vào máy chủ và chạy lệnh như sau

sudo su - bumboo 

Sử dụng đoạn mã sau tôi thành công có thể kết nối nhưng khi tôi cố gắng để chạy lệnh nó mang lại cho tôi lỗi sudo: sorry, you must have a tty to run sudo

Tiếp theo là mã của tôi

public static Channel sudoBamboo(Session session, String sudo_pass) throws Exception { 

     ChannelExec channel = (ChannelExec) session.openChannel("exec"); 
     //SUDO to bamboo user 
     String command = "sudo su - bumboo"; 
     channel.setCommand(command); 

     //InputStream in = channel.getInputStream(); 
     channel.setInputStream(null, true); 

     OutputStream out = channel.getOutputStream(); 
     //channel.setErrStream(System.err); 
     channel.setOutputStream(System.out, true); 
     channel.setExtOutputStream(System.err, true); 
     //Test change 
     //channel.setPty(false); 
     channel.connect(); 

     out.write((sudo_pass + "\n").getBytes()); 
     out.flush(); 

     return channel; 
    } 

jsch trong sudo.java sẵn ví dụ họ khuyên nên sử dụng

// man sudo 
     // -S The -S (stdin) option causes sudo to read the password from the 
     //  standard input instead of the terminal device. 
     // -p The -p (prompt) option allows you to override the default 
     //  password prompt and use a custom one. 
     ((ChannelExec)channel).setCommand("sudo -S -p '' "+command); 

nhưng khi tôi chạy lệnh như "sudo -S -p - su bamboo" vẫn mang lại cho tôi những lỗi tương tự

bất kỳ sự giúp đỡ đánh giá cao.

Trả lời

10

Nó làm việc cho tôi

String command = "sudo su - bumboo"; 
channel.setPty(true);