java ssh2 试卷_JAVA-SSH2:JSch试用

试用开源软件JSch实现通过SSH2协议,在JAVA程序内远程执行linux命令。

package ssh;

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.nio.charset.Charset;

import com.jcraft.jsch.Channel;

import com.jcraft.jsch.ChannelExec;

import com.jcraft.jsch.JSch;

import com.jcraft.jsch.Session;

public class JSchDemo

{

public static void main(String[] args) throws Exception

{

String charset = "UTF-8";

String user = "root";

String passwd = "123456";

String host = "40.40.40.50";

String command = "ls -l";

JSch jsch = new JSch();

Session session = jsch.getSession(user, host, 22);

session.setPassword(passwd);

java.util.Properties config = new java.util.Properties();

config.put("StrictHostKeyChecking", "no");

session.setConfig(config);

session.connect();

Channel channel = session.openChannel("exec");

((ChannelExec) channel).setCommand(command);

channel.setInputStream(null);

((ChannelExec) channel).setErrStream(System.err);

channel.connect();

InputStream in = channel.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charset.forName(charset)));

String buf = null;

while ((buf = reader.readLine()) != null)

{

System.out.println(buf);

}

reader.close();

channel.disconnect();

session.disconnect();

}

}

已标记关键词 清除标记
表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页