bat脚本:通过psftp.exe程序上传文件到sftp服务器

上传:

点击查看代码
@echo off
::::::::::::::::::::::::::
:: xiaoweibaxiangkanxue ::
::::::::::::::::::::::::::
title "upload object to sftp"


:: 固定信息,
set SFTP_HOST=172.xx.xx.xx
set SFTP_PORT=2222
set SFTP_USER=stpuser
set SFTP_PASSWD=zzzzzz!pwd
set SFTP_DIR=/aa/bb/cc-temp

set SCRIPT_HOME=%~dp0
set PSFTP_TOOL=%SCRIPT_HOME%\psftp.exe
set PSFTP_CONF_FILE=%SCRIPT_HOME%\psftp_upload_conf.conf

:: 这里填写上传的文件或者文件夹,文件夹内不允许有子文件夹
:: 新文件夹不允许与已上传的文件夹重名
:: 目录(和目录下的文件)/文件名需要是英文,避免上传乱码影响摆渡
set OBJECTS=test
echo OBJECTS is: "%OBJECTS%"


:: 单个文件上传报错能识别到具体报错文件
if exist "%SCRIPT_HOME%\%OBJECTS%" (
	for %%Z In ("%SCRIPT_HOME%\%OBJECTS%") do ( 
		if "%%~aZ" GEq "d" (
			rem OBJECTS is folder
			echo %SCRIPT_HOME%\%OBJECTS% is directory 
			echo mkdir %SFTP_DIR%/%OBJECTS% > %PSFTP_CONF_FILE%
			echo cd %SFTP_DIR%/%OBJECTS% >> %PSFTP_CONF_FILE%
			
			cd %SCRIPT_HOME%\%OBJECTS%
			for %%i in (*) do (
				rem put every file
				echo put %%i
				echo put %%i >> %PSFTP_CONF_FILE%
			)
			
		) else if "%%~aZ" GEq "-" (
			echo %OBJECTS% is a file
			echo cd %SFTP_DIR% > %PSFTP_CONF_FILE%
			echo put %OBJECTS% >> %PSFTP_CONF_FILE%
			echo bye >> %PSFTP_CONF_FILE%
			
		) else echo inaccessible
		
		:: begin put the OBJECTS
		echo begin put the %OBJECTS%
		%PSFTP_TOOL% -P %SFTP_PORT% %SFTP_USER%@%SFTP_HOST% -pw %SFTP_PASSWD% -b %PSFTP_CONF_FILE%
	)

	
) else (
	:: 异常退出批处理流程
	echo %SCRIPT_HOME%\%OBJECTS% is not exist	
	exit
)


pause

psftp_upload_conf.conf文件内容为需要上传的文件

从sftp服务器下载的bat脚本:

点击查看代码
@echo off
:::::::::::::::::::::::::::::
:: xiaoweibaxiangkanxue    ::
:: download object from 22 ::
:: transfer to 122         ::
:::::::::::::::::::::::::::::
title "download from 22 transfer to 122"


:: 固定信息
set SFTP_HOST=172.xx.xx.xx
set SFTP_PORT=2222
set SFTP_USER=sftpuser
set SFTP_PASSWD=xxxxx!pwd
set SFTP_DIR=/aa/bb/cc-sftp

set SCRIPT_HOME=%~dp0
set PSFTP_TOOL=%SCRIPT_HOME%\psftp.exe
set PSFTP_CONF_FILE=%SCRIPT_HOME%\psftp_download_conf.conf

:: 这里填写下载的文件或者文件夹
set OBJECTS=20230227
echo OBJECTS is:"%OBJECTS%"


:: 22下载
echo cd %SFTP_DIR% > %PSFTP_CONF_FILE%
echo mget -r %OBJECTS% >> %PSFTP_CONF_FILE%
echo bye >> %PSFTP_CONF_FILE%
%PSFTP_TOOL% -P %SFTP_PORT% %SFTP_USER%@%SFTP_HOST% -pw %SFTP_PASSWD% -b %PSFTP_CONF_FILE%

echo please enter a keyword to continue
pause



pause
posted @ 2023-07-04 16:58  小尾巴想看雪  阅读(819)  评论(0编辑  收藏  举报