1.JSch介绍JSch是SSH2的纯Java实现。
JSch允许您连接到sshd服务器并使用端口转发,X11转发,文件传输等,并且可以将其功能集成到您自己的Java程序中。
2.实现原理根据远程主机的IP地址,用户名和端口,建立会话(Session)设置用户信息(包括密码和Userinfo),然后连接session,getSession只是创建一个session,需要设置必要的认证信息之后,调用connect才能建立连接。设置channel上需要远程执行的Shell脚本,连接channel,就可以远程执行该Shell脚本,调用openChannel(String type) 可以在session上打开指定类型的channel。该channel只是被初始化,使用前需要先调用connect进行连接。可以读取远程执行Shell脚本的输出,然后依次断开channel和session的连接3.代码工程实验目标:实现文件上传到服务,服务器下载文件以及执行服务器命令 pom.xmlspringboot-democom.et1.0-SNAPSHOT4.0.0JSch88org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-autoconfigureorg.springframework.bootspring-boot-starter-testtestcom.jcraftjsch0.1.55org.projectlomboklombokcom.alibabafastjson1.2.78 remote.javapackage com.et.jsch.model;import lombok.Data;@Datapublic class Remote { private String host; private final int port = 22; private String user; private String password; private final String identity = "~/.ssh/id_rsa"; private String passphrase;}
DemoApplication.javapackage com.et.jsch;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}
代码仓库github.com/Harries/spr…4.测试import com.alibaba.fastjson.JSONObject;import com.et.jsch.DemoApplication;import com.et.jsch.model.Remote;import com.et.jsch.util.JSchUtil;import com.jcraft.jsch.JSchException;import com.jcraft.jsch.Session;import org.junit.After;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.InputStream;import java.util.List;@RunWith(SpringRunner.class)@SpringBootTest(classes = DemoApplication.class)public class JSchUtilTests { private Logger log = LoggerFactory.getLogger(getClass); Session session; @Before public void before throws JSchException { Remote remote= new Remote; remote.setHost("xxx.xxx.xxx.xxx"); remote.setUser("root"); remote.setPassword("xxxx"); session= JSchUtil.getSession(remote); } @After public void after{ JSchUtil.disconnect(session); } @Test public void remoteExecute throws JSchException { List list= JSchUtil.remoteExecute(session,"ls"); System.out.println(JSONObject.toJSON(list)); } @Test public void uploadFile throws JSchException, FileNotFoundException { String filestr ="D:\tmp\test\file_utils\file1.txt"; File file = new File(filestr); InputStream in = new FileInputStream(file); String directory="/root/test"; String fileName="test.txt"; boolean flag= JSchUtil.uploadFile(session,in,directory,fileName); System.out.println(flag); } @Test public void deleteFile throws JSchException, FileNotFoundException { String directory="/root/test"; String fileName="test.txt"; boolean flag= JSchUtil.deleteFile(session,directory,fileName); System.out.println(flag); } @Test public void scpFrom throws JSchException, FileNotFoundException { String source="/root/test/file1.txt"; String destination ="D:\tmp\scfFrom.txt"; long filesize= JSchUtil.scpFrom(session,source,destination); System.out.println(filesize); } @Test public void scpTo throws JSchException, FileNotFoundException { String filestr ="D:\tmp\test\file_utils\file1.txt"; String destination="/root/test/file1.txt"; long filesize= JSchUtil.scpTo(session,filestr,destination); System.out.println(filesize); }}
自己尝试一下吧,非常好的一个工具,更多功能可以去官网看例子
转载此文是出于传递更多信息目的。若来源标注错误或侵犯了您的合法权益,请与本站联系,我们将及时更正、删除、谢谢。
https://www.414w.com/read/37240.html