SSHD密钥登录脚本

root
233
文章
0
评论
2020年7月19日20:02:09 评论 1377字阅读4分35秒

SSHD密钥登录脚本

batch_ssh_non_pwd_login.sh

# !/bin/bash
SERVERS="master01"
PASSWORD="123456"

for SERVER in $SERVERS
do
	yum -y install expect
#	apt-get -y install expect
	# 批量创建文件夹
	echo 'no this dir and then will create it.'
	expect -c "set timeout -1;
		spawn ssh root@$SERVER mkdir -p /root/bin/ssh
	expect {
		*password:* {send -- $PASSWORD\r;exp_continue;}
		*(yes/no)* {send -- yes\r;exp_continue;}
		eof         {exit 0;}
	}"
	expect -c "set timeout -1;
		spawn scp ./ssh_non_pwd_login.sh $SERVER:/root/bin/ssh
	expect {
		*password:* {send -- $PASSWORD\r;exp_continue;}
		*(yes/no)* {send -- yes\r;exp_continue;}
		eof         {exit 0;}
	}"
	expect -c "set timeout -1;
		spawn ssh root@$SERVER /root/bin/ssh/ssh_non_pwd_login.sh
	expect {
		*password:* {send -- $PASSWORD\r;exp_continue;}
		*(yes/no)* {send -- yes\r;exp_continue;}
		eof         {exit 0;}
	}"
done

ssh_non_pwd_login.sh

# !/bin/bash
SERVERS="node1 node2"
PASSWORD=123456
auto_gen_ssh_key() {
    	expect -c "set timeout -1;
    	spawn ssh-keygen;
	expect {
	    *(/root/.ssh/id_rsa)* {send -- \r;exp_continue;}
		*passphrase)* {send -- \r;exp_continue;}
		*again*	{send -- \r;exp_continue;}
		0*(y/n)* {send -- y\r;exp_continue;}
		*password:* {send -- $PASSWORD\r;exp_continue;}
		eof         {exit 0;}
	}";
}

auto_ssh_copy_id() {
	expect -c "set timeout -1;
    	spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $1;
	expect {
	    *(yes/no)*  {send -- yes\r;exp_continue;}
	    *password:* {send -- $2\r;exp_continue;}
	    eof         {exit 0;}
	}";
}

auto_copy_id_to_all() {
    for SERVER in $SERVERS
    do
         auto_ssh_copy_id $SERVER $PASSWORD
    done
}
#apt-get -y install expect
yum -y install expect
auto_gen_ssh_key
auto_copy_id_to_all

 

 

继续阅读
weinxin
我的微信
这是我的微信扫一扫
  • 文本由 发表于 2020年7月19日20:02:09
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
网站数据会滚脚本 shell

网站数据会滚脚本

网站数据会滚脚本 当代码经过测试环境后,正式进入生产环境,突然发现有问题,这时候需要立刻还原数据,写一个简单的网站代码回滚脚本 java项目的 #!/bin/bash Web_Dir=/server/...
三剑客练习 shell

三剑客练习

三剑客练习 grep I am hewenfu teacher! I teach linux. test I like badminton ball ,billiard ball and chines...
awk练习 shell

awk练习

awk 1.列出UID=0的用户 # awk -F : '$3==0' /etc/passwd root:x:0:0:root:/root:/bin/bash 2.列出系统的用户 # awk -F :...
函数,数组 shell

函数,数组

shell中函数的作用 命令合集,完成特定功能的代码块,在shell中定义函数可以使用代码模块化,便于复用代码,加强可读性,函数和变量类似,先定义才可调用,如果定义不调用则不会被执行 传参 $1,$2...
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: