057-跳板机的shell小脚本

这个脚本须要仔细看看,实用价值不错,无密钥链接详情能够看(本博文S02)bash

执行的脚本的过程:app

1.生成公钥和密钥(统一用户jump)

[root@cnsz142728 profile.d]# su - jump
[jump@cnsz142728 ~]$ ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa    (生成密钥公钥)
Generating public/private dsa key pair.
Your identification has been saved in /home/jump/.ssh/id_dsa.
Your public key has been saved in /home/jump/.ssh/id_dsa.pub.
The key fingerprint is:
77:57:90:82:75:9b:7e:54:d5:e7:bc:15:86:de:b0:fe jump@cnsz142728
The key's randomart image is:
+--[ DSA 1024]----+
|           o. +.=|
|          . .+.*+|
|            ..Bo=|
|             + ++|
|        S . o o +|
|         . . o o |
|              .  |
|               E |
|                 |
+-----------------+


[jump@cnsz142728 ~]$ ssh-copy-id -i ~/.ssh/id_dsa.pub 10.25.172.91
The authenticity of host '10.25.172.91 (10.25.172.91)' can't be established.
RSA key fingerprint is 69:0c:9e:d9:dd:68:08:35:3b:1f:c4:40:c7:a9:7f:40.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.25.172.91' (RSA) to the list of known hosts.
jump@10.25.172.91's password: 
Now try logging into the machine, with "ssh '10.25.172.91'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[jump@cnsz142728 ~]$ ssh-copy-id -i ~/.ssh/id_dsa.pub 10.25.172.90
jump@10.25.172.90's password: 
Now try logging into the machine, with "ssh '10.25.172.90'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

 

2.将脚本添加到/etc/profile.d 文件中

[root@cnsz142728 profile.d]#echo '[ $UID -ne 0 ] && . /server/scripts/jump.sh'>/etc/profile.d/jump.sh  
[root@cnsz142728 profile.d]# cat jump.sh 
[ $UID -ne 0 ] && . /server/scripts/jump.sh

 

3. 查看脚本

[root@cnsz142728 scripts]# cat jump.sh 
#!/bin/bash
##by Carlton at 10171018,I am study by oldboy 
trapper(){
    trap ':' INT EXIT TSTP TERM HUP
}
main(){
while :
do
   trapper
   clear
   cat <<menu
    1)machine01-10.25.172.90
    2)machine02-10.25.172.91
menu
read -p "Pls input a num :" num
case "$num" in
 1)
     echo "login in 10.25.172.90"
     ssh 10.25.172.90
    ;;
 2)
     echo "login in 10.25.172.91"
     ssh 10.25.172.91
    ;;
 110)read -p "your birthday:" char
  if [ "$char" = "0819" ];then
     exit 
     sleep 5
  fi 
  ;;


*)
     echo "select error."
     exit
     esac
done
}
main

 

4.执行的过程

[root@cnsz142728 scripts]# su jump
    1)machine01-10.25.172.90
    2)machine02-10.25.172.91
Pls input a num :1
login in 10.25.172.90
WARNING: If you are not authorized to access this private computer system, disconnect now. All activities on this system will be monitored and recorded without prior notification or permission!
[jump@cnsz142726 ~]$ logout
Connection to 10.25.172.90 closed.
    1)machine01-10.25.172.90
    2)machine02-10.25.172.91
Pls input a num :2
login in 10.25.172.91
WARNING: If you are not authorized to access this private computer system, disconnect now. All activities on this system will be monitored and recorded without prior notification or permission!
[jump@cnsz142727 ~]$ logout
Connection to 10.25.172.91 closed.
    1)machine01-10.25.172.90
    2)machine02-10.25.172.91
Pls input a num :3
select error.
[root@cnsz142728 scripts]# su jump
    1)machine01-10.25.172.90
    2)machine02-10.25.172.91
Pls input a num :110
your birthday:0819
[root@cnsz142728 scripts]#

 

你们能够结合执行的结果进行脚本的理解,也能够运用到本身的生产机器当中,机器数量也能够增长!dom

相关文章
相关标签/搜索