我试图登录到一个需要密码的远程计算机,即使我用私钥/公钥设置ssh。我不能突然被造出来。这是我用来做这件事的脚本。
⌂107% [:~/bin] develop(+1/-1) ± cat _script
#!/bin/bash
#
# provide the info to remote-host from this script
#
function _ssh() {
USER=$1
HOST=$2
PSWD=$3
/usr/local/bin/expect<<EOD
set timeout 30
log_user 1
set send_slow {1 .01}
log_file ~/log/ssh_tmp.log
send_log "Connecting to $HOST using $USER user\n"
eval spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ConnectTimeout=30 "$USER\@$HOST"
expect {
timeout {send_user "timeout while connecting to $HOST\n"; exit }
"*No route to host*" { send_user "$HOST not reachable\n"; exit }
"*assword: " { send -s $PSWD\r }
}
expect {
timeout { send_user "timeout waiting for prompt\n"; exit }
"*]#" { send_user "Login successful to $HOST\n" }
}
send "\hostname\r"
expect {
"[*~]$" { send "exit\r" }
}
send_user "Disconnected\n"
close
EOD
}
_ssh remote-userid remote-host 'required-passphrase'以下是我得到的回应:
⌂77% [:~/bin] develop(+1/-1) 11s ± _script
spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ConnectTimeout=30 useid@remote-host
Warning: Permanently added 'remote-host,128.255.155.99' (ECDSA) to the list of known hosts.
--------------------------------------------------------------------------------
This is a private computing facility. Access to this service is limited to those
who have been granted access by the operating service provider on behalf of the
contracting authority and use is restricted to the purposes for which access was
granted. All access and usage are governed by the terms and conditions of access
agreed to by all registered users and are thus subject to the provisions of the
Computer Misuse Act, 1990 under which unauthorised use is a criminal offence.
If you are not authorised to use this service you must disconnect immediately.
--------------------------------------------------------------------------------
userid@remote-host's password:
Welcome to remote-host-name
Website: https://remote-host/
Documentation: https://remote-host/en/master/
Last login: Sat Dec 10 10:16:03 2021 from my-ip
[userid@remote-host ~]$ ls (I entered this, when the [userid@remote-host ~]$ appeared)
timeout waiting for prompt它似乎让我登录并显示提示符,但它还在期待什么吗?我该怎么处理?
发布于 2021-12-11 19:02:30
就像上面说的:“超时等待提示”
expect {
timeout { send_user "timeout waiting for prompt\n"; exit }
"*]#" { send_user "Login successful to $HOST\n" }
}将提示模式更改为-re {\][$#] $},以匹配一个紧跟在# (根)或$ (您)后面的方括号,后面是一个空格。
由于您将在几个地方期待提示符,所以将它存储在一个变量中
set prompt {\][$#] $}
expect {
timeout { send_user "timeout waiting for prompt\n"; exit }
-re prompt
}
send_user "Login successful to $HOST\n"将expect程序添加到bash脚本中,使用这里引用的-doc并将shell变量传递给环境:这很重要,因为您不希望shell扩展您的expect变量,也不希望开始转义操作。
function _ssh() {
export USER=$1
export HOST=$2
export PSWD=$3
/usr/local/bin/expect << 'EOD'
...
send_log "Connecting to $env(HOST) using $env(USER) user\n"产卵不需要评估:
spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ConnectTimeout=30 "$env(USER)@$env(HOST)"将close更改为expect eof,以允许ssh连接优雅地结束。
最后,expect代码正在执行与ssh流程的所有交互。除非您指示期望将ssh进程的控制权让给用户,否则您将无法在远程提示符下键入。您可以使用interact命令来完成这一任务。
https://stackoverflow.com/questions/70318056
复制相似问题