onelinerでshe-bangを使用する

onelinerでshe-bangを使用する

onelinerでshe-bangを使用できますか?

このスクリプトを次から実行しようとしています。https://stackoverflow.com/questions/16928004/how-to-enter-ssh-password-using-bash、ワンライナーで。

ファイルに入れて./fileを使用して実行すると、期待どおりに動作しますが、どのようにワンライナーで実行できますか?

これは問題なくスクリプトとして機能します。

#!/usr/bin/expect -f
spawn ssh [email protected]
expect "assword:"
send "mypassword\r"
interact

onelinerでどのように実行できますか?

私が走ろうとしたら

spawn ssh [email protected]; expect "assword:"; send "mypassword\r"; interact

次を返します。

bash: spawn: command not found

私が走ろうとしたら

#!/usr/bin/expect -f; spawn ssh [email protected]; expect "assword:"; send "mypassword\r"; interact

これはすべてがコメントとして扱われ、何も起こりません。

編集する:

回答で提案された内容を実行してみてください。

expect -c `spawn ssh user@server "ls -lh /some/file"; expect "assword:"; send "mypassword\r"; interact`

帰ってきた

bash: spawn: command not found
couldn't read file "assword:": no such file or directory
bash: send: command not found
bash: interact: command not found
expect: option requires an argument -- c

実行時間:

$ cat << 'EOF' | /usr/bin/expect -
> spawn ssh user@server "ls -lh file"
> expect "assword:"
> send "password\r"
> interact
> EOF

SSH経由でサーバーに接続しているようですが、コマンドを実行しないか、結果を出力しません。

次を返します。

spawn ssh user@server ls -lh file
user@server password

スクリプトで実行するとき:

./test
spawn ssh user@server ls -lh file
user@server's password:
-rw-rw-r-- 1 user user 467G Jan  2 00:46 /file

編集2:

最初のコマンドの問題は、単一引用符の代わりにバックティックを使用することです。次のコマンドは期待どおりに機能します。

expect -c 'spawn ssh user@server "ls -lh file"; expect "assword:"; send "mypassword\r"; interact'

:

ベストアンサー1

文としてスクリプトをパラメータとして渡すことができます。expect -c

expect -c 'spawn ssh [email protected]; expect "assword:"; send "mypassword\r"; interact'

シェルの引用符'...'は と であり"..."、予想に対する対応する引用符は{...}"..."なので、シェル変数などを渡すのにある程度柔軟性があります。

しかし、これはすぐに読み取れずメンテナンスできなくなります。

おすすめ記事