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
相关文章推荐
- JSch连接SSH远程执行命令
- Java使用JSch远程连接SSH,模拟sshClient
- centos 创建用户(可ssh连接)
- [github]创建SSH密钥,并连接github
- 创建ssk_key,A机器通过SSH连接到B机器
- 关于夏天出生的人,Flex前端显示出生时间少一个小时和Ubuntu创建ssh远程连接用户
- OpenStack创建虚拟机,用SSH连接到实例
- 【Jsch】使用SSH协议连接到远程Shell执行脚本
- 创建SSH密钥,并连接GitHub
- 创建SSH连接快捷方式
- MyEclipse创建ssh项目和连接数据库
- 创建SSH连接快捷方式
- Putty和Xming创建远程GUI的SSH连接
- 2013.08.05----ssh创建无密码密钥(ssh连接不用输入密码)
- ssh通过nfs挂载,创建软连接,实现免密码登录
- 创建SSH密钥,并连接GitHub
- github上创建ssh连接多个账户
- 新购阿里云服务器ECS创建之后无法ssh连接的问题处理
- 创建SSH密钥,并连接GitHub
- Docker创建可以SSH连接的容器