windows install ssh
linux itself uses ssh without installation
Visit the Openssh official website and follow the instructions Select the appropriate installation package according to the number of bits of the operating system
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.
Open the powershell terminal and enter the folder containing the ssh executable exe file cd C:\OpenSSH-Win32 \OpenSSH-Win32.
Enter the following command in the powershell terminal:
powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1
After successful installation, the following will appear:
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
Start ssh service
net start sshd
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.
Finally open cmd or powershell, enter ssh and the following image appears, which means the installation has been successful.
Open "Server Manager" in sequence -> "Tools" - "Service"
Enter the service list interface and find the OpenSSH SSH Server service
Change the openssh authentication agent in the picture below Do the same as in the picture above.
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
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
Run Windows as administrator PowerShell
Enable SSHD service
Start-Service sshd
Set the service to start automatically
Set-Service -Name sshd -StartupType 'Automatic'
Confirm whether the firewall is open
Get-NetFirewallRule -Name *ssh*
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
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.3.4</version> </dependency>
<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.55</version> </dependency>
@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); }
Running results:
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!