Linux 如何调用在执行期间传递给另一个bash脚本的变量?

Linux 如何调用在执行期间传递给另一个bash脚本的变量?,linux,bash,expect,Linux,Bash,Expect,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) “ Ba

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?)

bash脚本将调用expect脚本,如下所示:

expect script.exp "$username" "$password" "$hostname"
expect脚本的开头如下所示:

#!/bin/expect

lassign $argv username password hostname

# or if your version of expect is old and does not have "lassign"
# foreach {username password hostname} $argv break

# or, if you prefer
# set username [lindex $argv 0]
# set password [lindex $argv 1]
# set hostname [lindex $argv 2]

spawn ssh -o StrictHostKeyChecking=no $username@$hostname
# etc etc
@
字符没有特殊含义,因此不需要转义


上述方法存在安全风险:当expect代码运行时,
ps
输出将显示密码

您可以通过以下环境共享密码:

猛击

期待

spawn ssh -o StrictHostKeyChecking=no $env(username)@$env(hostname)
# etc etc
请看一看,您可以使用哪些工具仅使用shell代码编写Expect脚本。
spawn ssh -o StrictHostKeyChecking=no $env(username)@$env(hostname)
# etc etc