Home > Java > javaTutorial > How to use Hutool's JschUtil in springboot

How to use Hutool's JschUtil in springboot

王林
Release: 2023-05-11 11:07:05
forward
1993 people have browsed it

windows server 2012 R2 install openssh

windows install ssh

linux itself uses ssh without installation

1. Download

Visit the Openssh official website and follow the instructions Select the appropriate installation package according to the number of bits of the operating system

How to use Hutools JschUtil in springboot

After entering the official website, select the appropriate installation package according to the number of bits of the operating system. However, 64-bit systems can also support 32-bit installation packages. For the 64-bit system here, I installed the 32-bit installation package.

2. Unzip the downloaded installation package to the C:/Program Files/ directory

Open the powershell terminal and enter the folder containing the ssh executable exe file cd C:\OpenSSH-Win32 \OpenSSH-Win32.

How to use Hutools JschUtil in springboot

3. Install ssh service

Enter the following command in the powershell terminal:

 powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1
Copy after login

After successful installation, the following will appear:

How to use Hutools JschUtil in springboot

4. Configure ssh service

  • Open port 22 in the firewall: Enter the following command in powershell:

netsh advfirewall firewall add rule name=sshd dir=in action=allow protocol=TCP localport=22
Copy after login

How to use Hutools JschUtil in springboot

Start ssh service

net start sshd
Copy after login

5. Add environment system variables

How to use Hutools JschUtil in springboot

will contain ssh Add the path to the executable exe file (in my case: C:\Program Files\OpenSSH-Win32\OpenSSH-Win32) to the environment system variables.

How to use Hutools JschUtil in springboot

Finally open cmd or powershell, enter ssh and the following image appears, which means the installation has been successful.

How to use Hutools JschUtil in springboot

6. Set the sshd service to start automatically at boot

Open "Server Manager" in sequence -> "Tools" - "Service"

How to use Hutools JschUtil in springboot

Enter the service list interface and find the OpenSSH SSH Server service

How to use Hutools JschUtil in springboot

Change the openssh authentication agent in the picture below Do the same as in the picture above.

How to use Hutools JschUtil in springboot

Windows that come with their own ssh service (such as windows 10) enable the ssh service

1. Client installation

Start-> Applications and Features-> Optional Features-> Add Features

There is an option for OpenSSH client in the list

Click to install OpenSSH client

After installation, you can use Windows PowerShell Directly use the ssh command

2. Server installation

Start-> Applications and Features-> Optional Features-> Add Features

There is an OpenSSH server in the list Options

Click to install the OpenSSH server

After the server is installed, you need to perform some configuration

3. Server configuration

Run Windows as administrator PowerShell

Enable SSHD service

Start-Service sshd
Copy after login

Set the service to start automatically

Set-Service -Name sshd -StartupType 'Automatic'
Copy after login

Confirm whether the firewall is open

Get-NetFirewallRule -Name *ssh*
Copy after login

Check whether OpenSSH-Server-In-TCP is enabled After the configuration is completed for True

, other clients can use ssh to connect to windows. The username and password are the username and password of windows

springboot uses

Introducing hutool

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.3.4</version>
</dependency>
Copy after login

Introduce jsch

<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.55</version>
</dependency>
Copy after login

Usage

Upload
@Test
void test18() {
//测试目录
    String linuxPath = "/var/file/test/";
    //创建session连接
    Session sessionLinux = JschUtil.getSession("106.12.127.40", 22,"root", "Ocean@123");
// 1.文件操作
    // 建立sftp
    Sftp sftp = JschUtil.createSftp(sessionLinux);
    //进入输入目录
    sftp.cd(linuxPath);
    //文件名称
    String fileName = "Sftp创建文件夹于"+DateUtil.format(new Date(), "yyyy年MM月dd日HH时mm分ss秒");
    //1.创建文件夹
    sftp.mkdir(fileName);
    System.out.println("=========================1.远程文件操作=========================");
    System.out.println(fileName+",文件是否存在?"+sftp.exist(linuxPath+fileName));
    //删除文件
    sftp.delDir(linuxPath+fileName);
    System.out.println(fileName+",文件是否存在?"+sftp.exist(linuxPath+fileName));
    System.out.println();
//2.上传文件
    // 本地新建文件
    System.out.println("=========================2.上传文件操作=========================");
    String localFile = DateUtil.format(new Date(), "yyyy年MM月dd日HH时mm分ss秒")+".txt";
    System.out.println(localFile);
    FileWriter fileWriter = new FileWriter(localFile);
    // 写入内容
    File file = fileWriter.write("123");
    fileWriter.append("追加信息");
    System.out.println(file.getPath());
    sftp.upload(linuxPath, file);
    //upload方法
    System.out.println("1.upload方法");
    System.out.println(localFile+",文件是否存在?"+sftp.exist(linuxPath+localFile));
    sftp.delFile(linuxPath+localFile);
    System.out.println(localFile+",文件是否存在?"+sftp.exist(linuxPath+fileName));
    sftp.put(file.getPath(),linuxPath);
    //put方法
    System.out.println("2.put方法");
    System.out.println(localFile+",文件是否存在?"+sftp.exist(linuxPath+localFile));
    sftp.delFile(linuxPath+localFile);
    System.out.println(localFile+",文件是否存在?"+sftp.exist(linuxPath+fileName));
    //删除本地文件
    FileUtil.del(file);
}
Copy after login

Running results:

How to use Hutools JschUtil in springboot

The above is the detailed content of How to use Hutool's JschUtil in springboot. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template