您的位置:首页 > Web前端 > JavaScript

Jsch 创建ssh连接

2017-10-30 13:30 260 查看
以下代码是根据Jsch0.1.53.jar来实现 ssh连接,进行通信。

package com.ssh.jsch;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.HostKey;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
/**
*
* @author gfw2306
*
*/
public class JschTest {
//匹配结束符
String pattern = "<.+>";

InputStream is = null;
OutputStream os = null;
String username = "username";
String password = "ts-fwkf";
String host = "1**.6.4.2";
int jschTimeOut = 100;

String command = "";
int port = 22;
JSch jsch = null;
Session session = null;

public JschTest(){

}

public JschTest(String pattern, String username, String password,
String host, int port, int jschTimeOut) {
super();
this.pattern = pattern;
this.username = username;
this.password = password;
this.host = host;
this.port = port;
this.jschTimeOut = jschTimeOut;
}

/**
* 初始化信息
* @throws Exception
*/
public void init() throws Exception{
jsch = new JSch();
session = jsch.getSession(username, host, port);
//session.setUserInfo(new SshUserInfo(password));
session.setPassword(password);
session.setConfig("userauth.gssapi-with-mic", "no");
session.setConfig("StrictHostKeyChecking", "no");  // 第一次访问服务器时不用输入yes
session.setTimeout(jschTimeOut);//设置连接超时时间
session.connect();
Channel channel = session.openChannel("shell");
channel.connect();
is = channel.getInputStream();
os = channel.getOutputStream();
HostKey hostKey = session.getHostKey();
System.out.println(hostKey.getHost());
}

/**
* 发送消息
* @param command
* @throws java.lang.Exception
*/
public void conSend(String command) throws java.lang.Exception{
if(os == null){
throw new Exception("OutputStream Object is null");
}
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
if(!command.equals(" ")){
bw.write(command + "\r\n");  //  "\n" 或  "\r\n"
}else{
bw.write(command);
}
bw.flush();
//Thread.sleep(2000);
}

/**
* 接收消息,目前不好用
* @throws IOException
*/
public void receive() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
if(is.available()>0){
StringBuilder message = new StringBuilder();
String readLine = "";
while((readLine = br.readLine()) != null){
System.out.println("readLine:"+readLine);
message.append(readLine);
/*if(readLine.equals("")){
break;
}*/
}
System.out.println("总的信息:"+message.toString());

}
}

/**
* 接收ssh指令执行后结果
* @throws IOException
*/
public void receive2() throws IOException{
String message = "";
boolean isPattern = false;
//StringBuilder allMessage = new StringBuilder();

while(!isPattern){
if(is.available()>0){
//StringBuilder message = new StringBuilder();
//String readLine = "";
byte[] bytes = new byte[4096];

int read = is.read(bytes, 0, bytes.length);

message = new String(bytes,0,read,"UTF-8");
System.out.println(message);
//allMessage.append(message);
//System.out.println("总的信息:"+message.toString());
//是否匹配到结束符
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(message.toString());
if(m.find()){
isPattern = true;
}
}
try {
Thread.sleep(2000);
System.out.println("读取时睡了2秒...");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public void close(){
if(is != null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(os != null){
try {
os.close();

c2eb
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(session != null){
session.disconnect();
}

}

public static void main(String[] args) throws JSchException, IOException {
String command = "";
String pattern = "";
String username = "";
String password = "";
String host = "";
int port = 22;
int jschTimeOut = 0;
InputStream ins = JschTest.class.getClassLoader().getResourceAsStream("com/ssh/jsch/device.properties");
//BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("src/com/ssh/jsch/device.properties")));
Properties p = new Properties();
try {
p.load(ins);
pattern = p.getProperty("pattern");
username = p.getProperty("username");
password = p.getProperty("password");
host = p.getProperty("host");
port = Integer.parseInt(p.getProperty("port"));
command = p.getProperty("command");
jschTimeOut = Integer.parseInt(p.getProperty("jschTimeOut"));
//System.out.println(p.getProperty("username"));
} catch (IOException e) {
e.printStackTrace();
}

JschTest ssh = new JschTest(pattern, username, password, host, port,jschTimeOut);
try {
ssh.init();
ssh.receive2();
ssh.conSend(command);
ssh.receive2();
ssh.close();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}


配置文件

pattern=<.+>
username = username
password = password
host = 10.*.*.*
command = show system1
port = 22
jschTimeOut = 30000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ssh 通信 jsch ssh2