建立信任主机关系脚本
#!/bin/bash #功能:分发 #时间:2016-7-27 #版本:v0.01 #引用全局变量 . global_var #远程主机接收文件目录-->$remote_dir #远程主机接受文件用户-->$user #远程主机接受文件用户密码-->$passwd #============================== #Function->error_log #错误日志 #============================== error_log() { if [ $? -eq 0 ] ; then echo $1,$2,$3 >> $logfile cat tips >> $logfile else echo $1,$2,$4 >> $logfile cat tips >> $logfile fi } #============================== #Function->create_ip_list #获得需要分发文件的主机ip列表 #============================== create_ip_list() { echo ">>>>>>>>>>>>>>>正在创建ip列表<<<<<<<<<<<<<<<<" echo "10.0.10.111" > ip_list echo "10.0.10.12" >> ip_list } #============================== #Function->send_file #大批量发送 #============================== send_file() { echo echo echo -n -e "\t请输入文件的绝对路径:" read file filename=`echo $file|awk -F'/' 'END{print $NR}'` for ip in `cat ip_list` do ssh $user@$ip "mkdir $remote_dir" &>tips error_log "文件大批量分发" "ssh创建$remote目录" "【成功】" "【失败】" scp -r $file $user@$ip:$remote_dir/$filename &>tips error_log "文件大批量分发" "scp复制$file到$ip:$remote_dir" "【成功】" "【失败】" done } #============================== #Function->check_sys #检查系统状态 #============================== check_sys() { echo ">>>>>>>>>>>>>>>>正在检测系统<<<<<<<<<<<<<<<<<" if [ ! -f ~/.ssh/known_hosts ] ; then echo "正在建立信任关系..." ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa &>tips error_log "建立信任关系" "ssh-keygen创建密钥对" "【成功】" "【失败】" for ip in `cat ip_list` do sshpass -p $root_passwd ssh root@$ip -o StrictHostKeyChecking=no "useradd $user && echo $passwd | passwd $user --stdin" &>tips error_log "建立信任关系" "sshpass创建远程用户和设置密码" "【成功】" "【失败】" sshpass -p $passwd ssh $user@$ip -o StrictHostKeyChecking=no "mkdir ~/.ssh" &>tips error_log "建立信任关系" "sshpass创建.ssh目录" "【成功】" "【失败】" sshpass -p $passwd scp /home/$user/.ssh/id_rsa.pub $user@$ip:/home/$user/.ssh/authorized_keys &>tips error_log "建立信任关系" "sshpass拷贝公钥到远端" "【成功】" "【失败】" sshpass -p $root_passwd ssh root@$ip -o StrictHostKeyChecking=no "chmod 600 /home/$user/.ssh/authorized_keys && chmod 700 /home/$user/.ssh" &>tips error_log "建立信任关系" "sshpass更改远程文件权限" "【成功】" "【失败】" done else echo "已经建立信任关系" fi } #============================== #Function->Menu #菜单 #============================== Menu() { clear echo echo echo -e "\t===========ssh大批量分发部署系统==========" echo -e "\t\t(1)登录到WEB服务器" echo -e "\t\t(2)文件大批量分发" echo -e "\t\t(0)退出" echo -n -e "\t请输入菜单选项:" } #============================== #Function->login_web #登录web #============================== login_web() { clear echo echo echo -e "\t============登录到web服务器============" awk '{print "\t"NR"\t登录到WEB"NR"\t"$0}END{print "\t0\t退出"}' ip_list echo -n -e "\t一共有以上web服务器,请选择:" read op ip=`awk -v i=$op '{if(NR==i)print $0}' ip_list` case $op in 0) ;; *) if [ "$ip" == "" ] ; then echo -n -e "\t输入有误,请重新输入" sleep 1 login_web else ssh $ip error_log "登录WEB服务器" "ssh登录到$ip" "【成功】" "【失败】" fi ;; esac } #============================== #Function->Main #主函数 #============================== Main() { create_ip_list check_sys while true do Menu read op case $op in 1) login_web ;; 2) send_file ;; 0) echo -e "\t正在退出....." exit 1 ;; *) echo echo "输入选项错误" ;; esac echo echo -n "请输入任意值继续操作" read done } Main; #================global_var文件内容================= #==========================set global var======== remote_dir="~/recv_file" user="user500" passwd="aixocm" root_passwd="aixocm" logfile="logfile"
#----------All efforts I have paid today...