我想编写一个 shell 脚本,可以通过 telnet 调用一系列 IP,发送命令,并将结果写入文件。
答案1
ssh 比 telnet 更简单:
for host in host1 host2 host3; do
ssh user@host remote_command with args
done > result.file
对于无密码登录,请设置 ssh 密钥
这会有错误,但是类似于:
#!/usr/bin/env expect
exp_internal 1
set out [open "results.txt" w]
foreach ip $argv {
spawn telnet $ip
expect "password: "
send "$password\r"
expect "% " ;# this is the prompt, adjust to suit
sent "the command\r"
expect -re "([01,]+).*% "
puts $out "$ip=$expect_out(1,string)"
send exit
expect eof
exp_close
wait
}
close $out