各位好:
好久没有写博文了。今天分享一个关于Linux交互式登陆及交互式执行命令语句的经验。
————————————————————————————————————————
在生产环境中,一般不会做ssh互信配置。那么问题来了,如果要大批量执行操作或大批量分发脚本及配置文件(不使用自动化运维工具情况下),那么就要去写shell脚本。首先考虑到的,ssh登陆时会询问是否信任该主机
The authenticity of host '[127.0.0.1]:22 ([127.0.0.1]:22)' can't be established.
RSA key fingerprint is cd:67:80:18:40:**:**:dd:**:f3:29:63:45:2f:a8:70.
Are you sure you want to continue connecting (yes/no)?
Warning: Permanently added '[127.0.0.1]:22' (RSA) to the list of known hosts.
root@127.0.0.1's password:此处输入密码
这种情况正常使用expect脚本来实现应答式交互,但是expect语言比较麻烦。本人有点懒,走了个捷径,使用sshpass工具来实现交互式登陆。不多说,直接上代码。该脚本实现自动从密码文里面自动尝试密码登陆,并远程拷贝脚本和执行脚本并最后回收结果。
————————————————————————————————————————
#!/bin/bash # # # # 2017-09-25 # # By:Xiaohuan # ##This script is scp check_linux_performance.sh to server. # echo -e " +-------------------------------------------------------------------+ " echo -e " | 脚本分发程序(自动尝试密码) | " echo -e " | 脚本完成时间:2017-09-25 | " echo -e " | By:Xiaohuan | " echo -e " +-------------------------------------------------------------------+ " end=./end/ script=./check_linux_performance.sh VAR_SUBSYS_IPTABLES=`ps aux | grep $0 | grep -v grep | wc -l` time=`date +%Y%m%d-%H:%M:%S` #帮助 help() { echo -e '\E[31m'"\033[1\[help:]\033[0m" echo -e '\E[31m'"\033[1\ scp: scp script to server \033[0m" echo -e '\E[31m'"\033[1\ start: Connect to the server and execute the script \033[0m" echo -e '\E[31m'"\033[1\ end: Collection report to Directory for ./end \033[0m" echo -e '\E[31m'"\033[1\ Second parameter is port number \033[0m" echo -e 'Example:./start.sh scp 22' echo '' } if [[ "$1" == "" ]] || [[ "$2" == "" ]];then help exit 1; else if [ -f "$end" ];then echo "Directory $end not found,Please add ${end}." exit 1; else port=$2 fi fi #投放脚本 scp() { xargs -a ip.txt -n 1 -P 2 -I IP sh -c "for i in \$(seq 1 $(cat ./passwd.txt| wc -l)) ;do /usr/bin/sshpass -p \$(sed -n "\${i}p" passwd.txt) scp -P ${port} -o StrictHostKeyChecking=no \ check_linux_performance.sh IP:/tmp/ 2>/dev/null;if [ \$? -eq 0 ];then echo IP success ;break;fi;done" } #执行脚本并收集结果 start() { echo -en '\E[31m'"\033[1\[Please enter the log name of the result]:\033[0m" ;read name if [[ "$name" == "" ]];then echo "Variable cannot be empty" else xargs -a ip.txt -n 1 -P 2 -I IP sh -c "for i in \$(seq 1 $(cat ./passwd.txt| wc -l)) ;do /usr/bin/sshpass -p \$(sed -n "\${i}p" passwd.txt) ssh -p ${port} -o StrictHostKeyChecking=no \ IP \"chmod +x /tmp/${script};/tmp/${script} >> /tmp/${name}_IP_$(date +%Y%m%d-%H:%M:%S).txt\" ; \ if [ \$? -eq 0 ];then echo IP success ;break;fi;done" 2>/dev/null fi } #回拉结果 end() { echo -en '\E[31m'"\033[1\[Please enter the log name of the result]:\033[0m" ;read name if [[ "$name" == "" ]];then echo "Variable cannot be empty" else xargs -a ip.txt -n 1 -P 2 -I IP sh -c "for i in \$(seq 1 $(cat ./passwd.txt| wc -l)) ;do /usr/bin/sshpass -p \$(sed -n "\${i}p" passwd.txt) scp -P ${port} -o StrictHostKeyChecking=no \ IP:/tmp/${name}_*.txt ./${end}/;if [ \$? -eq 0 ];then echo IP success ;break;fi;done" 2>/dev/null fi } case "$1" in scp) [[ "$VAR_SUBSYS_IPTABLES" -eq 0 ]] && exit 1 scp RETVAL=$? ;; start) [[ "$VAR_SUBSYS_IPTABLES" -eq 0 ]] && exit 1 start RETVAL=$? ;; end) [[ "$VAR_SUBSYS_IPTABLES" -eq 0 ]] && exit 1 end RETVAL=$? ;; help) [[ "$VAR_SUBSYS_IPTABLES" -eq 0 ]] && exit 1 help RETVAL=$? ;; esac exit $RETVAL
脚本写的比较low,重在分享。另外,希望各位留言,多谢。
转载于:https://blog.51cto.com/x1aoxiao/1975494