2012-04-19 93 views
1

我運行一個遠程服務器上的腳本喜歡使用這個命令:期待重定向標準輸入

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 
    } 
} 
+1

可能重複以ssh](http://stackoverflow.com/questions/459182/using-expect-to-pass-a-password-to-ssh) – 2012-04-19 23:10:17

+0

從我可以告訴,你不管在腳本中,你只是執行腳本。一旦你讀到重複的問題,並改變你的腳本來匹配你應該沒問題。 – 2012-04-19 23:17:23

+0

我沒有得到。你的意思是我應該將腳本複製到遠程主機來執行它?我不想複製腳本。 – multiholle 2012-04-19 23:30:06

回答

5

你需要給你看劇本的標準輸入到遠程主機:

while {[gets stdin line] != -1} { 
    send "$line\r" 
} 

# then you may have to send ctrl-D to signal end of stdin 
send \004 
+0

我嘗試了你的想法,但不幸的是它不起作用。執行'spawn root @ host'bash -s''後,我嘗試發送一些命令,但最後我得到這個錯誤:'write()未能寫入任何內容 - 將睡眠(1)並重試...' – multiholle 2012-04-20 18:45:26

+0

嘗試不帶'-s'選項 – 2012-04-20 18:46:56

+0

仍然無法正常工作。 :/見我原來的問題,我更新了描述。 – multiholle 2012-04-20 18:55:34

0

使用expect_user,如圖手冊頁:

以下腳本讀取密碼,然後每小時運行一個程序,每次運行時都要求輸入密碼。腳本提供密碼,以便您只需輸入一次即可。 (見它演示瞭如何關閉密碼呼應stty命令。)

send_user "password?\ " 
expect_user -re "(.*)\n" 
for {} 1 {} { 
    if {[fork]!=0} {sleep 3600;continue} 
    disconnect 
    spawn priv_prog 
    expect Password: 
    send "$expect_out(1,string)\r" 
    . . . 
    exit 
} 

這是我現在有,還提高了它雖然:

#!/usr/local/bin/expect 

# For debugging make the following to be line 1: 
#!/usr/local/bin/expect -D 1 

set timeout 20 

send_user "Username?\ " 
expect_user -re "(.*)\n" 
set user $expect_out(1,string) 

send_user "password?\ " 
stty -echo 
expect_user -re "(.*)\n" 
stty echo 
set password $expect_out(1,string) 

spawn su 

expect { 

    "Password" {send "$password\r"} 

    "#"   {interact + return} 

} 
[使用期望通過密碼的
相關問題