该脚本实现对不同主机批量执行命令
[[email protected] ~]# cat exe.expect#!/usr/bin/expect set host [lindex $argv 0] set passwd "123456" set cm [lindex $argv 1] spawn ssh [email protected]$host expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect "]*" send "$cm\r" expect "]*" send "exit\r"
[[email protected] ~]# cat exe.sh#!/bin/bash for ip in `cat ip.txt` do echo $ip ./exe.expect $ip ‘w ; free -m ; ls /tmp" done
[[email protected] ~]# cat ip.txt 192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4
[[email protected] ~]# chmod a+x exe.expect [[email protected] ~]# chmod a+x exe.sh [[email protected] ~]# sh exe.sh
时间: 2025-01-02 16:07:33