简单使用!使用shell脚本实现ssh免密登录

目录

1.expect命令:可以在外输入,某些命令执行后弹出的输入提示

2.使用expect命令来创建本地的公钥文件

3.复制ssh的公钥文件到各个主机上:

4.完整的shell脚本:


1.expect命令:可以在外输入,某些命令执行后弹出的输入提示

spawn   shell 命令程序 
expect   "捕获到shell 命令程序执行之后输出的字符串"  
send  "发送给 shell 命令程序的字符串"
[root@localhost .ssh]# ssh-keygen -t rsa -b 1024
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? 
/usr/bin/expect << eof

spawn ssh-keygen -t rsa -b 1024

# 开始进连续捕获
# 获取“Overwrite (y/n)?”的提示行,输入“y”表示同意(其中\n是换行符)
expect	{
        ".ssh/id_rsa)"      { send "\n";  }
        "Overwrite (y/n)?"  { send "y\n"; }
}
# 结束捕获
eof

2.使用expect命令来创建本地的公钥文件

create_ssh_pub(){
echo "生成本地ssh公钥"
/usr/bin/expect << eof
# 设置捕获字符串后,期待回复的超时时间
set timeout 30

spawn ssh-keygen -t rsa -b 1024

## 开始进连续捕获
expect	{
        ".ssh/id_rsa)"      { send "\n";  exp_continue }
        "Overwrite (y/n)?"  { send "y\n"; exp_continue }
        "no passphrase):"   { send "\n";  exp_continue }
        "passphrase again:" { send "\n";  exp_continue }
}
eof
}
if [ ! -f /root/.ssh/id_rsa.pub ];then
	create_ssh_pub
fi

3.复制ssh的公钥文件到各个主机上:

我的shell脚本在本机master上

# 定义复制ssh公钥方法
copy_ssh(){
echo "复制公钥到对应的主机上"
/usr/bin/expect << eof
# 设置捕获字符串后,期待回复的超时时间
set timeout 30

spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $1@$2

## 开始进连续捕获
expect	{
        "connecting (yes/no)?" { send "yes\n";  exp_continue }
        "s password:"          { send "${ssh_passwd}\n"; exp_continue }
}
eof
}
for name in ${ssh_networkname[*]};do
	timeout 5 ssh root@${name} "echo ${name}: 'This is success!'"
	if [[ $? -ne 0 ]];then
		echo "复制文件到: ${name}"
		copy_ssh root ${name} > /dev/null
	fi
	
done

4.完整的shell脚本:

#! /bin/bash

ssh_hosts=(192.168.178.101 192.168.178.151 192.168.178.201)
ssh_networkname=(master node1 node2)
ssh_passwd=110119

# 定义修改/etc/hosts文件的方法
echo "开始修改地址映射"

for ((i=0;i<${#ssh_hosts[*]};i++));do
    sed -i '/^'"${ssh_hosts[$i]}"'/d' /etc/hosts
	echo "尝试连接: ${ssh_networkname[$i]}"
	timeout 5 ssh root@${ssh_networkname[$i]} "echo ${ssh_networkname[$i]}: 'This is success!'"
	if [[ $? -ne 0 ]];then
		echo "添加地址映射:${ssh_hosts[$i]} ${ssh_networkname[$i]}"
		echo "${ssh_hosts[$i]} ${ssh_networkname[$i]}" >> /etc/hosts
	fi
done

expect -v
if [ `echo $?` -ne 0 ];then
	echo "安装expect命令"
	yum install -y expect
fi 
create_ssh_pub(){
echo "生成本地ssh公钥"
/usr/bin/expect << eof
# 设置捕获字符串后,期待回复的超时时间
set timeout 30

spawn ssh-keygen -t rsa -b 1024

## 开始进连续捕获
expect	{
        ".ssh/id_rsa)"      { send "\n";  exp_continue }
        "Overwrite (y/n)?"  { send "y\n"; exp_continue }
        "no passphrase):"   { send "\n";  exp_continue }
        "passphrase again:" { send "\n";  exp_continue }
}
eof
}
if [ ! -f /root/.ssh/id_rsa.pub ];then
	create_ssh_pub
fi


# 定义复制ssh公钥方法
copy_ssh(){
echo "复制公钥到对应的主机上"
/usr/bin/expect << eof
# 设置捕获字符串后,期待回复的超时时间
set timeout 30

spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $1@$2

## 开始进连续捕获
expect	{
        "connecting (yes/no)?" { send "yes\n";  exp_continue }
        "s password:"          { send "${ssh_passwd}\n"; exp_continue }
}
eof
}
for name in ${ssh_networkname[*]};do
	timeout 5 ssh root@${name} "echo ${name}: 'This is success!'"
	if [[ $? -ne 0 ]];then
		echo "复制文件到: ${name}"
		copy_ssh root ${name} > /dev/null
	fi
	
done

你可能感兴趣的:(shell,ssh,linux,服务器)