作者:我在竹苑写代码
阅读时长:8分钟
技术预备:shell命令、maven
Java使用Ganymed的SSH封装类库,连接Linux服务器变得十分简单,只需几行代码就能完成连接,鉴权操作交给Ganymed接管,但Ganymed的源码没有注释,API的使用需要慢慢琢磨,沉下心来解读它的每个类负责什么。
一、如何引入Genymed?
1.1 maven工程在pom文件中添加红框内的依赖
1.2 非maven管理的工程,下载jar包手动添加都工程
二、如何使用Genymed-SSH连接Linux?
2.1 预先准备服务器的连接信息
2.2 连接linux服务器,并进入【/home】目录创建【Genymed】文件夹
2.3 Session.execCommand()的API讲解execCommand()方法一次可以执行多条shell命令,每条命令以英文的分号作为结束符;例如执行进入【/home】目录创建note.txt文件,并复制到【/usr/local】目录,则代码为execCommand("cd /home; touch note.txt; cp note.txt /usr/local")。
【/home】目录已创建【note.txt】文件
【/home】目录的【note.txt】文件已备份到【/usr/local】目录 2.4 最后在java方法体的末尾 关闭Connection对象三、Java连接Linux的应用场景
3.1 客户端上传的文件需要做备份,执行cp命令
3.2 A服务器定时扫描B服务器生成的mysql.txt文件,执行scp命令拷贝该文件到A服务器
3.3 查看linux服务器cpu、内存、硬盘灯资源使用情况
----- End -----
更多好文
请扫描下面二维码
欢迎关注~