ssh免密登录脚本
ssh 脚本 登录 免密
2023-06-13 09:12:49 时间
yum -y install expect
#!/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}
相关文章
- Windows 下 VSCode 使用 SSH 连接报 Bad owner or permissions on C:UsersAdministrator/.ssh/config 错误问题解决
- 利用shell脚本通过ssh绕过输入密码直接登录主机详解程序员
- SSH 网站制作之AJAX无刷新动态更新页面详解编程语言
- 配置 Linux 服务器 SSH 远程登录(linux配置ssh)
- Linux开启SSH安全远程访问(linux开启ssh)
- 使用 Linux 系统设置 SSH 客户端连接(ssh客户端linux)
- 使用SSH命令控制Linux服务器(ssh命令linux)
- 密钥保护Linux服务器:存储SSH密钥的正确姿势(linux保存ssh)
- 服务器重启Linux服务器:通过SSH连接来实现(ssh重启linux)
- Linux下开启SSH登录,实现安全连接(linux允许ssh登录)
- 轻松实现:通过 SSH 登录 MySQL(ssh登录mysql)
- 如何使用SSH安装Linux系统(ssh安装linux)
- Linux之神秘:SSH的奥秘(linux软件ssh)
- 通过CMD使用SSH连接远程MySQL数据库(cmd连接远程mysql)
- SSH连接Linux:探索无限可能(ssh登录linux)
- Linux连接SSH:极速体验架构分享(linux连接ssh)
- SSH远程登录Redis数据库(ssh登录 redis)
- 使用SSH登录Redis服务器(ssh 登陆redis)
- 使用SSH远程查看Redis状态(ssh查看redis)
- 突破物理距离的Oracle网络SSH通道(oracle ssh通道)
- perl脚本实现限制ssh最大登录次数(支持白名单)