个人实验环境定制简易跳转功能

需求:由于工作需要,我自己的工作站上搭建有很多套实验环境,而在某种特定场景下,我需要通过默认连接的终端能随时跳转切换到其他环境下临时测试使用,因为只有我自己用,无需引入复杂的跳转机系统,也不需要审计。
环境:均为Linux服务器
实现:我想通过最简单的一些运维知识,搭建一套最简易的跳转程序,提升自己的工作效率。

1.梳理有跳转需求的环境信息

首先我默认连接的环境本身就是一套Oracle环境,默认也是以oracle用户连接的,IP地址为192.168.1.61。先梳理出目前所有需要纳入跳转清单的实验环境IP地址:
这里我编辑 /home/oracle/jump_server/server_list 文件,内容如下(这个输出格式优化调整过):

+-----------------------------+----------+---------------+
|           outline           | shortcut |  ip_address   |
+-----------------------------+----------+---------------+
| Oracle 9i                   |        9 | 192.168.1.9   |
+-----------------------------+----------+---------------+
| Oracle 10g                  |       10 | 192.168.1.10  |
+-----------------------------+----------+---------------+
| Oracle 11g - 11.2.0.3       |       11 | 192.168.1.11  |
| Oracle 11g - 11.2.0.4       |        x | 192.168.1.11  |
+-----------------------------+----------+---------------+
| Oracle 11g RAC              |       61 | 192.168.1.61  |
|                             |       63 | 192.168.1.63  |
+-----------------------------+----------+---------------+
| Open Source Software Test   |       99 | 192.168.1.99  |
+-----------------------------+----------+---------------+
| Oracle 11g RAC(xData)       |      107 | 192.168.1.107 |
|                             |      108 | 192.168.1.108 |
|                             |      109 | 192.168.1.109 |
+-----------------------------+----------+---------------+
| Oracle EM 13c(12.1.0.2 RAC) |      111 | 192.168.1.111 |
|                             |      112 | 192.168.1.112 |
+-----------------------------+----------+---------------+
| RHEL6.8(Test Only)          |      121 | 192.168.1.121 |
|                             |      122 | 192.168.1.122 |
|                             |      123 | 192.168.1.123 |
+-----------------------------+----------+---------------+
| Oracle 10g RAC              |      171 | 192.168.1.171 |
|                             |      173 | 192.168.1.173 |
+-----------------------------+----------+---------------+
| Oracle 19c RAC              |      193 | 192.168.1.193 |
|                             |      195 | 192.168.1.195 |
+-----------------------------+----------+---------------+

可以看到清楚的描述了环境的概要信息和对应IP地址。

2.加入用户的环境变量

编辑环境变量的配置文件,增加内容如下: vi ~/.bash_profile

###################
##  跳转主机列表
###################
alias j='cat /home/oracle/jump_server/server_list'
# Alias for my env:
alias 9='ssh 192.168.1.9'
alias 10='ssh 192.168.1.10'
alias 11='ssh 192.168.1.11'
alias x='ssh ora11204@192.168.1.11'
alias 61='ssh 192.168.1.61'
alias 63='ssh 192.168.1.63'
alias 99='ssh root@192.168.1.99'
alias 107='ssh 192.168.1.107'
alias 108='ssh 192.168.1.108'
alias 109='ssh 192.168.1.109'
alias 111='ssh 192.168.1.111'
alias 112='ssh 192.168.1.112'
alias 121='ssh root@192.168.1.121'
alias 122='ssh root@192.168.1.122'
alias 123='ssh root@192.168.1.123'
alias 171='ssh 192.168.1.171'
alias 173='ssh 192.168.1.173'
alias 193='ssh 192.168.1.193'
alias 195='ssh 192.168.1.195'

3.配置ssh无密码登陆

这点我有考虑过是否有必要?是否因配置了ssh无密码登陆会容易产生误操作?但结合自己的需求,在个人测试环境中还是需要这样的功能方便提升效率的。
配置/etc/hosts文件,添加:
vi /etc/hosts

# Alfred's Virtual Machine
192.168.1.9     vm9
192.168.1.10    vm10
192.168.1.11    vm11
192.168.1.61    vm61
192.168.1.63    vm63
192.168.1.99    vm99
192.168.1.107   vm107
192.168.1.108   vm108
192.168.1.109   vm109
192.168.1.111   vm111
192.168.1.112   vm112
192.168.1.121   vm121
192.168.1.122   vm122
192.168.1.123   vm123
192.168.1.171   vm171
192.168.1.173   vm173
192.168.1.193   vm193
192.168.1.195   vm195

这里可参考我之前的随笔:

上传cluster开头的2个脚本之后,配置环境变量,增加NODE_LIST变量值:

# NODE_LIST:
export NODE_LIST='vm9 vm10 vm11 vm61 vm63 vm99 vm107 vm108 vm109 vm111 vm112 vm121 vm122 vm123 vm171 vm173 vm193 vm195'

清除ssh互信的当前所有配置信息(根据情况选做):

cluster_run_all_nodes "hostname; rm -rf ~/.ssh"
rm -rf ~/.ssh

3.1 使用cluster_run_all_nodes脚本,快速在各节点ssh-keygen生成RSA密钥和公钥

cluster_run_all_nodes "hostname; ssh-keygen -q -t rsa  -N \"\" -f  ~/.ssh/id_rsa"

3.2 我需要使用shell结合for循环遍历数组,如下:

IP_NET="192.168.1."
a=(9 10 11 61 63 99 107 108 109 111 112 121 122 123 171 173 193 195)
for i in ${a[@]}
do
ssh $IP_NET$i cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
echo Summarize ssh info from $IP_NET$i into a single file.
done

3.3 安全性,分发确认无误:

chmod 600 ~/.ssh/authorized_keys
cluster_copy_all_nodes ~/.ssh/authorized_keys ~/.ssh/
cluster_run_all_nodes "hostname; date"

若配置成功,就不需要输入密码即可执行完成:

[oracle@jystdrac1 ~]$ cluster_run_all_nodes "hostname; date"
db09
Wed Dec 25 23:58:31 CST 2019
db10
Wed Dec 25 23:58:42 CST 2019
OEL-ASM
Wed Dec 25 23:58:28 CST 2019
jystdrac1
Wed Dec 25 23:58:47 CST 2019
jystdrac2
Wed Dec 25 23:58:48 CST 2019
省略输出...

此外,特别需要注意99这台机器由于安装多款开源产品,我为了方便是跳转到root用户的,这样切换到其他任意用户不需要输入密码,至于ssh互信这里需要特殊配置下,只需将99上的root用户输入ssh-keygen生成的id_rsa.pub文件内容追加到61上oracle用户下的authorized_keys,然后将61的这个文件再同步回99上root用户下authorized_keys即可。

4.展示跳转机使用效果

最后来看下跳转机实际使用的效果:

4.1 输入j显示可以跳转的服务器信息:

[oracle@jystdrac1 ~]$ j
+-----------------------------+----------+---------------+
|           outline           | shortcut |  ip_address   |
+-----------------------------+----------+---------------+
| Oracle 9i                   |        9 | 192.168.1.9   |
+-----------------------------+----------+---------------+
| Oracle 10g                  |       10 | 192.168.1.10  |
+-----------------------------+----------+---------------+
| Oracle 11g - 11.2.0.3       |       11 | 192.168.1.11  |
| Oracle 11g - 11.2.0.4       |        x | 192.168.1.11  |
+-----------------------------+----------+---------------+
| Oracle 11g RAC              |       61 | 192.168.1.61  |
|                             |       63 | 192.168.1.63  |
+-----------------------------+----------+---------------+
| Open Source Software Test   |       99 | 192.168.1.99  |
+-----------------------------+----------+---------------+
| Oracle 11g RAC(xData)       |      107 | 192.168.1.107 |
|                             |      108 | 192.168.1.108 |
|                             |      109 | 192.168.1.109 |
+-----------------------------+----------+---------------+
| Oracle EM 13c(12.1.0.2 RAC) |      111 | 192.168.1.111 |
|                             |      112 | 192.168.1.112 |
+-----------------------------+----------+---------------+
| RHEL6.8(Test Only)          |      121 | 192.168.1.121 |
|                             |      122 | 192.168.1.122 |
|                             |      123 | 192.168.1.123 |
+-----------------------------+----------+---------------+
| Oracle 10g RAC              |      171 | 192.168.1.171 |
|                             |      173 | 192.168.1.173 |
+-----------------------------+----------+---------------+
| Oracle 19c RAC              |      193 | 192.168.1.193 |
|                             |      195 | 192.168.1.195 |
+-----------------------------+----------+---------------+

4.2 选择输入对应主机即可实现跳转:
比如选择输入107,即可跳转到192.168.1.107的主机:

[oracle@jystdrac1 ~]$ 107
Last login: Wed Dec 25 22:31:16 2019 from db02
[oracle@db01 ~]$ ps -ef|grep pmon
grid     19592     1  0 Dec22 ?        00:00:52 asm_pmon_+ASM1
oracle   20315 20293  0 00:02 pts/0    00:00:00 grep --color=auto pmon
oracle   21339     1  0 Dec22 ?        00:00:58 ora_pmon_orcl1
[oracle@db01 ~]$ exit
logout
Connection to 192.168.1.107 closed.

比如选择输入193,即可跳转到192.168.1.193的主机:

[oracle@jystdrac1 ~]$ 193
Last login: Wed Dec 25 22:30:01 2019 from 192.168.1.61
[oracle@db193 ~]$ ps -ef|grep pmon
grid     16918     1  0 Nov07 ?        00:05:33 asm_pmon_+ASM1
oracle   17329     1  0 Nov07 ?        00:06:51 ora_pmon_jydb1
oracle   28698 28674  0 00:00 pts/0    00:00:00 grep --color=auto pmon
[oracle@db193 ~]$ exit
logout
Connection to 192.168.1.193 closed.

比如选择输入99,即可跳转到192.168.1.99的主机(因为之前特殊处理,所以可成功跳转到root用户):

[oracle@jystdrac1 ~]$ 99
Last login: Thu Dec 26 00:24:57 2019 from 192.168.1.61
[root@alfred ~]# ps -ef|grep postgres
postgres 17659     1  0 Dec23 ?        00:00:18 /usr/pgsql-10/bin/postmaster -D /var/lib/pgsql/10/data/
postgres 17662 17659  0 Dec23 ?        00:00:00 postgres: logger process   
postgres 17664 17659  0 Dec23 ?        00:00:00 postgres: checkpointer process   
postgres 17665 17659  0 Dec23 ?        00:00:04 postgres: writer process   
postgres 17666 17659  0 Dec23 ?        00:00:05 postgres: wal writer process   
postgres 17667 17659  0 Dec23 ?        00:00:15 postgres: autovacuum launcher process   
postgres 17668 17659  0 Dec23 ?        00:00:32 postgres: stats collector process   
postgres 17669 17659  0 Dec23 ?        00:00:00 postgres: bgworker: logical replication launcher   
root     19218 19196  0 00:28 pts/0    00:00:00 grep --color=auto postgres
[root@alfred ~]# su - postgres
Last login: Tue Dec 24 09:23:16 CST 2019 on pts/0
-bash-4.2$ psql
psql (10.11)
输入 "help" 来获取帮助信息.

postgres=# \q
-bash-4.2$ exit
登出
[root@alfred ~]# exit
logout
Connection to 192.168.1.99 closed.

可以看到,这样方便的跳转功能,使我可以快速连接到各类环境进行测试学习。

This entry was posted in 善事利器 and tagged , , . Bookmark the permalink.