linux - Expect 命令在 Linux 中再次要求输入密码

标签 linux shell redhat

我是 Linux Shell 脚本的新手。我需要执行一个 shell 脚本,该脚本分别获取用户名、密码、主机名、要执行的命令作为 1、2、3、4 参数。我使用了 expect 命令,但它再次提示输入密码。这个脚本的主题是从远程服务器执行命令。密码安全问题不是问题。主要问题是我不能被提示输入密码。我必须为大约 80 台服务器运行它,所以我不能每次都提供密码。请帮我解决这个问题。

我的脚本:

echo username = $1
echo Password = $2
echo Host-Name = $3
echo Command to Be executed = $4
expect -c "spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no $1@$3 $4; expect \"*assword:*\"; send "$2"; interact
#expect eof
"

我的输出:

username = root
Password = root@123
Host-Name = host-name
Command to Be executed = ls
spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no root@host-name ls
root@host-name's password:
~

最佳答案

首先你的索引将从 0 而不是 1 开始所以用户名 = $0

您还需要像这样设置您的变量:(这是我在服务器数百个节点上使用的期望脚本)

#!/usr/bin/expect
set user [lindex $argv 0]
set password [lindex $argv 2]
set ip [lindex $argv 2]
set command [lindex $argv 3]
spawn ssh -o StrictHostKeyChecking=no "$user\@$ip"
expect "assword:"
send "$password\r";
expect "$ "
send "$command\r"
expect "$ "  
send "exit\r"

关于linux - Expect 命令在 Linux 中再次要求输入密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24323779/

相关文章:

linux - 为什么 exec+tee 输出显示乱序?

linux - 如何从多个虚拟机中的多个资源组卸载Azure扩展?

linux - bash: ./mybashfile.sh: 权限被拒绝

c - 如何为 SIGCHLD 制作一个信号处理程序,它将在 shell 中获取后台进程?

r - 无法在 Redhat 上的 R 3.4.2 中安装 stringi 包

linux - "find -readable"找到不可读的文件夹

Linux 可执行文件格式。规范在哪里?

bash - 如何从多个文件中删除特定字符串之前的所有行

linux - 带有 _ 和 - 的内核模块名称?

linux - 使用 "default permission"选项融合文件系统