Sending commands to remote server through ssh by Java with JSch -
i'm trying set class can ssh remote server (i have ip, username, , password) , send command "echo "test"" , receive output (e.g., "test"). i'm using jsch don't understand how it.
import com.jcraft.jsch.*; public class connectssh { public int execute (string command) { jsch jsch = new jsch(); string ip = "00.00.00.00; string user = "root"; string pass = "password"; int port = 22; try { session session = jsch.getsession(user, ip, port); session.setpassword(pass); session.connect(); ...
i'm not sure do, i'm stuck after connecting.
any advice appreciated.
try this:
jsch jsch=new jsch(); session session=jsch.getsession(remotehostusername, remotehostname, 22); session.setpassword(remotehostpassword); properties config = new properties(); config.put("stricthostkeychecking", "no"); session.setconfig(config); session.connect(); channelexec channel=(channelexec) session.openchannel("exec"); bufferedreader in=new bufferedreader(new inputstreamreader(channel.getinputstream())); channel.setcommand("pwd;"); channel.connect(); string msg=null; while((msg=in.readline())!=null){ system.out.println(msg); } channel.disconnect(); session.disconnect();
Comments
Post a Comment