常见的批量管理软件:ssh+rsync+sersync+expect Saltstack ansible puppet
rpm -qa openssh openssl
ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa
# or
ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_dsa.pub -p 31 linyaohong@10.10.10.31
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
关于公钥和私钥 使用 ssh-keygen 命令来创建
RSA 一种加密算法,也能用来做数字签名
DSA 只能做数字签名
SSH是安全的加密协议,默认端口22,权限协议版本SSH2(有两个版本1和2)
SSH服务端主要包含两个服务功能,SSH远程连接,SFTP服务
ssh -p 31 linyaohong@10.10.10.31 /sbin/ifconfig #执行单个命令
ssh -p 31 linyaohong@10.10.10.31 /sbin/ifconfig #远程执行命令,要跟全路径
ssh_command.sh "/usr/bin/free -h" #使用脚本执行命令
常用脚本01:执行命令:
#!/bins/sh
if [ $# -ne 1 ];then
echo "USAGE:/bin/sh $0 command"
exit 1
fi
for n in 41 31 7 8
do
echo "===10.10.10.${n}==="
ssh -p ${n} 10.10.10.${n} $1
done
常用脚本02:分发文件
#!/bin/sh
. /etc/init.d/functions
if [ $# -ne 1 ];then
echo "usage: /bin/sh $0 FILENAME"
exit 1
fi
for n in 41 31 7 8
do
echo "===10.10.10.${n}==="
scp -P ${n} $1 10.10.10.${n}:~ &>/dev/null
if [ $? -eq 0 ];then
action "dis $1 10.10.10.${n}" /bin/true
else
action "dis $1 10.10.10.${n}" /bin/false
fi
done
#-------------如果不是root用户使用sudo
#!/bin/sh
. /etc/init.d/functions
if [ $# -ne 2 ];then
echo "usage: /bin/sh $0 FILENAME /PATH/"
exit 1
fi
for n in 41 31 7 8
do
scp -P ${n} -rp ${1} 10.10.10.${n}:~ &>/dev/null &&\
ssh -t -p ${n} linyaohong@10.10.10.${n} sudo /bin/cp -rp ~/`basename ${1}` ${2} &>/dev/null
if [ $? -eq 0 ];then
action "dis $1 10.10.10.${n}" /bin/true
else
action "dis $1 10.10.10.${n}" /bin/false
fi
done
常用脚本03:分发公钥
如何来批量分发 shel脚本 expect
expect是一个免费的编程工具,用来实现自动的交互式任务,而无需人为干预。说白了,expect就是一套用来实现自动交互功能的软件。
在实际工作中,我们运行命令、脚本或程序时,这些命令、脚本或程序都需要从终端输入某些继续运行的指令,而这些输入都需要人为的手工进行。而利用expect,则可以根据程序的提示,模拟标准输入提供给程序,从而实现自动化交互执行。
fenfa_sshkey.exp 和 fenfa_sshkey.sh 两个文件配合
#!/usr/bin/expect
if { $argc != 2 } {
send_user "usage: expect fenfa_sshkey.exp file host\n"
exit
}
#define var
set file [lindex $argv 0]
set host [lindex $argv 1]
set password "111111"
#spawn scp /etc/hosts root@10.0.0.142:/etc/hosts
#spawn scp -P52113 $file oldboy@$host:$dir
#spawn ssh-copy-id -i $file "-p 52113 oldboy@$host"
spawn ssh-copy-id -i $file -p 8 yaohong@$host
expect {
"yes/no" {send "yes\r";exp_continue}
"*password" {send "$password\r"}
}
expect eof
exit -onexit {
send_user "$host ok!\n"
}
#--------------------------------------
#!/bin/sh
. /etc/init.d/functions
for ip in `cat iplist`
do
expect fenfa_sshkey.exp ~/.ssh/id_dsa.pub $ip >/dev/null 2>&1
if [ $? -eq 0 ];then
action "$ip" /bin/true
else
action "$ip" /bin/false
fi
done