免密登陆脚本(shell版)

news/2024/7/8 6:26:23

为什么80%的码农都做不了架构师?>>>   hot3.png

  1. 使用说明: 1.ssh.sh为单节点无密码登录脚本; 2.ssh1.sh为集群无密码登录脚本; 3.将ssh与ssh1放到同一节点上,运行ssh2脚本; 4.整体脚本中expect需要提前安装tcl-与expect-,目前此脚本在外网部署已经写好;在内网安装时,需要手动下载expect-*进行安装,命令为;yum install expect -y 5.所有节点需要有/root/Desktop文件夹

##ssh.sh

#!/bin/bash
#fn_install_ssh(){
#HOST_NAME="cloudera-08"
/usr/bin/expect <<-EOF
spawn ssh-keygen -t rsa -P ""
expect "*rsa):" { 
	send "\n" 
	expect "*y/n)?" {send "y\n"}
	}	
expect eof
EOF
     	sleep 1  
     	cd /root/.ssh/
      	cat id_rsa.pub >> authorized_keys
    	chmod 700 authorized_keys
#}

##ssh1.sh

#!/bin/bash
HOST_NAME=("ab-01" "ab-02" "ab-03" "ab-04" "ab-05" "ab-06" "ab-07")
user="root"
file_need_dir="/root/Desktop/"
file_ssh="ssh.sh"
PASSWORD="123456"
ssh_dir="/root/.ssh/"
author="authorized_keys"
keys="authorized_keysab*"
for hostname in ${HOST_NAME[@]}
do
yum install expect -y
sleep 2
/usr/bin/expect <<-EOF 
spawn scp /root/Desktop/ssh.sh $user@$hostname:/root/Desktop/
expect { 
 	"*s/no)?" {send "yes\r"
 		 expect "*ssword:" {send "$PASSWORD\r"}
	}	
	"*ssword:" {send "$PASSWORD\n\r"}
}
expect eof
EOF
done

for hostname in ${HOST_NAME[@]}
do
/usr/bin/expect <<-EOF
spawn ssh $hostname
expect {
	"*yes/no)?" {send "yes\n\r"
                 expect "*ssword:" {send "$PASSWORD\n\r"}
        }
        "*ssword:" {send "$PASSWORD\n\r"}
	}
expect "*#"
send "cd /root/Desktop\n\r"
expect "*#"
send "./ssh.sh\n\r"
expect eof
EOF
done


for hostname in ${HOST_NAME[@]}
do
/usr/bin/expect <<-EOF
spawn ssh $hostname
expect {
	"*yes/no)?" {send "yes\n\r"
                 expect "*ssword:" {send "$PASSWORD\n\r"}
        }
        "*ssword:" {send "$PASSWORD\n\r"}
	}
expect "*#" {send "scp -r $ssh_dir$author $user@${HOST_NAME[0]}:$ssh_dir$author${hostname}\n\r"
         expect {
              "*yes/no)?" {send "yes\n\r"
                expect "*ssword:" {send "$PASSWORD\n\r"}
                      }
          "*ssword:" {send "$PASSWORD\n\r"}
        }
        } 
expect eof
EOF
done
echo "*****************************·Ö·¢Íê³É**********************************"


/usr/bin/expect <<-EOF
spawn ssh ${HOST_NAME[0]}
expect {
         "*no)?" {send "yes\n"
                  expect "*word:" {send "$PASSWORD\n"}
                  }
         "*word:" {send "$PASSWORD\n\r"}
      }
expect "*#" 
send "cat $ssh_dir$keys >> $ssh_dir$author\n\r" 
interact
expect eof
EOF
sleep 3
echo "********************ÈÚºÏÍê³É***************************"


for hostname in ${HOST_NAME[@]}
do
/usr/bin/expect <<-EOF
spawn ssh ${HOST_NAME[0]}
expect {
         "*no)?" {send "yes\n\r"
                  expect "*word:" {send "$PASSWORD\n\r"}
                  }
         "*word:" {send "$PASSWORD\n\r"}
      }
expect {
       "*#" { send "scp -r $ssh_dir$author $user@$hostname:$ssh_dir\n\r"
       expect {
              "*no)?" 
                  {send "yes\n\r"
                  expect "*ssword:" {send "$PASSWORD\n\r"}
                   }
             "*ssword:" {send "$PASSWORD\n\r"}
      }
      }
      }   
expect eof
EOF
done

转载于:https://my.oschina.net/112612/blog/1560133


http://www.niftyadmin.cn/n/4227210.html

相关文章

为我国特高压交直流混联大电网安上智慧大脑

调度是控制电网安全稳定运行的大脑&#xff0c;是维系电力生产过程的基础&#xff0c;是保障智能电网运行和发展的重要手段。相比于国外&#xff0c;我国电网调度控制技术研究起步较晚&#xff0c;但发展迅速&#xff0c;很多技术已达到世界领先水平。其中&#xff0c;2008年国…

【Docker实战之入门】Dockerfile详细分析(5):ENTRYPOINT和CMD命令的区别

ENTRYPOINT用法 entrypoint在dockerfile当中只有最后一条生效&#xff0c;其他entrypoint都不生效第一种写法类似数组形式&#xff0c;推荐使用。使用这种方法启动的进程的pid为1。第二章方法执行命令启动的进程&#xff0c;该进程的pid为执行完这个shell的pid。 CMD用法 第一种…

常用linux解压命令(原创)

tar-cvf myball.tar mydir将mydir目录打包。tar-tf myball.tar 查看myball.tar包中内容。tar-xvf myball.tar将myball.tar 在当前目录下解包tar-zcvf mybll.tar.gz mydir 将mydir目录打包后压缩调用gzip压缩工具tar-ztf myball.tar.gz 查看myball.tar.gz包中内容tar-zxvf mybal…

IdeaSVN下载完全安装idea配置使用

下载&#xff1a;https://tortoisesvn.net/downloads.zh.html 安装配置&#xff1a;https://www.cnblogs.com/iceywu/p/12376981.html 在桌面可以右键可以检出&#xff0c; 在ideal close project后也可以检出

把应用服务写成系统服务

应用服务写成系统服务&#xff0c;方便日后自动化管理 123456789101112131415161718192021222324252627282930313233343536#!/bin/bashservice_dir/path/to/file start() {ps ax | grep "APPLICATION" | grep -v grep >/dev/null 2>&1 if [ $? -eq 0 ];th…

域名购买以及云解析DNS

之前阿里购买的域名3年了&#xff0c;11月份就到期了。 转载自 https://blog.csdn.net/qq_38196524/article/details/111584011

Quartz可以用来做什么

线程框架Quartz可以用来做什么 转载https://www.cnblogs.com/leigepython/p/10729195.html https://blog.csdn.net/huihuimimi17/article/details/8215779

IDEA上传代码到gitee全过程

IDEA上传代码到gitee全过程 转https://www.pianshen.com/article/72361182789/