Thanks a lot,
I try that tomorrow.
Le 28 avr. 2010 à 18:01, Keith Alan Richardson <[email protected]>
a écrit :
> Here is example code. Tested against Linux run OpenSSH.
>
> @ Ehud, I wasn't able to get it to work with openChannel("session").
> Any ideas why?
>
>
>
> import java.io.InputStream;
> import java.io.PipedInputStream;
> import java.io.PipedOutputStream;
>
> import com.jcraft.jsch.Channel;
> import com.jcraft.jsch.JSch;
> import com.jcraft.jsch.Session;
>
> public class SuExample {
> public static void main(String[] arg) throws Exception {
> JSch jsch = new JSch();
>
> String host = "host";
> // user we intially connect as
> String user1 = "user1";
> String password1 = "XXXXXXX";
>
> // user we want to su - to.
> String user2 = "user2";
> String password2 = "YYYYYYY";
>
> Session session = jsch.getSession(user1, host, 22);
> session.setConfig("StrictHostKeyChecking", "no");
>
> session.setPassword(password1);
> session.connect();
>
> Channel channel = session.openChannel("shell");
>
> // create the IO streams to send input to remote session.
> PipedOutputStream commandIO = new PipedOutputStream();
> InputStream sessionInput = new PipedInputStream(commandIO);
> // this set's the InputStream the remote server will read from.
> channel.setInputStream(sessionInput);
>
> // this will have the STDOUT from server.
> InputStream sessionOutput = channel.getInputStream();
>
> // this will have the STDERR from server
> InputStream sessionError = channel.getExtInputStream();
>
> channel.connect();
>
> String command;
> command = "su - " + user2 + "\n";
> commandIO.write(command.getBytes());
> commandIO.flush();
>
> // Read input until we get the 'Password:' prompt
> byte[] tmp = new byte[1024];
> String stdOut = "";
> String stdErr = "";
>
> int i;
>
> while (true) {
> if (sessionError.available() > 0) {
> i = sessionError.read(tmp, 0, tmp.length);
> if (i < 0) {
> System.err.println("input stream closed earlier
> than expected");
> System.exit(1);
> }
> stdErr += new String(tmp, 0, i);
> }
>
> if (sessionOutput.available() > 0) {
> i = sessionOutput.read(tmp, 0, tmp.length);
> if (i < 0) {
> System.err.println("input stream closed earlier
> than expected");
> System.exit(1);
> }
> stdOut += new String(tmp, 0, i);
> }
>
> if (stdOut.contains("assword")) {
> break;
> }
>
> Thread.sleep(1000);
> }
> command = password2 + "\n";
> commandIO.write(command.getBytes());
> commandIO.flush();
>
> // The rest of the commands are just for dem
>
> commandIO.write("whoami\n".getBytes());
> commandIO.flush();
>
> // logout from user2
> commandIO.write("exit\n".getBytes());
> commandIO.write("whoami\n".getBytes());
> // logout from user1
> commandIO.write("exit\n".getBytes());
>
> // read and print output.
> while ((i = sessionOutput.read(tmp, 0, tmp.length)) != -1) {
> System.out.println(new String(tmp, 0, i));
> }
>
> // cleanup.
> commandIO.close();
> sessionInput.close();
>
> channel.disconnect();
> session.disconnect();
>
> }
>
> }
>
>
> On Tue, Apr 27, 2010 at 3:32 PM, Vivien Duparc <[email protected]
> > wrote:
>>
>> Is there no way to connect remote host with a user and when
>> connected made a 'su - user2' command (command that need to give a
>> password) without asking anything to the application user ?
>> I begin to believe it's impossible.
>> Thanks for your help.
>>
>> Le 24 avr. 2010 à 01:47, Keith Alan Richardson <[email protected]
>> om> a écrit :
>>
>> There is no documentation. Look at the examples directory
>>
>> For running commands remotely, even su, look at Exec.java in the
>> examples directory
>>
>> On Apr 23, 2010 2:41 PM, "Vivien Duparc" <[email protected]>
>> wrote:
>>
>> Hi,
>>
>> I'm a new user of api jsch and i have two question please
>>
>> First, is There any doc anywhere ? Or the only help for beginner are
>> examples ?
>>
>> Second, do you know how to execute an su command please ?
>>
>> Thx.
>>
>>
>> ---
>> ---
>> ---
>> ---------------------------------------------------------------------
>> _______________________________________________
>> JSch-users mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/jsch-users
------------------------------------------------------------------------------
_______________________________________________
JSch-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jsch-users