expect脚本:
[10:40:45 root@centos8 ~]#cat expect{1..3}#!/usr/bin/expectspawn ssh 10.0.0.151expect { "yes/no" { send "yes\n";exp_continue } "password" { send "123456\n" }}interact###################################################!/usr/bin/expectset ip 10.0.0.151set user rootset password 123456set timeout 10spawn ssh $user@$ipexpect { "yes/no" { send "yes\n";exp_continue } "password" { send "$password\n" }}interact###################################################!/usr/bin/expectset ip [lindex $argv 0]set user [lindex $argv 1]set password [lindex $argv 2]set timeout 10spawn ssh $user@$ipexpect { "yes/no" { send "yes\n" ;exp_continue } "password" { send "$password\n" }}expect "#" { send "useradd hha\n" }send "exit\n"expect eof#使用expect -f expect1执行脚本,或者给执行权限后./expect1执行。
shell脚本:
[10:43:08 root@centos8 ~]#cat expect4#!/bin/bashnetwork=10.0.0user=rootpassword=123456iplist="151"for ID in $iplist;doip=$network.$IDexpect <<fffspawn ssh $user@$ipset timeout 3expect { "yes/no" { send "yes\n";exp_continue } "password" { send "$password\n" }}expect "#" { send "useradd hhh\n" }expect eoffffdone#使用bash expect4,或给执行权限后./expect4运行脚本