因工作的需要,每天多次的需要从QNX和Linux服务器上下载Log文件,顾想利用WIndows批处理脚本结合WinSCP指令来实现Log文件下载。
1、Window 批处理脚本,生成和检查目录是否存在。
bash
echo ==============
echo Does the directory exist?
echo ==============
set "dir_QNX=QNX_Log"
set "dir_Linux=Linux_Log"
dir "%dir_QNX%" 2>nul
if !errorlevel! equ 0 (
echo Directory exists
) else (
echo make Directory
mkdir "%dir_QNX%"
)
dir "%dir_Linux%" 2>nul
if !errorlevel! equ 0 (
echo Directory exists
) else (
echo make Directory
mkdir "%dir_Linux%"
)
2、利用WinSCP命令,从QNX服务器上下载Log文件到本地
bash
echo ==============
echo QNX Log Download
echo ==============
echo === del log.txt ===
del /q log_file_Linux.txt
echo === winscp use sftp command ===
winscp.exe /console /command "option batch continue" "option confirm off " "open sftp://root:root@qnx-server.com" "get /log/ %dir_QNX% " "exit" /log=log_file_QNX.txt
echo View downloaded files
dir "%dir_QNX%"
echo Press any key to exit.......
pause
3、同理,从Linux服务上下载Log文件到本地
bash
echo ==============
echo Linux Log Download
echo ==============
echo === del log.txt ====
del /q log_file_QNX.txt
echo === winscp use scp command ===
winscp.exe /console /command "option batch continue" "option confirm off" "open scp://root:root@linux-server.com:2222" "get /log/ %dir_Linux% " "exit" /log=log_file_Linux.txt
echo View downloaded files
dir "%dir_Linux%"
echo Press any key to exit.......
pause
将上述3个部分整合到批处理脚本中,就可以实现自动去服务器下载Log文件。