#!/usr/bin/expect -f set remote_hosts { {"172.17.120.186" "root" "Haiyisec@123"} {"172.17.120.76" "root" "Haiyisec@123"} } # 生成SSH密钥对 send_user "在服务器上生成SSH密钥对 ...\n" spawn ssh-keygen -t rsa expect { "Enter file in which to save the key*" { send -- "\r" exp_continue } "Enter passphrase*" { send -- "\r" exp_continue } "Enter same passphrase again:" { send -- "\r" } } expect eof # 遍历远程服务器列表 foreach server $remote_hosts { send_user "配置服务器 $remote_host ...\n" # 拷贝公钥到远程服务器 spawn ssh-copy-id $remote_user@$remote_host expect { "*yes/no*" { send -- "yes\r" exp_continue } "*assword:" { send -- "$remote_password\r" } } expect eof # 配置本地SSH配置文件 #spawn sh -c "echo -e 'Host $remote_host\n\tUser $remote_user\n\tIdentityFile ~/.ssh/id_rsa' >> ~/.ssh/config" #expect eof # 修改SSH配置文件权限 #spawn chmod 600 ~/.ssh/config #expect eof send_user "服务器 $remote_host 配置完成!\n\n" } send_user "所有服务器SSH免密登录配置完成!\n"有什么错
时间: 2023-10-08 19:08:49 浏览: 42
在这段代码中,出现了一个错误。在`foreach`循环中,你没有正确地设置`remote_host`、`remote_user`和`remote_password`变量的值,导致后续的代码无法正确执行。
为了解决这个问题,你可以在循环的每次迭代中设置这些变量的值。请修改代码如下:
```bash
foreach server $remote_hosts {
set remote_host [lindex $server 0]
set remote_user [lindex $server 1]
set remote_password [lindex $server 2]
send_user "配置服务器 $remote_host ...\n"
# 后续代码...
}
```
在每次迭代中,使用`lindex`命令从当前服务器信息中提取出IP地址、用户名和密码,并将它们分别赋值给`remote_host`、`remote_user`和`remote_password`变量。这样,后续的代码就能够正确地使用这些变量,并完成SSH免密登录配置。
另外,如果你想配置本地SSH配置文件和修改SSH配置文件权限,你需要取消相关代码行前面的注释(`#`),并确保这些操作符合你的需求。
修改完毕后,再次运行代码应该就不会出错了。记得替换远程服务器的IP地址、用户名和密码为实际值。
相关问题
#!/usr/bin/expect -f和#!/bin/bash
引用中给出了一个脚本的示例,其中#!/usr/bin/expect -f是脚本的第一行,用来指定使用expect作为脚本的解释器。这意味着这个脚本将使用expect的语法和功能来执行。而#!/bin/bash是另一种脚本的解释器,用于指定使用bash作为脚本的解释器。bash是一种常用的Unix shell程序,用于执行命令和脚本。
简而言之,#!/usr/bin/expect -f用于指定使用expect解释器来执行脚本,而#!/bin/bash用于指定使用bash解释器来执行脚本。两者都是根据脚本的需要来选择合适的解释器。<em>1</em><em>2</em><em>3</em>
#### 引用[.reference_title]
- *1* *2* *3* [shell阶段-day12-expect](https://blog.csdn.net/qq_23995091/article/details/113571554)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item]
[ .reference_list ]
#!/usr/bin/expect -f set remote_hosts { {"172.17.120.186" "root" "Haiyisec@123"} {"172.17.120.76" "root" "Haiyisec@123"} } # 生成SSH密钥对 send_user "在服务器上生成SSH密钥对 ...\n" spawn ssh-keygen -t rsa expect { "Enter file in which to save the key*" { send -- "\r" exp_continue } "Enter passphrase*" { send -- "\r" exp_continue } "Enter same passphrase again:" { send -- "\r" } } expect eof # 遍历远程服务器列表 foreach server $remote_hosts { send_user "配置服务器 $remote_host ...\n" # 拷贝公钥到远程服务器 spawn ssh-copy-id $remote_user@$remote_host expect { "*yes/no*" { send -- "yes\r" exp_continue } "*assword:" { send -- "$remote_password\r" } } expect eof send_user "服务器 $remote_host 配置完成!\n\n" } send_user "所有服务器SSH免密登录配置完成!\n"将remote_hosts写在文本里
抱歉,但我无法执行你所描述的操作。我是一个基于文本的AI模型,无法直接读取或写入文本文件。我只能处理和生成文本内容。
你可以将远程服务器列表保存在一个文本文件中,然后在脚本中读取该文件并解析出远程服务器的信息。以下是一个示例:
```bash
#!/usr/bin/expect -f
# 从文件中读取远程服务器列表
set file [open "remote_hosts.txt" r]
set remote_hosts [split [read $file] "\n"]
close $file
# 遍历远程服务器列表
foreach server $remote_hosts {
set parts [split $server " "]
set remote_host [lindex $parts 0]
set remote_user [lindex $parts 1]
set remote_password [lindex $parts 2]
send_user "配置服务器 $remote_host ...\n"
# 后续代码...
}
```
在这个示例中,我们使用`open`命令打开名为"remote_hosts.txt"的文本文件,并将其保存在`file`变量中。然后,我们使用`read`命令读取文件内容,并使用换行符"\n"对其进行分割,得到一个远程服务器列表。接下来,我们使用`foreach`循环遍历这个列表,并使用`split`命令将每个服务器条目分割成IP地址、用户名和密码。最后,我们将这些信息分别赋值给`remote_host`、`remote_user`和`remote_password`变量,并执行后续的代码。
请确保在"remote_hosts.txt"文件中按照正确的格式(每个服务器一行,IP地址、用户名和密码之间用空格分隔)保存远程服务器的信息。这样,脚本就可以从文件中读取并使用这些信息进行配置。