A small tool for uploading files from windows to Linux with one click

Time:2021-1-17

1、 Preface

It’s not easy to keep learning experience by coding. Although I’m busy at work, it’s just an excuse to be lazy. In 19 years, it’s almost a month. This year, there are only four. No wonder I feel like I haven’t made much progress this year. It’s the end of 2020. In 2021, I will insist on writing more experiences, recording and sharing what I have learned.

Recently, I think it’s troublesome to update the configuration file and upload the jar package to Linux every time in the development stage, so I wrote a batch script for my own use. (the JSON configuration file of the game is placed outside the project to facilitate online configuration update during the game)

2、 Install winscp

Winscp is an open source graphical SFTP Client Using SSH in Windows environment. It also supports SCP protocol. Its main function is to copy files safely between local and remote computers.
The main purpose of using it is to update the files to be uploaded to the server through batch script.

I downloaded version 5.17.9. I don’t think the version is a big problem. Just choose any version.

3、 Scripting

1. Write script file upload_ to_ server.bat

cd D:\software\WinSCP
WinSCP.com /script=C:\Users\zzx\Desktop\PlanH5\farm\excel\upload_detail.bat /log=log.txt
pause

Line 1: go to your winscp installation directory
Line 2: specify DOS command program WinSCP.com To run another script of our winscp command. Specify log file generation / log= log.txt It doesn’t make much sense. If there is an error, it will be displayed in the window. It’s OK to remove it.

2. Actually run the script file upload_ detail.bat

open root:[email protected]:22
cd /mnt/farm_server/extensions/farm/property_config
rm *.json
put C:\Users\zzx\Desktop\PlanH5\farm\excel\out\data\*.json
exit

Line 1: open an SSH connection, format: user:[email protected] :22
2. Line 3 and line 4 are execution commands, among which put is the command of winscp. Put your local windows directory file into the directory you just CD.

Through such a simple script, I can upload the JSON file that I typed to the server directly, without compressing it and then throwing it in through xshell and decompressing it. (don’t ask Jenkins, just not yet. T T)

4、 Enhanced version

What if we want to do something else? Or do something else?

For example, when I want to update to the intranet, I want to use shell script to update to the extranet environment?
If you want to use the SCP command in a shell script, install expect first.

yum -y install expect

Writing shell scripts transfer.sh

#!/usr/bin/expect
spawn scp -r /mnt/farm_server/extensions/farm/property_config [email protected]:/tmp
expect "*password:"
send "password\n"
expect eof

It must be specified in the expect environment with #! / usr / bin / expect, otherwise it will not run.

Splash: start a new process 
Send: used to send a string to a process 
Expect: receives a string from a process

After executing the first line, it will receive the notification of entering the password through expect detection. At this time, it will send the password to the process through send, ending with \.

Use at the end of expect EOF.

After the script is written, how to execute it through winscp script?

In fact, some commands winscp can’t be executed directly. They need to be modified by the keyword call. For example, in the above script, CP, RM and other commands can be modified by call.
Upload above_ detail.bat Add:

open root:[email protected]:22
call cd /mnt/farm_server/extensions/farm/property_config
call rm *.json
put C:\Users\zzx\Desktop\PlanH5\farm\excel\out\data\*.json
call cd /mnt/farm_server
call ./transfer.sh
exit

In this way, you can transfer files to other servers or handle other things by calling shell scripts remotely.

5、 Winscp related commands

Winscp related:

Call to execute any remote command
CD change remote working directory
Checksum calculates the check code of the remote file
Chmod changes remote file permissions
Close close session
CP copy remote file
Echo displays parameters as messages
Exit closes all sessions and ends the program
Get download files from remote directory to local directory
Help display help
Keep update continuously reflects the changes of local directory in a remote directory
LCD changes local working directory
LLS lists the contents of the local directory
Ln new remote symbolic link
Lpwd displays the local working directory
Ls lists the contents of the remote directory
MKDIR new remote directory
MV move or rename remote files
Open connect to server
Option sets or displays the value of the script option
Upload files from local directory to remote directory
PWD displays the remote working directory
Deleting remote files
Rmdir Delete remote directory
Session lists the connected sessions or selects the active session
Stat get remote file properties
Synchronize synchronizes a remote directory with a local directory

If there are other requirements, you can use these commands, but I feel that a call is enough….

The year 2020 is coming to an end, and I hope I can go to a higher level next year.

come on.

The script and installation package have been uploaded to GitHub,
Address:https://github.com/zhuangzhen…