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

搭建ssh无密码远程登录的安全(ssl)shell script

2012-08-27 00:00 459 查看
#!/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

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

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell script