使用跳板机的目的:我们的服务器都是云主机,使用的都是aws比较多,而且aws是以私钥去连接公钥,连接的方式都是以root权限连接,这样子的话,开发人员要连接上服务器的话,不可能给root权限直接连吧,否则会对服务器造成很大的威胁.

跳板机实现的思路:使用shell去编写,账户这块使用LDAP做集中式管理+认证,权限这块使用sudo集中式控制管理.

LDAP:部署可以使用yum,安装简单,不过安装的目录和结构都不同于一个位置.

也可以使用编译安装,编译安装的话,需要一定的时间,期间也会遇到很多错误,好处就是可以把编译的环境统一管理在相同目录中.

两者安装LDAP,迁移的时候,也非常容易.

(1)跳板机实现的过程:LDAP安装+部署.我管理的LDAP是通过WEB管理工具来管理.

 (2)编写shell菜单

(3)创建普通用户的私钥+公钥,并把公钥上传到对端的服务器的目录底下.

实现的截图:

 

 



       

脚本如下:

 
  
  1. [root@localhost jc]# cat tiaoban_deploy.sh 
  2.  
  3. #!/bin/bash 
  4.  
  5. ip=`/sbin/ifconfig eth0|grep "inet addr"|awk -F ":" '{print $2}'|awk '{print $1}'` 
  6.  
  7. current_date=`date +%Y-%m-%d-%H:%M:%S` 
  8.  
  9. dis_manumenu="<ip:${ip}-server>" 
  10.  
  11.  
  12.  
  13. user=`echo $USER` 
  14.  
  15. # Source function library. 
  16.  
  17. . /etc/rc.d/init.d/functions 
  18.  
  19.  
  20.  
  21. new_echo () { 
  22.  
  23.  
  24.  
  25. if [ -z "$2" ] 
  26.  
  27. then 
  28.  
  29.     COLOR=33 
  30.  
  31. elif [ $2 == red ] 
  32.  
  33. then 
  34.  
  35.     COLOR=31 
  36.  
  37. elif [ $2 == green ] 
  38.  
  39. then 
  40.  
  41.     COLOR=32 
  42.  
  43. elif [ $2 == blue ] 
  44.  
  45. then 
  46.  
  47.     COLOR=36 
  48.  
  49. elif [ $2 == purple ] 
  50.  
  51. then 
  52.  
  53.     COLOR=35 
  54.  
  55. elif [ $2 == white ] 
  56.  
  57. then 
  58.  
  59.     COLOR=39 
  60.  
  61. else 
  62.  
  63.     echo new_echo function use error 
  64.  
  65.     exit 
  66.  
  67. fi 
  68.  
  69.  
  70.  
  71. if [ -z "$4" ] 
  72.  
  73. then 
  74.  
  75.     COLOR1=33 
  76.  
  77. elif [ $4 == red ] 
  78.  
  79. then 
  80.  
  81.     COLOR1=31 
  82.  
  83. elif [ $4 == green ] 
  84.  
  85. then 
  86.  
  87.     COLOR1=32 
  88.  
  89. elif [ $4 == blue ] 
  90.  
  91. then 
  92.  
  93.     COLOR1=36 
  94.  
  95. elif [ $4 == purple ] 
  96.  
  97. then 
  98.  
  99.     COLOR1=35 
  100.  
  101. elif [ $4 == white ] 
  102.  
  103. then 
  104.  
  105.     COLOR1=39 
  106.  
  107. else 
  108.  
  109.     echo new_echo function use error 
  110.  
  111.     exit 
  112.  
  113.  
  114.  
  115. fi 
  116.  
  117. if [ -z "$3" ] 
  118.  
  119. then 
  120.  
  121.  
  122.  
  123.     echo -en "\\033[1;${COLOR}m""\033[3m$1 \033[0m\n" 
  124.  
  125. else 
  126.  
  127.     echo -en "\\033[1;${COLOR}m""\033[3m$1 \033[0m\033[70G\\033[1;${COLOR1}m\033[3m $3 \033[0m\n" 
  128.  
  129. fi 
  130.  
  131.  
  132.  
  133.  
  134. for signal in `seq 1 1000` 
  135.  
  136. do 
  137.  
  138.         trap ':' INT  EXIT TSTP TERM HUP $signal &> /dev/null 
  139.  
  140. done 
  141.  
  142.  
  143.  
  144. clear 
  145.  
  146.  
  147.  
  148. for signal in `name` 
  149.  
  150. do 
  151.  
  152.         trap trap ':' INT  EXIT TSTP TERM HUP $signal &> /dev/null 
  153.  
  154. done 
  155.  
  156.  
  157.  
  158. clear 
  159.  
  160.  
  161.  
  162.  
  163.  
  164. function getchar() 
  165.  
  166.  
  167. stty cbreak -echo  
  168.  
  169.         dd if=/dev/tty bs=1 count=1 2>/dev/null  
  170.  
  171.         stty -cbreak echo  
  172.  
  173. }  
  174.  
  175.  
  176.  
  177. ############################################################################### 
  178.  
  179. #while : 
  180.  
  181. #do 
  182.  
  183. name=`cat /opt/username` 
  184.  
  185.         clear 
  186.  
  187.         read -p "请输入登陆跳板机验证标识:" name 
  188.  
  189.  
  190.  
  191. repa=`cat /opt/username` 
  192.  
  193.          if [ "$name" ==  "$repa" ];then 
  194.  
  195.              action "认证标识正确,欢迎登陆跳板机。。。。。。。。。。。。。。。。。。。" /bin/true 
  196.  
  197.  
  198.  
  199. else 
  200.  
  201.          action "对不起,您输入的认证标识有错误,无法登陆跳板机,已退出............" /bin/false 
  202.  
  203.          exit  
  204.  
  205.          exit  
  206.  
  207. fi  
  208.  
  209. #        realuser=$(awk '{print $1}' $name)  
  210.  
  211. #        realpass=$(awk '{print $1}' $pass)  
  212.  
  213. #        if [ "$pass" == "$realpass" ]; then 
  214.  
  215. #          echo "欢迎登陆跳板机。。。。。。。。。。。" 
  216.  
  217. #else 
  218.  
  219. #          echo "对不起,您的输入有错误..............." 
  220.  
  221. #fi 
  222.  
  223. #done  
  224.  
  225.  
  226.  
  227.  
  228.  
  229. cat <<EOF 
  230.  
  231.               当前系统时间:$current_date                      
  232.  
  233.               ============================================================================= 
  234.  
  235.               $dis_manumenu 当前使用跳板机用户:$USER    版本:Versin1.0                          
  236.  
  237.               =============================================================================  
  238.  
  239.               ---------------------------跳板机帮助手册-------------------------------- 
  240.  
  241.               (1)菜单0选项,是退出整个跳板机. 
  242.  
  243.               (2)菜单1-11选项是服务器列表,这里只有11台服务器提供选择. 
  244.  
  245.               (3)跳板机上面服务器包含:邮件系统,redmine,开发应用服务. 
  246.  
  247.               ----------------------------------------------------------------------------- 
  248.  
  249.  
  250.  
  251.               **0)(exit)                                                           
  252.  
  253.               -->[--------------------公司跳板机菜单---------------------]:<--      
  254.  
  255.               **1)redmine-server(8)                    
  256.  
  257.               **2)数据分析打点测试服(18)                                             
  258.  
  259.               **3)测试服(11)                                          
  260.  
  261.               **4)日志测试服I(13)                                   
  262.  
  263.               **5)日志测试服II(22)                                     
  264.  
  265.               **6)测试服I(12)                                     
  266.  
  267.               **7)测试服II(16)                                     
  268.  
  269.               **8)内网SSO服务器(192)                                     
  270.  
  271.               **9)用户中心开发服(15)                                    
  272.  
  273.               **10)用户中心QA测试服(23)                                    
  274.  
  275.               **11)用户中心本地测试服(24)                                  
  276.  
  277.               **12)用户中心本地测试服(24)                                  
  278.  
  279.               ############################################################################## 
  280.  
  281.               ============================================================================== 
  282.  
  283. EOF 
  284.  
  285.  
  286.  
  287. read -p "请选择: " num 
  288.  
  289.  
  290.  
  291.  
  292.  
  293. case $num in 
  294.  
  295. 0) 
  296.  
  297. action "-------------------------exit----------------------------" /bin/true 
  298.  
  299. exit 
  300.  
  301.  
  302.  
  303. ;; 
  304.  
  305. 1) 
  306.  
  307. ip="192.168.3.8" 
  308.  
  309. public=/home/$USER 
  310.  
  311.  
  312.  
  313.  
  314.  
  315. if [ -z $CHECK ] 
  316.  
  317. then 
  318.  
  319.     clear 
  320.  
  321.     new_echo "----------------------------------------------------------------------------------" green 
  322.  
  323.     new_echo "Connection '$ip'($USER)          " purple "[ SUCCESS ]" green 
  324.  
  325.     new_echo "`date +'%x %X'`                                         " white 
  326.  
  327.     new_echo "--------------------------------- use ${SECONDS}s -----------------------------------------" green 
  328.  
  329.     action 用户:$user-"现在开始登陆,请稍等..............................." /bin/true 
  330.  
  331.     ssh -i "/home/$USER/.ssh/id_rsa" $user@$ip 
  332.  
  333. sleep 2 
  334.  
  335.   
  336.  
  337.  
  338.  
  339. fi 
  340.  
  341.  
  342.  
  343. ;; 
  344.  
  345. 2) 
  346.  
  347.  
  348.  
  349.   
  350.  
  351. ip="192.168.3.18" 
  352.  
  353. public=/home/$USER 
  354.  
  355. if [ -z $CHECK ] 
  356.  
  357. then 
  358.  
  359.     clear 
  360.  
  361. new_echo "----------------------------------------------------------------------------------" green 
  362.  
  363. new_echo "Connection '$ip'($USER)          " purple "[ SUCCESS ]" green 
  364.  
  365. new_echo "`date +'%x %X'`                                         " white 
  366.  
  367. new_echo "--------------------------------- use ${SECONDS}s -----------------------------------------" green 
  368.  
  369. action 用户:$user-"现在开始登陆,请稍等..............................." /bin/true 
  370.  
  371. ssh -i "/home/$USER/.ssh/id_rsa" $user@$ip  
  372.  
  373.  
  374.  
  375. fi 
  376.  
  377. ;; 
  378.  
  379.  
  380.  
  381. 3) 
  382.  
  383.   
  384.  
  385. ip="192.168.3.11" 
  386.  
  387. public=/home/$USER 
  388.  
  389.  
  390.  
  391. if [ -z $CHECK ] 
  392.  
  393. then 
  394.  
  395.     clear 
  396.  
  397. new_echo "----------------------------------------------------------------------------------" green 
  398.  
  399. new_echo "Connection '$ip'($USER)          " purple "[ SUCCESS ]" green 
  400.  
  401. new_echo "`date +'%x %X'`                                         " white 
  402.  
  403. new_echo "--------------------------------- use ${SECONDS}s -----------------------------------------" green 
  404.  
  405. action 用户:$user-"现在开始登陆,请稍等..............................." /bin/true 
  406.  
  407. ssh -i "/home/$USER/.ssh/id_rsa" $user@$ip 
  408.  
  409. fi 
  410.  
  411.  
  412.  
  413. ;; 
  414.  
  415. 4) 
  416.  
  417.   
  418.  
  419. ip="192.168.3.13" 
  420.  
  421. public=/home/$USER 
  422.  
  423.  
  424.  
  425. if [ -z $CHECK ] 
  426.  
  427. then 
  428.  
  429.     clear 
  430.  
  431. new_echo "----------------------------------------------------------------------------------" green 
  432.  
  433. new_echo "Connection '$ip'($USER)          " purple "[ SUCCESS ]" green 
  434.  
  435. new_echo "`date +'%x %X'`                                         " white 
  436.  
  437. new_echo "--------------------------------- use ${SECONDS}s -----------------------------------------" green 
  438.  
  439. action 用户:$user-"现在开始登陆,请稍等..............................." /bin/true 
  440.  
  441. ssh -i "/home/$USER/.ssh/id_rsa" $user@$ip 
  442.  
  443. fi 
  444.  
  445.  
  446.  
  447. ;; 
  448.  
  449. 5) 
  450.  
  451.   
  452.  
  453. ip="192.168.3.22" 
  454.  
  455. public=/home/$USER 
  456.  
  457. if [ -z $CHECK ] 
  458.  
  459. then 
  460.  
  461.     clear 
  462.  
  463. new_echo "----------------------------------------------------------------------------------" green 
  464.  
  465. new_echo "Connection '$ip'($USER)          " purple "[ SUCCESS ]" green 
  466.  
  467. new_echo "`date +'%x %X'`                                         " white 
  468.  
  469. new_echo "--------------------------------- use ${SECONDS}s -----------------------------------------" green 
  470.  
  471. action 用户:$user-"现在开始登陆,请稍等..............................." /bin/true 
  472.  
  473. ssh -i "/home/$USER/.ssh/id_rsa" $user@$ip 
  474.  
  475. fi 
  476.  
  477.  
  478.  
  479. ;; 
  480.  
  481. 6) 
  482.  
  483.   
  484.  
  485. ip="192.168.3.12" 
  486.  
  487. public=/home/$USER 
  488.  
  489.  
  490.  
  491. if [ -z $CHECK ] 
  492.  
  493. then 
  494.  
  495.     clear 
  496.  
  497. new_echo "----------------------------------------------------------------------------------" green 
  498.  
  499. new_echo "Connection '$ip'($USER)          " purple "[ SUCCESS ]" green 
  500.  
  501. new_echo "`date +'%x %X'`                                         " white 
  502.  
  503. new_echo "--------------------------------- use ${SECONDS}s -----------------------------------------" green 
  504.  
  505. action 用户:$user-"现在开始登陆,请稍等..............................." /bin/true 
  506.  
  507. ssh -i "/home/$USER/.ssh/id_rsa" $user@$ip 
  508.  
  509. fi 
  510.  
  511.  
  512.  
  513. ;; 
  514.  
  515. 7) 
  516.  
  517.   
  518.  
  519. ip=192.168.3.16 
  520.  
  521. public=/home/$USER 
  522.  
  523.  
  524.  
  525. if [ -z $CHECK ] 
  526.  
  527. then 
  528.  
  529.     clear 
  530.  
  531. new_echo "----------------------------------------------------------------------------------" green 
  532.  
  533. new_echo "Connection '$ip'($USER)          " purple "[ SUCCESS ]" green 
  534.  
  535. new_echo "`date +'%x %X'`                                         " white 
  536.  
  537. new_echo "--------------------------------- use ${SECONDS}s -----------------------------------------" green 
  538.  
  539. action 用户:$user-"现在开始登陆,请稍等..............................." /bin/true 
  540.  
  541. ssh -i "/home/$USER/.ssh/id_rsa" $user@$ip 
  542.  
  543. fi 
  544.  
  545.  
  546.  
  547. ;; 
  548.  
  549. 8) 
  550.  
  551.   
  552.  
  553. ip=192.168.3.92 
  554.  
  555. public=/home/$USER 
  556.  
  557.  
  558.  
  559. if [ -z $CHECK ] 
  560.  
  561. then 
  562.  
  563.     clear 
  564.  
  565. new_echo "----------------------------------------------------------------------------------" green 
  566.  
  567. new_echo "Connection '$ip'($USER)          " purple "[ SUCCESS ]" green 
  568.  
  569. new_echo "`date +'%x %X'`                                         " white 
  570.  
  571. new_echo "--------------------------------- use ${SECONDS}s -----------------------------------------" green 
  572.  
  573. action 用户:$user-"现在开始登陆,请稍等.............................." /bin/true 
  574.  
  575. ssh -i "/home/$USER/.ssh/id_rsa" $user@$ip 
  576.  
  577. fi 
  578.  
  579. ;; 
  580.  
  581.  
  582.  
  583. 9) 
  584.  
  585.   
  586.  
  587. ip=192.168.3.15 
  588.  
  589. public=/home/$USER 
  590.  
  591.  
  592.  
  593. if [ -z $CHECK ] 
  594.  
  595. then 
  596.  
  597.     clear 
  598.  
  599. new_echo "----------------------------------------------------------------------------------" green 
  600.  
  601. new_echo "Connection '$ip'($USER)          " purple "[ SUCCESS ]" green 
  602.  
  603. new_echo "`date +'%x %X'`                                         " white 
  604.  
  605. new_echo "--------------------------------- use ${SECONDS}s -----------------------------------------" green 
  606.  
  607. action 用户:$user-"现在开始登陆,请稍等..............................." /bin/true 
  608.  
  609. ssh -i "/home/$USER/.ssh/id_rsa" $user@$ip 
  610.  
  611. fi 
  612.  
  613.  
  614.  
  615. ;; 
  616.  
  617. 10) 
  618.  
  619.   
  620.  
  621. ip=192.168.3.23 
  622.  
  623. public=/home/$USER 
  624.  
  625.  
  626.  
  627. if [ -z $CHECK ] 
  628.  
  629. then 
  630.  
  631.     clear 
  632.  
  633. new_echo "----------------------------------------------------------------------------------" green 
  634.  
  635. new_echo "Connection '$ip'($USER)          " purple "[ SUCCESS ]" green 
  636.  
  637. new_echo "`date +'%x %X'`                                         " white 
  638.  
  639. new_echo "--------------------------------- use ${SECONDS}s -----------------------------------------" green 
  640.  
  641. action 用户:$user-"现在开始登陆,请稍等..............................." /bin/true 
  642.  
  643. ssh -i "/home/$USER/.ssh/id_rsa" $user@$ip 
  644.  
  645. fi 
  646.  
  647.  
  648.  
  649. ;; 
  650.  
  651. 11) 
  652.  
  653.   
  654.  
  655. ip=192.168.3.24 
  656.  
  657. public=/home/$USER 
  658.  
  659.  
  660.  
  661. if [ -z $CHECK ] 
  662.  
  663. then 
  664.  
  665.     clear 
  666.  
  667. new_echo "----------------------------------------------------------------------------------" green 
  668.  
  669. new_echo "Connection '$ip'($USER)          " purple "[ SUCCESS ]" green 
  670.  
  671. new_echo "`date +'%x %X'`                                         " white 
  672.  
  673. new_echo "--------------------------------- use ${SECONDS}s -----------------------------------------" green 
  674.  
  675. action 用户:$user-"现在开始登陆,请稍等..............................." /bin/true 
  676.  
  677. ssh -i "/home/$USER/.ssh/id_rsa" $user@$ip 
  678.  
  679. fi 
  680.  
  681.  
  682.  
  683. ;; 
  684.  
  685.   
  686.  
  687. 12) 
  688.  
  689.   
  690.  
  691. ip=192.168.3.25 
  692.  
  693. public=/home/$USER 
  694.  
  695.  
  696.  
  697. if [ -z $CHECK ] 
  698.  
  699. then 
  700.  
  701.     clear 
  702.  
  703. new_echo "----------------------------------------------------------------------------------" green 
  704.  
  705. new_echo "Connection '$ip'($USER)          " purple "[ SUCCESS ]" green 
  706.  
  707. new_echo "`date +'%x %X'`                                         " white 
  708.  
  709. new_echo "--------------------------------- use ${SECONDS}s -----------------------------------------" green 
  710.  
  711. action 用户:$user-"现在开始登陆,请稍等..............................." /bin/true 
  712.  
  713. ssh -i "/home/$USER/.ssh/id_rsa" $user@$ip 
  714.  
  715. fi 
  716.  
  717. ;; 
  718.  
  719.   
  720.  
  721.   
  722.  
  723. *) 
  724.  
  725. clear 
  726.  
  727. action 账户:$USER-"您的输入有误,已退出,请重新登录,谢谢................" /bin/true 
  728.  
  729. exit 
  730.  
  731.   
  732.  
  733.  
  734.  
  735. ;; 
  736.  
  737. esac 
  738.  
  739.