java - How to execute Multiple commands -
i want to:
- log in putty using hostname, username, password , port number. have achieved.
- once logged in, want connect server1. in putty connect using ssh command (ssh user@server1).
- once connected server.i need run multiple commands like:
df -kh ps -ef|grep www
- and after executing above commands, need log out server1 , need log in server2.
how can in jsch?
jsch jsch=new jsch(); session session=jsch.getsession(remotehostusername, remotehostname, remotehostportno); session.setpassword(remotehostpassword); properties config = new properties(); config.put("stricthostkeychecking", "no"); session.setconfig(config); system.out.println("please wait..."); session.connect(); system.out.println("connected "+remotehostusername+"@"+remotehostname); channelexec channel=(channelexec) session.openchannel("shell"); bufferedreader in=new bufferedreader(new inputstreamreader(channel.getinputstream())); channel.setcommand("df -kh"); channel.setcommand("pwd"); channel.connect();
try channelshell channel = (channelshell) session.openchannel("shell");
setup inputstream
, outputstream
, subsequently perform following loop:
- write connected inputstream , flush it
- read connected outputstream
this way can construct second commands based on outcome of first one.
Comments
Post a Comment