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
#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
相关文章推荐
- shell script自动搭建简单的mysql主从(replication)
- shell script自动搭建简单的nfs服务器
- shell script自动搭建简单的pxe服务器
- shell script自动搭建简单的cacti监控
- shell script自动搭建简单的mail服务器(postfix+dovecot)
- shell script自动搭建简单的yum
- shell script自动搭建简单的linux系统初始化脚本
- shell script自动搭建简单的lamp环境
- shell script自动搭建简单的dhcp服务器
- 【自动驾驶】如何利用深度学习搭建一个最简单的无人驾驶系统
- hell script自动搭建简单的iscsi服务器
- MYSQL双机主从环境搭建(流程简单)
- 简单的hibernate环境搭建、自动生成model/配置/hibernate.xml配置文件
- shell script自动搭建简单的quota(磁盘配额)
- (最齐全)Jenkins+Maven+SVN搭建自动部署,自动测试环境
- 一个简单的dubbo项目搭建案例
- springmvc最简单的搭建,初学者必看
- spring-boot搭建简单web(整合freemarker)(二)
- automation - 使用Artifactory简单搭建本地library仓库
- Struts2开发环境搭建 附简单登录功能实例