我的服务器有 2fa 并且不允许使用 ssh 密钥登录。我想通过
FTP
连接,我正在使用sshfs
。
正常sshfs user@host:/remote/ /local/
工作正常。但我想使用 expect
传递密码(-o password_stdin
对我不起作用)。
我已经在
.bashrc
中定义了函数
function mount_sshfs_with_2fa() {
# Get the FTP password from the encrypted file
local password=$(gpg --decrypt encrypted_password.txt)
# Prompt for verification code
read -p "Verification code: " otp
echo $otp
expect << EOF
spawn sshfs user@host:/remote/ /local/
expect -re "code:"
send -- "$otp\r"
send_user "Verification code: $otp\n"
expect -re "Password:"
send -- "$password\r"
send_user "Password: $password\n"
EOF
}
它不会给出任何错误,但也不会安装。请告诉我怎么了。
调试
expect -d
给
Verification code: 325776
325776
expect version 5.45.4
argv[0] = expect argv[1] = -d
set argc 0
set argv0 "expect"
set argv ""
executing commands from command file
spawn sshfs user@host:/remote/ /local/
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {153497}
expect: does "" (spawn_id exp4) match glob pattern "code:"? no
Verification code:
expect: does "\rVerification code: " (spawn_id exp4) match glob pattern "code:"? yes
expect: set expect_out(0,string) "code:"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "\rVerification code:"
send: sending "325776\r" to { exp4 }
expect: does " " (spawn_id exp4) match glob pattern "assword:"? no
expect: does " \r\n" (spawn_id exp4) match glob pattern "assword:"? no
Password:
expect: does " \r\n\rPassword: " (spawn_id exp4) match glob pattern "assword:"? yes
expect: set expect_out(0,string) "assword:"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) " \r\n\rPassword:"
send: sending "(my password)\r" to { exp4 }
更新2 而不是在这里编码以下也不起作用
expect -c "
spawn sshfs user@host:/remote/ /local/
expect "code:"
send -- "$otp\r"
expect "assword:"
send -- "$password\r"
interact
"
expect
是严重的事情还是浪费时间?我见过多种适用于其他人的解决方案给了我错误!例如,interact
在expect
here-code? 中不起作用
需要注意的一点:当heredoc结束时(在EOF处),然后expect进程退出,sshfs进程将被杀死。
你需要保持 expect 运行,你可能想在后台启动它:
function mount_sshfs_with_2fa() {
# Get the FTP password from the encrypted file
local password=$(gpg --decrypt encrypted_password.txt)
# Prompt for verification code
read -p "Verification code: " otp
{
expect << EOF
spawn sshfs user@host:/remote/ /local/
expect -re {Verification code: $}
send -- "$otp\r"
expect -re {Password: $}
send -- "$password\r"
set timeout -1
expect eof
EOF
} &
}
像这样尝试:
expect -d << EOF
spawn -ignore SIGHUP sshfs user@host:/remote/ /local/
set timeout -1
expect -re "code:"
send -- "$otp\r"
expect -re "Password:"
send -- "$password\r"
expect eof
sleep 1
EOF