【发布时间】:2018-11-29 23:10:19
【问题描述】:
Bash 脚本 1:
询问用户名和主机名并存储到另一个变量。
#!/bin/bash
echo "Please enter hostname:"
read hostname
echo -n "Enter the username"
read username
echo -n "enter the password:"
read -s password
: ' (我想在 ssh 之前使用 spawn 命令,因此,我编写了另一个脚本,用 expect 作为解释器。
我想将用户输入的详细信息传递给脚本 2)
'
Bash 脚本 2:
#!/bin/expect
spawn ssh -o StrictHostKeyChecking=no $username\@$hostname
expect {
timeout
{ send_user "\nTimeout Exceeded - Check Host\n"; exit 1 }
eof
{ send_user "\nSSH Connection To $hostname Failed\n"; exit 1 }
incorrect
{send_user "invalid password or account\n"; exit 1}
"*assword:"
{ send "$password\n" }
expect "$"
interact
}
: ' (但是,如何将脚本1在运行时询问的信息调用到脚本2?)'
【问题讨论】:
-
看看sexpect,你可以用它编写Expect脚本,shell代码。