Summer's Blog
😈酷炫主页
✨运维
🎉安装
👀踩坑
  • k8s
  • shell
  • python
  • redis
  • elasticsearch
  • mysql
  • ceph
  • spark
  • 关于
  • 思维
  • 命令
  • 友链
  • 分类
  • 标签
  • 归档
👨‍👩‍👦‍👦腾讯云社区
🗣GitHub

Summer———夏苏文

💨运维界的前行者
😈酷炫主页
✨运维
🎉安装
👀踩坑
  • k8s
  • shell
  • python
  • redis
  • elasticsearch
  • mysql
  • ceph
  • spark
  • 关于
  • 思维
  • 命令
  • 友链
  • 分类
  • 标签
  • 归档
👨‍👩‍👦‍👦腾讯云社区
🗣GitHub
  • K8s

  • shell

    • 自动重启应用脚本
    • 自动重启docker脚本
    • 安装java脚本
    • 安装mysql脚本
    • 安装elasticsearch脚本
    • 安装redis脚本
    • 清理docker日志脚本
    • 钉钉报警脚本
    • 彩色进度条脚本
    • 奇数行和偶数行合并
    • 备份mysql脚本
    • cpu硬盘报警脚本
    • 安装node-exporter脚本
    • 批量ping探测网段ip
    • 一键安装VNC脚本
    • ssh免密登录脚本
    • 创建10个文件并写入内容
    • 模拟linux内存占用脚本
  • spark

  • Python

  • Redis

  • ceph

  • Elasticsearch

  • Mysql

  • 学习
  • shell
summer
2020-07-22

ssh免密登录脚本

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}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
上次更新: 1/25/2021, 6:15:49 PM
一键安装VNC脚本
创建10个文件并写入内容

← 一键安装VNC脚本 创建10个文件并写入内容→

最近更新
01
ceph块设备使用iscsi
03-29
02
Citrix设置vm开机自启动
02-17
03
ntp与chrony时间同步
12-17
更多文章>
Theme by Vdoing | Copyright © 2019-2023 夏苏文 | MIT License

网站已在灾难中运行:

蜀ICP备2022029853号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式