您的位置:首页 > 运维架构 > Shell

shell script自动搭建简单的双机ssh认证

2013-03-04 17:26 411 查看
#!/bin/bash

#Shell name: login_ssh.sh

#

#program:

# use key-gen to login other pc without password

#Author:pero

#Email: perofu.com@gmail.com

#

#History:

# 2012/7/21

#

#If you have any opinion please contact me

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:.

export PATH

SHELL_NAME=$(basename $0)

#Add

if [ "$#" -ne "0" ]

then

if [ "$#" -ne "2" ]

then

echo "Usage:"

echo "A: ./${SHELL_NAME}"

echo "B: ./${SHELL_NAME} username ip"

echo

exit 77

else

user=$1

ip=$2

fi

else

read -p "Please input a user name in Server that is you want to ssh:" user

read -p "Please input IP in Server that is you want to ssh:" ip

fi

#read -p "Please input a user name in Server that is you want to ssh:" user

#read -p "Please input IP in Server that is you want to ssh:" ip

#test prot 22 status

nc -w2 ${ip} 22

if [ "$?" -ne "0" ]

then

echo "$ip port 22 isn't opening,Please check"

exit 77

fi

#install openssh

#if [ $user = "root" ]

#then

# echo "Don't using root to login others PC,It's dangers!!!"

# exit 77

#fi

soft=$(rpm -qa |grep openssh | wc -l)

if [ $soft -lt 3 ]

then

yum install *openssh* -y &> /dev/null

fi

#create key

ssh-keygen -t rsa

echo "Please input password to check ssh-server have [~/.ssh/authorized_keys]:"

exist=$(ssh "$user"@"$ip" 'ls -a ~/.ssh/authorized_keys' &> /dev/null )

if [ $? = "0" ]

then

echo "Please input password twice to add infomation to [~/.ssh/authorized_keys] in ssh-server:"

scp ~/.ssh/id_rsa.pub "$user"@"$ip":~/authorized_keys &> /dev/null

ssh "$user"@"$ip" 'cat ~/authorized_keys >> ~/.ssh/authorized_keys; rm -rf ~/authorized_keys'

else

echo "Please input password twice to add infomation to [~/.ssh/authorized_keys] in ssh-server:"

ssh "$user"@"$ip" 'mkdir -p ~/.ssh/'

scp ~/.ssh/id_rsa.pub "$user"@"$ip":~/.ssh/authorized_keys &> /dev/null

fi

本文出自 “无咎” 博客,请务必保留此出处http://perofu.blog.51cto.com/6061242/1144590
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: