sshfs在expect脚本中没有使用密码

时间:2011-08-19 07:07:56

标签: ssh expect

我写了一个小脚本来为sshfs提供密码,但由于某种原因,sshfs没有抓取密码。有什么指针吗? (PS我知道ssh密钥更好/更安全,但我工作的政治会阻止在目标服务器上设置基于密钥的身份验证 - 叹息......)。

#!/usr/bin/expect
# NOT WORKING!!
exp_internal 1
spawn sshfs server:/export/pc_storage /home/sonia/mnt/server
expect {
    "assword:" {
        send "secret\r\r"
        send_user "\n"
    }
    timeout {
        send_user "timed out!\n"
     }
}

要终止密码,我尝试过\ r \ n \ r \ n \ r \ n-无效。

调试输出,显示密码提示正在触发:

spawn sshfs server:/export/pc_storage /home/sonia/mnt/server
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {17532}

expect: does "" (spawn_id exp6) match glob pattern "assword:"? no
pcuser@server's password: 
expect: does "pcuser@server's password: " (spawn_id exp6) match glob pattern "assword:"? yes
expect: set expect_out(0,string) "assword:"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "pcuser@server's password:"
send: sending "secret\r\r" to { exp6 }

% uname -a
Linux zapote 2.6.38-10-generic-pae #46-Ubuntu SMP Tue Jun 28 16:54:49 UTC 2011 i686 i686 i386 GNU/Linux

% sshfs -V
SSHFS version 2.2
FUSE library version: 2.8.4
fusermount version: 2.8.4
using FUSE kernel interface version 7.12

解决:

(因为我没有100分,我不能发一个答案 - 双重叹息)

我需要稍微解决这个问题......例如,我将.bashrc中的PS1硬编码为“bash_prompt”(我默认使用zsh)。

#!/usr/bin/expect

# FIX: get homedir from env, set bash prompt somehow...

set timeout 30

spawn /bin/bash
expect "bash_prompt"
send_user "Shell spawned.\n"

send -- "sudo umount /home/sonia/mnt/server &> /dev/null\r"
expect "bash_prompt"

send -- "sshfs server:/export/pc_storage /home/sonia/mnt/server\r"
expect {
    "assword:" {
        send "secret\r"
        send_user "\n"
    }
    timeout {
        send_user "timed out!\n"
    }
}
expect "bash_prompt"

3 个答案:

答案 0 :(得分:5)

您可以直接提供密码,而不是使用Expect。 试试这个:

echo your-password | sshfs server:/export/pc_storage /home/sonia/mnt/server -o password_stdin

不确定您是否可以使用密码策略,但值得一试。

答案 1 :(得分:2)

我需要稍微解决这个问题......例如,我将.bashrc中的PS1硬编码为“bash_prompt”(我默认使用zsh)。

#!/usr/bin/expect

# FIX: get homedir from env, set bash prompt somehow...

set timeout 30

spawn /bin/bash
expect "bash_prompt"
send_user "Shell spawned.\n"

send -- "sudo umount /home/sonia/mnt/server &> /dev/null\r"
expect "bash_prompt"

send -- "sshfs server:/export/pc_storage /home/sonia/mnt/server\r"
expect {
    "assword:" {
        send "secret\r"
        send_user "\n"
    }
    timeout {
        send_user "timed out!\n"
    }
}
expect "bash_prompt"

答案 2 :(得分:0)

\r\r似乎不对。你试过\r\n吗?