#!/bin/bash
#remote command
#date 20160821
username=root
passwd=123456
usage(){
echo "Usage:sh `basename $0` -f [hostlist] -c [command]"
exit 1
}
while getopts ":f:c:" optname
do
case "$optname" in
f|-f)
file=$OPTARG
;;
c|-c)
command=$OPTARG
;;
*)
usage
;;
esac
#remote command
#date 20160821
username=root
passwd=123456
usage(){
echo "Usage:sh `basename $0` -f [hostlist] -c [command]"
exit 1
}
while getopts ":f:c:" optname
do
case "$optname" in
f|-f)
file=$OPTARG
;;
c|-c)
command=$OPTARG
;;
*)
usage
;;
esac
done
if [ -z "$file" ] || [ -z "$command" ] ;then
usage
fi
if [ ! -f "$file" ] ;then
echo "Host file $file does not exist"
exit
fi
for ip in `cat $file`
do
/usr/bin/expect<<EOF
set timeout 10
spawn ssh $username@$ip
expect {
"*yes/no" { send "yes\r"; exp_continue}
"*password:" { send "$passwd\r" }
}
expect "]*"
send "$command\r"
expect "]*"
send "exit\r"
EOF
done