linux - Java JSch changing user on remote machine and execute command -


i'm trying connect host, change user using "su - john" , execute command john. possible using jsch?

the problem after create session , open channel , execute aforementioned command should request password, nothing happens.

this how connect remote machine:

string address = "myremote.computer.com";  jsch jsch = new jsch(); string user = "tom"; string host = address; string password = "l33tpassw0rd"; session session = jsch.getsession( user, host, 22 ); java.util.properties config = new java.util.properties(); config.put( "stricthostkeychecking", "no" ); session.setconfig( config ); session.setpassword( password );  session.connect(); 

then execute commands via runsshcommand() method looks this:

try {     channel channel = session.openchannel( "exec" );     channel.setinputstream( null );     channel.setoutputstream( system.out );      ( (channelexec) channel ).setcommand( command );      channel.connect();      inputstream in = channel.getinputstream();      byte[] tmp = new byte[1024];     while ( true )     {         while ( in.available() > 0 )         {             int = in.read( tmp, 0, 1024 );             if ( < 0 )             {                 break;             }             system.out.print( new string( tmp, 0, ) );        }        if ( channel.isclosed() )        {            break;        }        try        {            thread.sleep( 1000 );        }        catch ( exception ee )        {        }    }    channel.disconnect(); } catch ( exception e ) {    e.printstacktrace(); } 

do have create channel, when change users, or how make work?

because if use

runsshcommand("su - john",session); runsshcommand("tail -1 ~/mylog.log",session); 

it executes "su" command doesn't finish change of users , afterwards executing "tail" result in error because "tom" hasn't got file :/

basically application connect machine, change user, read 1 file , return data. can shed light, please?

you have several problems here.

first, each channel in ssh connection independent of other ones, , command in each exec channel executed in own shell (command line interpreter). changes doing in 1 channel have no effect @ other channels. can't stuff this:

runsshcommand("cd documents", session); runsshcommand("ls -l", session); 

(actually can this, not show contents of documents directory, of home directory.)

for cd, can work around passing both commands 1 "command", used in same exec channel:

runsshcommand("cd documents; ls -l"); 

(instead of ; can use line break \n separate commands, or whatever else shell accepts.)

for su not work, come second problem.

su not command changes state of current shell (like cd), command opens new shell inside existing one. return outer shell when leave shell started su (e.g. exit, logout or end-of-file), , again same user before.

to pass commands "inner shell", you'll have pass them shells input. or use -c (--command) argument of su:

runsshcommand("su -c 'tail -1 ~/mylog.log' - john ",session); 

you might run in third problem: su ask john's password, , might refuse read standard input, try read terminal. , channel has no pseudo-terminal. can try use cannel.setpty(true) , write password output stream, though i'm not sure work.

alternatives: instead of su -c can use sudo, can configured not ask password commands , users (otherwise you'll have same terminal problem again). or directly log in john, or make logfile readable tom. (also, hope real password better 1 in source code.)


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -