远程文件解析-j2ssh

j2ssh 是集成ssh、sftp的java的工具包,利用j2ssh基本上可以完成你在服务器操作的所有命令。今天只就sftp讲解。

maven 依赖包引入

<dependencies>
        <!-- https://mvnrepository.com/artifact/net.sf.sshapi/sshapi-j2ssh -->
        <dependency>
            <groupId>net.sf.sshapi</groupId>
            <artifactId>sshapi-j2ssh</artifactId>
            <version>1.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

    </dependencies>

注意:如果依赖出现问题请加入下面的repo

    <repositories>
        <repository>
            <id>j2ssh</id>
            <url>http://artifactory.javassh.com/opensource-releases/</url>
        </repository>
    </repositories>

j2ssh 连接

        SshClient client = new SshClient();//初始化ssh

        client.connect(ip, 22,new IgnoreHostKeyVerification()); //可以用来测试主机的联通性 
        // 设置用户名和密码
        PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
        pwd.setUsername("root");
        pwd.setPassword("root");
//        client.
        int result = client.authenticate(pwd);//登录
        if (result == AuthenticationProtocolState.COMPLETE) {// 如果连接完成
        //todo  需要完成的操作
        }

获取sftp 客户端

sftp获取的客户端有两种channel和client,client的内部基本有channel完成,因为这边需要sftp的基础服务所以使用了channel

 ftpSubsystemClient sftpClient = client.openSftpChannel()

获取sftpfile

SftpFile sftpFile = sftpclient.openFile("/root/zookeeper.out", 1);//获取文件
SftpFile dirFile =sftpClient.openDirectory("/root"); //获取文件夹

判断sftpFile是否为文件夹

sftpFile.isDirectory()//不能使用openDirectory判断是否为文件夹,因为如果为openDir创建的sftpFile如果为文件会报找不到文件异常

遍历sftpFile

SftpFile writeFile =sftpClient.openDirectory("/root");
List<SftpFile> list = new ArrayList();
sftpClient.listChildren(writeFile,list);

读取sftpFile的文件内容

SftpFile sftpFile = client.openSftpChannel().openFile("/root/zookeeper.out", 1);
            SftpFileInputStream sftpFileInputStream = new SftpFileInputStream(sftpFile);
            BufferedReader br = new BufferedReader(new InputStreamReader(sftpFileInputStream));
            while(br.readLine() != null){
                String line = br.readLine();
                System.out.println(line);
            }

完整代码

package test.ssh2;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
import com.sshtools.j2ssh.sftp.SftpFile;
import com.sshtools.j2ssh.sftp.SftpFileInputStream;
import com.sshtools.j2ssh.sftp.SftpFileOutputStream;
import com.sshtools.j2ssh.sftp.SftpSubsystemClient;
import com.sshtools.j2ssh.transport.IgnoreHostKeyVerification;

public class GetMes {

    public void getMessage(String ip) throws IOException {
        SshClient client = new SshClient();

        client.connect(ip, 22,new IgnoreHostKeyVerification());
        // 设置用户名和密码
        PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
        pwd.setUsername("root");
        pwd.setPassword("root");
//        client.
        int result = client.authenticate(pwd);
        if (result == AuthenticationProtocolState.COMPLETE) {// 如果连接完成
            SftpSubsystemClient sftpClient = client.openSftpChannel();
            SftpFile sftpFile = sftpClient.openFile("/root/zookeeper.out", 1); //不知道为什么要用1
            SftpFileInputStream sftpFileInputStream = new SftpFileInputStream(sftpFile);
            BufferedReader br = new BufferedReader(new InputStreamReader(sftpFileInputStream));
            while(br.readLine() != null){
                String line = br.readLine();
                System.out.println(line);
            }
//            client.openSftpClient().put();

            SftpFile writeFile =sftpClient.openDirectory("/root");

            System.out.println(writeFile.isDirectory());
//            client.openSftpClient().ls()
            List<SftpFile> list = new ArrayList();
            sftpClient.listChildren(writeFile,list);
            boolean b = sftpClient.openFile("/root",1).isDirectory();
            System.out.println(b);
            SftpFile testFile = sftpClient.openFile("/root/test1",26); //还不知道为什么用26
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new SftpFileOutputStream(testFile)));
//            SftpFileOutputStream sftpOs = new SftpFileOutputStream(writeFile);
            bw.write("afdsafdasjfll;;;jfsdfsjakjkfdjklsklldfjdfjoaosfojwioekjeklqklekldsssssssmdssdfklsalkfdklakopfdsadfaskd");
            bw.flush();
            bw.close();

            bw.close();
            writeFile.close();
            client.disconnect();
        }
    }

        public static void main(String[] args) throws IOException {
        // 这里的path是我自己约定的ip#catalogue的形式,
        // 如果形式不同,方法体截取的方法只需做对应的修改即可。
        String path = "1.1.1.1";
        GetMes get = new GetMes();
        get.getMessage(path);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值