首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用expect在登录到远程计算机时提供所需的字符串

如何使用expect在登录到远程计算机时提供所需的字符串
EN

Stack Overflow用户
提问于 2021-12-11 18:44:49
回答 1查看 50关注 0票数 0

我试图登录到一个需要密码的远程计算机,即使我用私钥/公钥设置ssh。我不能突然被造出来。这是我用来做这件事的脚本。

代码语言:javascript
复制
⌂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'

以下是我得到的回应:

代码语言:javascript
复制
⌂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

它似乎让我登录并显示提示符,但它还在期待什么吗?我该怎么处理?

EN

回答 1

Stack Overflow用户

发布于 2021-12-11 19:02:30

就像上面说的:“超时等待提示”

代码语言:javascript
复制
   expect {
      timeout { send_user "timeout waiting for prompt\n"; exit }
      "*]#"  { send_user "Login successful to $HOST\n" }
      }

将提示模式更改为-re {\][$#] $},以匹配一个紧跟在# (根)或$ (您)后面的方括号,后面是一个空格。

由于您将在几个地方期待提示符,所以将它存储在一个变量中

代码语言:javascript
复制
   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变量,也不希望开始转义操作。

代码语言:javascript
复制
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"

产卵不需要评估:

代码语言:javascript
复制
  spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ConnectTimeout=30 "$env(USER)@$env(HOST)"

close更改为expect eof,以允许ssh连接优雅地结束。

最后,expect代码正在执行与ssh流程的所有交互。除非您指示期望将ssh进程的控制权让给用户,否则您将无法在远程提示符下键入。您可以使用interact命令来完成这一任务。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70318056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档