我運行一個遠程服務器上的腳本喜歡使用這個命令:期待重定向標準輸入
ssh [email protected] 'bash -s' < script.sh
現在我試圖用預期來處理密碼提示。這是腳本:
#!/usr/bin/expect
set cmd [lindex $argv 0]
spawn -noecho ssh [email protected] $cmd
expect {
"password:" {
send "password\r"
}
}
如果我運行該腳本,它沒有給輸出:
./ssh.exp 'bash -s' < script.sh
我知道這不是使用ssh無密碼的方式,但在這裏,這不是問題。
UPDATE我想格倫·傑克曼的想法用一個簡單的腳本,但它不工作。這是我使用的腳本:我設法它讓我的腳本來運行
[[email protected] bin]$ expect -d my.exp
expect version 5.43.0
argv[0] = expect argv[1] = -d argv[2] = my.exp
set argc 0
set argv0 "my.exp"
set argv ""
executing commands from command file my.exp
spawn ssh [email protected]
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {7599}
expect: does "" (spawn_id exp6) match glob pattern "*?assword:*"? no
[email protected]'s password:
expect: does "[email protected]'s password: " (spawn_id exp6) match glob pattern "*?assword:*"? yes
expect: set expect_out(0,string) "[email protected]'s password: "
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "[email protected]'s password: "
send: sending "pwd" to { exp6 }
send: sending "echo hello world" to { exp6 }
write() failed to write anything - will sleep(1) and retry...
UPDATE:
#!/usr/bin/expect
spawn ssh [email protected]
expect "*?assword:*"
send "pwd\r"
send "echo hello world"
這是輸出我得到。這是工作的結果:
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set pwd [lindex $argv 2]
spawn ssh [email protected]$host bash -s
expect {
"?asswor?: " {
send "$pwd\n"
}
}
while {[gets stdin line] != -1} {
send "$line\n"
}
send \004
expect {
"END_TOKEN_OF_SCRIPT" {
exit 0
}
default {
exit 1
}
}
可能重複以ssh](http://stackoverflow.com/questions/459182/using-expect-to-pass-a-password-to-ssh) – 2012-04-19 23:10:17
從我可以告訴,你不管在腳本中,你只是執行腳本。一旦你讀到重複的問題,並改變你的腳本來匹配你應該沒問題。 – 2012-04-19 23:17:23
我沒有得到。你的意思是我應該將腳本複製到遠程主機來執行它?我不想複製腳本。 – multiholle 2012-04-19 23:30:06