前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ssh免密登录脚本

ssh免密登录脚本

作者头像
summerking
发布2022-09-19 12:00:16
7140
发布2022-09-19 12:00:16
举报
文章被收录于专栏:summerking的专栏summerking的专栏

yum -y install expect

代码语言:javascript
复制
#!/bin/bash
#write by ddcw at 20200410
thiscript=$0
function exits(){
  echo -e "[`date +%Y%m%d-%H:%M:%S`] \033[31;40m$1\033[0m"
  exit 0
}
function install_sshNopasswd(){
	[ -f /usr/bin/sshNopasswd ] && exits "this OS has /usr/bin/sshNopasswd"
	tail -n +19 ${thiscript} > /usr/bin/sshNopasswd
	chmod 777 /usr/bin/sshNopasswd
	echo -e "[`date +%Y%m%d-%H:%M:%S`] [\033[32;40mINSTALL FINISH\033[0m] \033[31;40m you can run \033[0m \033[32;40msshNopasswd -h\033[0m \033[31;40mto get help\033[0m"
	exit 0 
}
[ -z $1 ] && install_sshNopasswd



#!/bin/env bash
#write by ddcw at 20200410


dt=$(date +%Y%m%d-%H%M%S)

function get_ssh_keygen() {
	tpe=$1
        expect << EOF
        set timeout 30
        spawn  /usr/bin/ssh-keygen -t ${tpe}
        expect {
                        "sa):" {send "\r";exp_continue}
                        "passphrase):" {send "\r";exp_continue}
                        "again:" {send "\r"}
        }
        expect eof
EOF
}
function scp_file_auto(){
        [ $# -eq 3 ] || echo_color red "script has internal err DDCW_0001"
        password=$3
        dir_tmp=$1
        host_and_dir=$2
        expect << EOF
        set timeout 30
        spawn scp ${dir_tmp} ${host_and_dir}
        expect {
                        "(yes/no" {send "yes\r";exp_continue}
                        "password:" {send "${password}\r"}
        }
        expect eof
EOF
}
function ssh_command(){
#        [ $# -eq 3 ] || echo_color red "script has internal err DDCW_0003"
        user=`echo $1 | awk -F "@" '{print $1}'` ||  echo_color red "script has internal err DDCW_0004"
        user_host=$1
        commd=$2
        password=$3
        expect << EOF
        set timeout 30
        spawn ssh ${user_host} ${commd}
        expect {
                        "(yes/no" {send "yes\r";exp_continue}
                        "password:" {send "${password}\r"}
        }
        expect "${user}@*" {send "exit\r"}
        expect eof
EOF
}


function help_this_script() {
	echo '---------------------------------------'
	echo 'sshNopasswd [USER]@HOSTNAME [PASSWORD] '
	echo "example: sshNopasswd $(whoami)@$(last | head -1 | awk '{print $3}') "
	echo '---------------------------------------'
	exit 0
}

case $1 in
	-h|-H|h|H|help|HELP|-help|-HELP|--help|--HELP|help=y|HELP=Y|?|-?)
		help_this_script;;
esac
	
if [ ! -f ~/.ssh/id_rsa ]
then
mv ~/.ssh ~/.ssh${dt}
get_ssh_keygen rsa
get_ssh_keygen dsa
fi
if [ ! -f ~/.ssh/id_rsa.pub ]
then
mv ~/.ssh ~/.ssh${dt}
get_ssh_keygen rsa
get_ssh_keygen dsa
fi

if [ ! -f ~/.ssh/id_dsa ]
then
mv ~/.ssh ~/.ssh${dt}
get_ssh_keygen dsa
get_ssh_keygen rsa
fi
if [ ! -f ~/.ssh/id_dsa.pub ]
then
mv ~/.ssh ~/.ssh${dt}
get_ssh_keygen dsa
get_ssh_keygen rsa
fi

[ -f ~/.ssh${dt}/authorized_keys ] && cp ~/.ssh${dt}/authorized_keys ~/.ssh/authorized_keys


ssh_rsa_pub=$(cat  ~/.ssh/id_rsa.pub | awk '{print $1 " " $2}')
ssh_dsa_pub=$(cat  ~/.ssh/id_dsa.pub | awk '{print $1 " " $2}')

[ -z ${2} ] && read -t 60 -p "please input ${1} password:" password
[ -z ${2} ] || export password=$2

ssh_command $1 'mkdir -p touch ~/.ssh' ${password}
ssh_command $1 '\[ -f ~/.ssh/authorized_keys \] || touch ~/.ssh/authorized_keys' ${password}
ssh_command $1 " grep '${ssh_rsa_pub}' ~/.ssh/authorized_keys >/dev/null || echo '${ssh_rsa_pub}' >> ~/.ssh/authorized_keys" ${password}
ssh_command $1 " grep '${ssh_dsa_pub}' ~/.ssh/authorized_keys >/dev/null || echo '${ssh_dsa_pub}' >> ~/.ssh/authorized_keys" ${passwd}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-07-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档