Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save toya33/075e0e66732d1a54897f to your computer and use it in GitHub Desktop.
Save toya33/075e0e66732d1a54897f to your computer and use it in GitHub Desktop.
expect_sample.sh
--------------------------------------------------------------
#!/bin/sh
COMMAND="ls -la"
expect -c"
set timeout 5
spawn env LANG=C /usr/bin/ssh UserName@host
expect \"\\\% \"
# ↑ホワイトスペースが必要!(zshの場合のみ確認)
send \"${COMMAND}\n\"
expect \"\\\% \"
exit 0
"
--------------------------------------------------------------
上記を実行すればUserName@hostにsshでログインしコマンド"ls -la"
を自動で実行してくれる
個人的に必要なこととして、実行結果のみを持ってきて、ログに吐き出す場合は以下
expect_sample_2.sh
--------------------------------------------------------------
#!/bin/sh
COMMAND="ls -la"
COMMAND_RESULT=`expect -c"
set timeout 5
spawn env LANG=C /usr/bin/ssh UserName@host
expect \"\\\% \"
send \"${COMMAND}\n\"
expect \"\\\% \"
exit 0
"`
echo "$COMMAND_RESULT" | cat | sed -e '1,7d' -e '$d' > test.log
--------------------------------------------------------------
もう少しなんとか見やすくしたいなー
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment