SSH免密登录SHELL安装脚本
2021-04-07 08:18
597 查看
SSH 免密登录 SHELL安装脚本
使用方法:执行安装脚本sshNopasswd_install.sh 即可生成 /usr/bin/sshNopasswd 这个脚本。(点击下载,或者文末自己复制也行)
这个功能也被ddcwRPM包包含
sshNopasswd [用户名@]主机名 [密码]
sh sshNopasswd_install.sh #只有第一次安装的时候才执行,安装好了,后面就可以直接用了 sshNopasswd 192.168.101.171 #用户名和SSH一样,可以省略,默认是当前用户,不跟密码的话,就会提示输入密码(推荐) ssh 192.168.101.171 #这台服务器连接192.168.101.171 现在及以后登录都不用输密码了(除非自己的密钥换了)
例子:
安装脚本,sshNopasswd连接服务器,并输入密码
测试:
之后ssh连接该服务器就不用输密码了
安装脚本sshNopasswd_install.sh内容如下:
#!/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}
相关文章推荐
- shell脚本自动 进行配置免密登录,安装java环境,安装tomcat
- Shell脚本实现ssh免密登录及批量配置管理 推荐
- 多级菜单系统安装维护shell脚本实现企业级案例
- Linux下安装Redis php-redis扩展 redis重启shell脚本 超详细!
- 通过脚本案例学习shell(四) --- 通过脚本初始化并加固新安装的Linux系统 推荐
- shell脚本浮点数,整数运算;for循环1加到100;Ubuntu12.04LTS 启动界面添加root登录;查找截取循环求和
- Shell实现多级菜单系统安装维护脚本实例分享
- shell脚本一键安装与配置ftp
- shell 之 lnmp(nginx-1.0.5+php-fpm5.3.6+mysql)安装脚本
- shell脚本:lnmp等源码安装脚本
- 用SHELL脚本自动化安装Nagios服务器端和客户端的
- 面试笔试-脚本-1:使用shell脚本输出登录次数最多的用户
- shell 脚本安装PHP扩展的简单方法
- Linux自动安装JDK的shell脚本
- 利用Shell脚本来监控Linux系统的负载、CPU、内存、硬盘、用户登录数
- shell脚本:LAMP一键自动安装 (linux,apache,mysql,php)
- Shell脚本实现检查服务器安全状态(用户、登录IP、防火墙检查)
- shell 脚本安装tomcat
- [linux] shell脚本编程-xunsearch安装脚本学习
- 使用shell脚本一键安装zabbix并配置客户端的自动发现选项