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 3 years have passed since last update.

WSLでsshpassを使ってscpファイル送信②

Last updated at Posted at 2020-09-27

初めに

WSL(Ubuntu)で、シェルを実行したら、パスに記載しているファイルを指定サーバー上の指定フォルダに転送する。
今回はパス毎で転送先を指定できるような形式
どこまでファイルにして、どこまで実行ファイルに書くかはお好みで
2020/10/7 修正

実行(確認)環境

WSL2
Ubuntu 18.04
CentOS7(格納先)

フォルダ構成

# 実行フォルダ(どこでも大丈夫なハズ)
c:\test\
 ├ transport.sh
 ├ filepath(パスを記載)
 └ passfile(パスワードを記載)

# 転送ファイル群
c:\temp\
 ├ test1.txt
 ├ test2.txt
 └ test3.txt

ソース

設定ファイル系

filepath
C:/temp/test1.txt,/root/temp1
C:/temp/test2.txt,/root/temp2
C:/temp/test3.txt,/root/temp3

※改行はLFで

passfile
hogehoge

※1行で記載する必要あり

シェルスクリプト

transport.sh
# !/bin/bash

# sshのユーザー名
USERNAME="root"
# サーバーのホスト
SERVERHOST="XXX.XXX.XXX.XXX"

# ファイルパスをループ
for line in `cat filepath`
do
    # 分割
    arr=(${line//,/ })
    # ローカルファイルパス変換
    upfiles=$(wslpath -u ${arr[0]})
    # サーバーパス
    serverpath=${arr[1]}
    # 送信情報をセット
    transports=$transports" ${upfiles} ${USERNAME}@${SERVERHOST}:${serverpath}"
done
# 送信
sshpass -f passfile scp -o StrictHostKeyChecking=no $transports

実行(一応)

cd /mnt/c/test/
./transport.sh

bin/shでの配列操作が分からなくて、bin/bashに変更、、
ていうか、違いなんて知らなかった、、
まぁ、実用性は遅すぎてほぼ0です、、
1つのsshpassに指定できるので、実用性はだいぶ上がるはず

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?