Application scenario
Recently, there is a requirement that after the server executes an instruction, it needs to back up the generated data to windows. It's not easy to check on the Internet. Some need to write a script to run in windows, but this application does not need windows operation. Instead, it monitors the operation of the server and backs up directly after executing an instruction.
Configure the ssh service on the server
This general server has been configured. If it is not configured, you can refer to it How to configure and install SSH service under Linux system - system operation and maintenance - Yisu cloud (yisu.com) Method configuration on
After the configuration is completed, enter SSH keygen and enter all the way. When y/n is encountered, yes will be displayed. The following description is OK.
fred@fred-VirtualBox:~/ssc338_v20211022$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/fred/.ssh/id_rsa): /home/fred/.ssh/id_rsa already exists. Overwrite (y/n)? y Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/fred/.ssh/id_rsa. Your public key has been saved in /home/fred/.ssh/id_rsa.pub. The key fingerprint is: SHA256:zpoJ5k3HizzF8qpW8z7kdU8vPp2QIItTBMbTQwk9wYE fred@fred-VirtualBox The key's randomart image is: +---[RSA 2048]----+ | .+O++ | | .E O | | o o | | o . | | .S o . . | | +*+.. .o. | | o..O*. . o.o.| | o.=.*=. +.o| | .o.O+o. ..o | +----[SHA256]-----+
Start ssh service in Windows 10
reference resources Install OpenSSH | Microsoft Docs Follow the instructions in to install and start the ssh service directly.
It should be noted that it is not enough to enter SSH in power shell to display the following contents. The following display shows that the computer has installed SSH client, so you need to install ssh server according to the above documents, because my application sends files from the server to Windows, so Windows is equivalent to the server at this time.
PS C:\Users\Fred> ssh usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface] [-b bind_address] [-c cipher_spec] [-D [bind_address:]port] [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11] [-i identity_file] [-J [user@]host[:port]] [-L address] [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address] [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]] destination [command]
test
The above two have installed and started the ssh services on the server and Windows normally. After a long time, you can test whether the ssh connection is normal. The following attempts are to use the IP of Linux is 192.168.0.119 and the IP of local Windows is 192.108.0.103
Connecting linux via ssh on Windows
PS C:\Users\Fred> ssh fred@192.168.0.119 The authenticity of host '192.168.0.119 (192.168.0.119)' can't be established. ECDSA key fingerprint is SHA256:shuor6LIVUODc9LBkBQI3tzlnnJFJ+WJ/KM/Rw0GwPc. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '192.168.0.119' (ECDSA) to the list of known hosts. fred@192.168.0.119's password: Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 5.4.0-89-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage * Canonical Livepatch is available for installation. - Reduce system reboots and improve kernel security. Activate at: https://ubuntu.com/livepatch 32 updates can be applied immediately. 1 of these updates is a standard security update. To see these additional updates run: apt list --upgradable New release '20.04.3 LTS' available. Run 'do-release-upgrade' to upgrade to it. Your Hardware Enablement Stack (HWE) is supported until April 2023. Last login: Thu Aug 12 11:01:13 2021 from 192.168.0.104 fred@fred-VirtualBox:~$
Similar to the above display, it indicates that Windows is connected to Linux normally
Connect windows on the server
fred@fred-VirtualBox:~/ssc338_v20211022$ ssh Fred@192.168.0.103 Fred@192.168.0.103's password: Microsoft Windows [Version 10.0.19042.1288] (c) Microsoft Corporation. All rights reserved. fred@DESKTOP-4B5T15G C:\Users\Fred>dir Driver C The volume in is OS The serial number of the volume is 0 A73-4233 C:\Users\Fred Directory of 2021/10/22 20:04 <DIR> . 2021/10/22 20:04 <DIR> .. 2021/09/07 fourteen:14 <DIR> .android 2021/06/02 09:25 888 .viminfo 2021/10/22 18:51 <DIR> .VirtualBox 2021/04/29 09:23 <DIR> .vscode 2021/09/15 11:34 <DIR> .xmos 2021/04/21 14:00 <DIR> 3D Objects 2021/04/21 13:58 <DIR> AppData 2021/08/27 16:42 143 authority_ctrl_user.csv 2021/04/21 14:00 <DIR> Contacts 2021/10/22 17:18 <DIR> Desktop 2021/09/23 19:41 <DIR> Documents 2021/08/10 17:07 <DIR> esp 2021/04/21 14:00 <DIR> Favorites 2021/04/21 14:00 <DIR> Links 2021/09/14 11:30 <DIR> Music 2021/04/21 15:02 <DIR> OneDrive 2021/06/16 09:46 <DIR> Pictures 2021/04/21 14:00 <DIR> Saved Games 2021/04/25 13:48 <DIR> Searches 2021/09/14 10:40 <DIR> usb_driver 2021/05/08 18:52 <DIR> Videos 5 Files 14,091 byte 33 Directories 71,653,093,376 Available bytes fred@DESKTOP-4B5T15G C:\Users\Fred>
As shown in the above figure, the test shows that the connection between Linux and windows is normal, and the ssh service of windows is normal. At this time, you can test the file transfer function
fred@fred-VirtualBox:~/ssc338_v20211022$ scp quickBuild.sh Fred@192.168.0.103:C:/Users/Fred/Documents/Downloads Fred@192.168.0.103's password: quickBuild.sh 100% 1978 4.6MB/s 00:00 fred@fred-VirtualBox:~/ssc338_v20211022$
After entering the above command, it means OK to find the file in the specified location of Windows. Note that when writing the path of Windows, use / can't use \, otherwise it will be copied to Disk C, and the file name will be changed to a pile of string superposition
-a---- 2021/10/22 20:37 1978 UsersFredDocumentsDownloads
SSH pass solves the problem of entering a password when canceling scp input
-
Installing sshpass
sudo apt install sshpass
-
Transfer files through sshpass
sshpass -p 123456 scp quickBuild.sh Fred@192.168.0.103:C:/Users/Fred/Documents/Downloads
The parameter - p is followed by the password
After all the above operations are completed, you can detect an instruction in the script, and then add the transmission instruction.