■sftpでファイルを操作する
Linuxコマンド【 sftp 】安全なファイル転送 - Linux入門 - Webkaru
https://webkaru.net/linux/sftp-command/
ssh、scp、sftpコマンドをポート番号を指定して使う - bacchi.me
https://bacchi.me/linux/ssh-tips/
【 sftp 】Linuxサーバ ⇔ ローカルのファイル転送 - Qiita
https://qiita.com/r18j21/items/373bd66324bda83e0349
AWS(EC2)上にSpringBootアプリをデプロイする - 好奇心の赴くままに
https://writerman.hatenablog.jp/entry/2019/12/22/215749
バッチファイルを書いてSFTPサーバへのファイル配置を自動化する - Qiita
https://qiita.com/sasaplus1/items/619283711399c55306b6
Linuxコマンド【 sftp 】安全なファイル転送 - Linux入門 - Webkaru
https://webkaru.net/linux/sftp-command/
■コマンドを入力して操作
コマンドプロンプトを起動して、以下を入力すると接続できる
>sftp -P 10022 -i C:\Users\refirio\path\to\myserver.pem ec2-user@203.0.113.1
The authenticity of host '[203.0.113.1]:10022 ([203.0.113.1]:10022)' can't be established.
ECDSA key fingerprint is SHA256:Uz/afrXXXXXsXwBPD5oyNR4Oe8w9sUNFOdz+x8Tja8A.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Warning: Permanently added '[203.0.113.1]:10022' (ECDSA) to the list of known hosts.
Connected to 203.0.113.1.
基本的な操作
sftp> ls
ansible mecab-ipadic-neologd selenium_test
sftp> ls -l
drwxrwxr-x 6 ec2-user ec2-user 90 Sep 9 2021 ansible
drwxrwxr-x 10 ec2-user ec2-user 196 Sep 10 2021 mecab-ipadic-neologd
drwxrwxr-x 3 ec2-user ec2-user 149 Jun 21 2022 selenium_test
sftp> put C:\localhost\home\test\public_html\test.php
Uploading C:/localhost/home/test/public_html/test.php to /home/ec2-user/test.php
C:/localhost/home/test/public_html/test.php 100% 379 23.2KB/s 00:00
sftp> put -r C:\Users\refirio\batch\test\file /home/ec2-user/test
Uploading C:/Users/refirio/batch/test/file/ to /home/ec2-user/test/
Entering C:/Users/refirio/batch/test/file/
C:/Users/refirio/batch/test/file/sample.jpg 100% 337KB 3.2MB/s 00:00
C:/Users/refirio/batch/test/file/sample.pdf 100% 236KB 7.4MB/s 00:00
C:/Users/refirio/batch/test/file/sample.txt 100% 44 2.8KB/s 00:00
sftp> ls -l
drwxrwxr-x 6 ec2-user ec2-user 90 Sep 9 2021 ansible
drwxrwxr-x 10 ec2-user ec2-user 196 Sep 10 2021 mecab-ipadic-neologd
drwxrwxr-x 3 ec2-user ec2-user 149 Jun 21 2022 selenium_test
-rw-rw-r-- 1 ec2-user ec2-user 379 Jan 16 17:41 test.php
sftp> get test.txt
Fetching /home/ec2-user/test.txt to test.txt
/home/ec2-user/test.txt 100% 11 0.3KB/s 00:00
sftp> exit
put でファイルをアップロードすると、ホームディレクトリにアップロードされる
以下のようにすると、test 内にファイルがアップロードされる
mkdir test
cd test
put C:\localhost\home\test\public_html\test.php
以下のようにすると、アップロード場所を指定できる
put C:\Users\refirio\sftp\test.txt /home/ec2-user/test
get でファイルをダウンロードすると、sftp を実行した場所に保存される
以下のようにすると、ダウンロード場所を指定できる
get /home/ec2-user/test.php C:\Users\refirio\sftp
■バッチファイルを指定して操作
一例だが process.bat という名前で以下の内容を記述したファイルを作成しておく
put -r C:\Users\refirio\batch\test\file /home/ec2-user/test
ls -l /home/ec2-user/test
これでコマンドプロンプトから以下のように入力して実行すると、
ファイルがアップロードされて、ファイルの一覧も表示される
>sftp -b C:\Users\refirio\batch\test\process.bat -P 10022 -i C:\Users\refirio\path\to\myserver.pem ec2-user@203.0.113.1
さらに、上記コマンドを記述したファイルを例えば upload.bat という名前で作成しておけば、
コマンドプロンプトから「upload」と入力するだけで処理を行うことができる
(パスの指定などでコマンドに日本語が含まれる場合、upload.bat の文字コードはUTF-8Nにする必要があった)
■初回実行時の注意点
別のパソコンで同様に実行しようとしたが、以下のように接続に失敗した
>sftp -b C:\Users\refirio\batch\test\process.bat -P 10022 -i C:\Users\refirio\path\to\myserver.pem ec2-user@203.0.113.1
Host key verification failed.
Connection closed
SSHコマンドで接続を試みると、初回接続の確認が表示された
通常どおり「yes」とすることで接続できた
>ssh -p 10022 -i C:\Users\refirio\path\to\myserver.pem ec2-user@203.0.113.1
The authenticity of host '[203.0.113.1]:10022 ([203.0.113.1]:10022)' can't be established.
ED25519 key fingerprint is SHA256:QzG8/XXXXXXXXXXXX+fiuTDRZTg9bnP5Qy10ENxLclU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[203.0.113.1]:10022' (ED25519) to the list of known hosts.
Last login: Thu Feb 2 11:37:51 2023 from example.com
__| __|_ )
_| ( / Amazon Linux AMI
___|\___|___|
https://aws.amazon.com/amazon-linux-ami/2016.09-release-notes/
144 package(s) needed for security, out of 229 available
Run "sudo yum update" to apply all updates.
Amazon Linux version 2018.03 is available.
これ以降、以下のように実行できるようになった
>sftp -b C:\Users\refirio\batch\test\process.bat -P 10022 -i C:\Users\refirio\path\to\myserver.pem ec2-user@203.0.113.1
sftp> ls -l /home/ec2-user/test
-rw-rw-r-- 1 ec2-user ec2-user 345132 Feb 1 13:12 sample.jpg
-rw-rw-r-- 1 ec2-user ec2-user 241316 Feb 1 13:12 sample.pdf
-rw-rw-r-- 1 ec2-user ec2-user 44 Feb 1 13:12 sample.txt
sftp> put -r C:/Users/refirio/batch/test/file /home/ec2-user/test
Entering C:/Users/refirio/batch/test/file/
sftp> ls -l /home/ec2-user/test
-rw-rw-r-- 1 ec2-user ec2-user 345132 Feb 2 16:42 sample.jpg
-rw-rw-r-- 1 ec2-user ec2-user 241316 Feb 2 16:42 sample.pdf
-rw-rw-r-- 1 ec2-user ec2-user 44 Feb 2 16:42 sample.txt