How to execute linux command “dzdo su - john” through JSch java api and execute some commands like “


I want to connect to the remote linux server using java jsch library and switch to another user using command dzdo su - john and I want to execute some commands on that user. I have tried several ways on this requirement but I am unable to do this could any one help on this.

public static void main(String args[]) { String host="xxxxx.yyyy.com"; String user="user"; String password="password"; String command1="dzdo su - lucy"; try{ java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); JSch jsch = new JSch(); Session session=jsch.getSession(user, host, 22); session.setPassword(password); session.setConfig(config); session.connect(); System.out.println("Connected"); Channel channel=session.openChannel("shell"); OutputStream ops = channel.getOutputStream(); PrintStream ps = new PrintStream(ops, true); channel.connect(); ps.println(command1); ps.println("ls -ltr"); InputStream in=channel.getInputStream(); 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){} } channel.disconnect(); session.disconnect(); System.out.println("DONE"); }catch(Exception e){ e.printStackTrace(); } } }

by executing this code I am getting an output like this and the program is not halting

Connected Last login: Thu Oct 4 13:24:38 2018 from xx.xx.xxx.xx $ dzdo su - lucy xxxx@zr1.xxxx.com:/u/zr1.xxxx.com/lucy $

the command ls -ltr was not executing. The program was going to infinite loop in the statement while(true){---code---}


It might be a timing issue.

Consider adding Thread.sleep between the two println calls.

ps.println(command1); Thread.sleep(1000); ps.println("ls -ltr"); <hr />

Or try disabling PTY:

((ChannelShell)channel).setPty(false); channel.connect();

If that works, it's a more robust solution than the delay.


  • Android SFTP resumable download file
  • how to excute shell script on difference machine using java
  • JSCH execute command on windows machine and take the output
  • Secure FTP using private key authentication in java
  • Conversion of my alfresco javascript file into java class [duplicate]
  • How to verify that data source is valid and configured properly?
  • play 2.1.1: Unable to rollback transaction with ebean orm
  • JSF View- returning null on actions do not update the view
  • How deserialize Byte array to object - Windows 8 / WP 8
  • JavaScript draw a circle on mouse click - cordinates don't match
  • Apache HttpClient able to communicate over HTTPS when DIRECT but not via PROXY error: javax.net.ssl.
  • overload virtual function with different parameters in c++
  • SpringSession DefaultCookieSerializer.setJvmRoute works, but HttpServletRequest does not have the jv
  • Splitting on comma outside quotes when escaped quotes exist
  • array from php to JavaScript
  • Go template remove the last comma in range loop
  • How do you split data from one column into two?
  • Produce a comma separated list using StringBuilder
  • enumerate days of week in t-sql
  • Session management in GWT client side
  • Restructure php contact form
  • Plot a CSV file where the delimiter is '; ' (semicolon + space)
  • How to remove comma or any characters from Python dataframe column name
  • Creating JS objects in PHP with commas in between
  • Returning the auto incrementing value after an insert using slick
  • hibernate sets dirty flag (and issues update) even though client did not change value
  • NHibernate manually control fetching
  • Unable to send e-mail through Java
  • Salesforce Different WSDL files and when to use
  • SQLite connection strategies
  • Javascript, Regex - I need to grab each section of a string contained in brackets
  • How can I extract results of aggregate queries in slick?
  • Change Inet root folder for iis 7
  • Java: can you cast Class into a specific interface?
  • Debugging ASP.NET on a built-in web server suddenly stops
  • AES padding and writing the ciphertext to a disk file
  • Updating server-side rendering client-side
  • How to extract text from Word files using C#?
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • How to get NHibernate ISession to cache entity not retrieved by primary key