Scripting 期望脚本从hosts.txt读取IP,从commands.txt读取命令,并进一步实现脚本以获取服务器的一般信息

Scripting 期望脚本从hosts.txt读取IP,从commands.txt读取命令,并进一步实现脚本以获取服务器的一般信息,scripting,expect,Scripting,Expect,有两个文件: 1.hosts.txt 2.commands.txt 我打算编写一个脚本,从hosts.txt获取第一个IP,并从commands.txt运行第一个命令。 然后,脚本转到第二个IP表单hosts.txt并从commands.txt运行第二个命令 有可能吗?如果有,怎么可能 我尝试编写下面的expect脚本,但未能获得所需的输出 下面是代码和optput #!/usr/bin/expect -f #This Script will Commission Multiple Gatewa

有两个文件: 1.hosts.txt 2.commands.txt

我打算编写一个脚本,从hosts.txt获取第一个IP,并从commands.txt运行第一个命令。 然后,脚本转到第二个IP表单hosts.txt并从commands.txt运行第二个命令

有可能吗?如果有,怎么可能

我尝试编写下面的expect脚本,但未能获得所需的输出

下面是代码和optput

#!/usr/bin/expect -f
#This Script will Commission Multiple Gateway with TUI

set fid [open ./hosts.txt r]
set contents [read -nonewline $fid]
close $fid

# Get the commands to run, one per line
set q [open "commands.txt"]
set commands [split [read $q] "\n"]
close $q

foreach host [split $contents "\n"] {

#Loggin into the Gateway as Normal user

spawn -noecho ssh -o StrictHostKeyChecking=No expect@$host
expect "password:"
send "$pass\n"

#foreach commands [split $commands "\n"] 

# Iterate over the commands
foreach cmd $commands {
        expect "% "
        send "$cmd\r"
}
}

**the output of this script:**
script logs into the server one by on which are there in hosts.txt but run all the commands mentioned in commands and not as i want which is stated above

Note:i set variables for username passowrd which i mention in along with ./ while executing command so leva that part

这就是您将使用Tcl的“多列表”公式的地方:

set hosts [split $contents \n]

foreach host $hosts cmd $commands {
    # handle unequal size lists
    if {$host eq "" || $cmd eq ""} then continue
    
    spawn -noecho ssh -o StrictHostKeyChecking=No expect@$host
    expect "password:"
    send "$pass\r"

    expect "% "
    send "$cmd\r"
    expect "% "
    send "exit\r"
    expect eof

    # or, instead of the above expect-send pairs, do
    #   spawn -noecho ssh -o StrictHostKeyChecking=No expect@$host $cmd
    #   send password
    #   expect eof
}