|
#!/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
|
|