Shell 需要在ssh脚本中修改';这是用expect写的

Shell 需要在ssh脚本中修改';这是用expect写的,shell,expect,Shell,Expect,我正在编写一个脚本,使用expect工具将ssh连接到服务器列表中。运行时出现以下错误 /剧本 #!/usr/local/bin/expect -f while /usr/bin/read hostname do spawn ssh user@$hostname expect "user@$hostname's password" send "resuidt\n" expect "user@$hostname" interact done < srvlist 需要帮助来修复此错误。您正在

我正在编写一个脚本,使用expect工具将ssh连接到服务器列表中。运行时出现以下错误

/剧本

#!/usr/local/bin/expect -f
while /usr/bin/read hostname
do
spawn ssh user@$hostname
expect "user@$hostname's password"
send "resuidt\n"
expect "user@$hostname"
interact
done < srvlist

需要帮助来修复此错误。

您正在编写一个Expect程序,它基本上是一个Tcl程序。您的
while
循环不是,但看起来像(Posix/Ksh/Bash/Zsh)-shell脚本


您必须下定决心:在Tcl中编写所有内容,或者将应用程序拆分为两个文件:一个(在shell脚本中)作为“主程序”,另一个单独的expect脚本,将由shell脚本调用。

因为user1934428表明您正在使用bash-type-while循环语法。 下面是一个如何使expect脚本执行所需操作的示例

#!/usr/local/bin/expect -f

set file hostname
set user myusername
set passwd mypassword

set f [open $file]
foreach target [split [read $f] "\n"] {
  spawn ssh $user@$target
  expect {
    timeout {send_user "Expect Timeout\n" ; exit}
    "password:"
  }
  send "$passwd\r"
  expect {
    timeout {send_user "Expect Timeout\n" ; exit}
    "$user@$target"
  }
  interact
}
close $f

我在expect部分中包含了超时,因为我发现,如果不添加这些安全机制,expect脚本甚至可以在没有正确响应的情况下继续运行。

如果要直接在expect脚本中使用shell变量,则必须在expect脚本中将这些变量作为$env(shell变量名称)传递


示例:生成ssh$env(myusername)@$env(hostname)

如何这样编写。任何可供参考的示例都会有所帮助。您所说的“that”是什么意思:纯Tcl解决方案,还是混合shell/Tcl解决方案?
#!/usr/local/bin/expect -f

set file hostname
set user myusername
set passwd mypassword

set f [open $file]
foreach target [split [read $f] "\n"] {
  spawn ssh $user@$target
  expect {
    timeout {send_user "Expect Timeout\n" ; exit}
    "password:"
  }
  send "$passwd\r"
  expect {
    timeout {send_user "Expect Timeout\n" ; exit}
    "$user@$target"
  }
  interact
}
close $f