0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

batファイル WinSCP コンソールモード

Last updated at Posted at 2022-10-12

Windows → Linux ファイルを1つアップロード

@echo off
REM Windows から Linux ファイルアップロード

REM 変更箇所
REM 転送したいファイル情報をベタ打ちする場合
set FileWindows="C:\Users\user\Desktop\test.txt"
set FileLinux="/home/user/test.txt"

REM 転送したいファイルをパラメータとして使う場合
REM set FileWindows=%~1
REM set SaveDir=/home/user/TestDir
REM set FileLinux="%SaveDir%/%~nx1"

set LinuxPass=hogehoge
set LinuxUser=root
set LinuxIP=192.168.x.xx

REM 必須
set p1=/console
set p2=/command
set p3="option batch on"
set p4="option confirm off"
set p5="open %LinuxUser%:%LinuxPass%@%LinuxIP%" 

REM 任意コマンド
set p6="put %FileWindows% %FileLinux%"
set p7="close" 
set p8="exit"

"C:\Program Files (x86)\WinSCP\WinSCP.exe" %p1% %p2% %p3% %p4% %p5% %p7% %p8%

記述 説明 備考
"C:\Program Files (x86)\WinSCP\WinSCP.exe" WinSCP.exeの絶対パス
/console コンソールモードでWinSCPを起動
/command WinSCP起動直後に実行するWinSCPコマンドを指定 複数指定可能
option batch on バッチモードに設定し、確認/問い合わせを無効にする WinSCPコマンド
option confirm off ファイル上書きの確認などを無効にする WinSCPコマンド
open root:password@192.168.xx.xxx パスワードを指定してサーバー接続 WinSCPコマンド
put C:\Users\user\Desktop\test.txt /home/User/%FileName% ローカル から サーバへファイルをアップロード
put コピー元ファイル コピー先ファイル
WinSCPコマンド
close 切断 WinSCPコマンド
exit 終了 WinSCPコマンド

パラメータの有無を判定する場合

if "%~1"=="" (
echo パラメータがありません。
pause
exit
)

Windows → Linux 同一ディレクトリの.pyファイルを全てアップロード

@echo off
setlocal EnableDelayedExpansion

set FileLinuxDir=/home/user/PG_TEST/
set LinuxUser=root
set LinuxPass=hogehoge
set LinuxIP=192.168.xx.xxx

for %%i in (*.py) do (

	set FileWindows=%%~dpnxi
	echo ファイル名:!FileWindows!
	call :sendFile !FileWindows!

)

:sendFile
	echo sendFile: %1
	set p1=/console
	set p2=/command
	set p3="option batch on"
	set p4="option confirm off"
	set p5="open !LinuxUser!:!LinuxPass!@!LinuxIP!" 

	REM 任意コマンド
	set p6="put %1 !FileLinuxDir!/%~nx1"
	set p7="close" 
	set p8="exit"

	"C:\Program Files (x86)\WinSCP\WinSCP.exe" !p1! !p2! !p3! !p4! !p5! !p6! !p7! !p8!


Windows → Linux 任意コマンド実行

@echo off
REM Windows から Linux Shellの実行

REM 変更箇所
set LinuxPass=hogehoge
set LinuxUser=root
set LinuxIP=192.168.x.xx

REM 必須
set p1=/console
set p2=/command
set p3="option batch on"
set p4="option confirm off"
set p5="open %LinuxUser%:%LinuxPass%@%LinuxIP%" 

REM 任意コマンド
set p6="call /home/test/test.sh para1 para2"
set p7="call cd /home/user"
set p8="call ls -ltr|tail -1"

"C:\Program Files (x86)\WinSCP\WinSCP.exe" %p1% %p2% %p3% %p4% %p5% %p6% %p7% %p8% 
記述 説明
call 任意のコマンドを実行

参考記事

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?