LoginSignup
1
2

More than 5 years have passed since last update.

expectを使ってみた

Last updated at Posted at 2016-10-31

expectを使ってみた

これはだいぶ昔に書いたブログ(今は消滅)の記事です。
間違ってた箇所、更新された箇所などあればご指摘ください。

scpって言うファイルの転送コマンドがあるのですが、これ、対話型なんで、
phpなどでスクリプトキックしてもうまく転送してくれなかったりするんですよ。

そんな時に使うのがexpect。
何かを入力するときに自動的に入力してくれる素敵コマンドです。
素敵です。shell叩くだけでファイル転送してくれます。

作ってみたは良い物の、正直言うとshell初心者なので、結構ひどい事になってます。。。

添削できる方・・・添削をお願いします!

scp_send_file.sh
Shell

#!/bin/sh
# $1 scp_send_file example: working.txt
# $2 scp_send_host example: hage@hogehoge.jp
# $3 password example: hogehoge
# $4 port example: 22
# *usage
# ./scp_send_file.sh working.txt hage@hogehoge.jp hogehoge 22

expect -c "set timeout 5
spawn scp -P $4 $1 $2:$1
expect \"s password\"
send \"$3\n\"
interact"
1
2
2

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
1
2