#!/bin/bash
#author:dcc
#date:2018/05/25
#version:v1
#description:ssh-copy-id to every PC,you must appoint a NET ID
#check whether all the PC is up
if [ -z $1 ];then
echo "Error:you must appoint a NET ID"
exit 1
fi
test01=`echo $1 | awk -F . '{print $4}'`
if [ ! $test01 -eq 0 ];then
echo "Error:please appoint a right NET ID"
exit 2
fi
tmp=$1
ID=${tmp%%0*}
for i in 159
do
ping -c 3 -i 0.3 -W 1 $ID$i &> /dev/null
if [ ! $? -eq 0 ];then
echo "$ID$i is down;please up this PC"
exit 3
fi
done
#check whether install expect
rpm -q expect &> /dev/null
if [ ! $? -eq 0 ];then
test01=`yum repolist|grep "repolist: "|sed -n 's/.*://p'`
if [ "$test01" == "0" ];then
echo "expect is no install and can not install expect because the yum is not usabled"
exit 1
else
yum -y install expect >> /dev/null || echo "install failed" && exit 1
fi
fi
#check /root/.ssh/id_rsa whether exist
if [ ! -f /root/.ssh/id_rsa ];then
#create ssh-key
expect<<EOF
spawn ssh-keygen
expect ":" { send "\r"}
expect ":" { send "\r"}
expect ":" { send "\r"}
expect ":" { send "\r"}
EOF
fi
#ssh-copy-id to all the PC in this NETWORK
for i in 159
do
expect<<EOF
spawn ssh-copy-id root@$ID$i
expect "password" { send "000000\r"}
expect "#" { send "\r"}
EOF
done
#check
for i in 159
do
expect<<EOF
spawn ssh root@$ID$i
expect "#" {send "exit\r"}
expect "#" {send "exit\r"}
EOF
if [ $? -eq 0 ];then
echo "$ID$i success"
fi
done