Mac-fastssh快速登跳转机

公司一个大牛的

#!/usr/bin/expect
set timeout 20
set dest [lindex $argv 0]

# login jump3
spawn ssh wenbronk@sss.oss.le.cn -p 502
expect {
    "Connection refused" exit
    "Name or service not known" exit
    "continue connecting" {send "yes\r";exp_continue}
    "password:" {send "password\r"}
    "Last login" {send_user "login jump successful"}
}

if { $dest == "hive" } {
    set user "bronkwen"
    set ip "10.112.32.15"
    set password "b0GBH28"
} elseif { $dest == "rdp" } {
    set user "rdp"
    set ip "10.183.93.1"
} elseif { $dest == "asdf" } {
    set user "root"
    set ip "10.185.29.2"
    set password "SJevL6"
} elseif { $dest == "redis" } {
    set user "rot"
    set ip "10.1.96.87"
    set password "E3vQC92"
} elseif { $dest == "rdp_h" } {
    set user "bronkwen"
    set ip "10.183.3.1"
    set password "b28"
} elseif { $dest == "storm" } {
    set user "slog"
    set ip "10.110.155.8"
    set password ".com"
} else {
   interact
   exit
}

expect "*:h<Enter>*"
send ":"
expect "*:*"
send_user "login $ip by user:$user\n"
send "ssh $user@$ip\r"
expect {
    "Connection refused" exit
    "Name or service not known" exit
    "continue connecting" {send "yes\r";exp_continue}
    "password:" {send "$password\r"}
    "Last login" {send_user "login $ip by user:$user\n successful"}
}

set rdp_ip [lindex $argv 1]
if { $dest != "rdp" || $rdp_ip == "" } {
   interact
   exit
}

if { $rdp_ip == "mongo" } {
    set rdp_ip "10.183.6.193"
} elseif { $rdp_ip == "canal1" } {
    set rdp_ip "10.110.1.35"
} elseif { $rdp_ip == "canal2" } {
    set rdp_ip "10.110.1.36"
}

send "ssh root@$rdp_ip\r"
expect {
    "Connection refused" exit
    "Name or service not known" exit
    "continue connecting" {send "yes\r";exp_continue}
    "Last login" {send_user "login $ip by user:$user\n successful"}
}
interact

 

精简了下, 用来登陆我的虚拟机

#!/usr/bin/expect                                                                                                                                                                                               
set timeout 20
set dest [lindex $argv 0]

set user "root"
set password "root"

if { $dest == "node1" } {
    set ip "192.168.199.115"
} elseif { $dest == "rdp" } {
    set ip "192.168.199.176"
} elseif { $dest == "asdf" } {
    set ip "192.168.199.189"
} elseif { $dest == "redis" } {
    set ip "192.168.199.181"
} else {
   echo unkonw server
   interact
   exit
}

spawn ssh $user@$ip -p 22
expect {
    "Connection refused" exit
    "Name or service not known" exit
    "password" {send "$password\r" }
    "continue connecting" {send "yes\r";exp_continue}
    "Last login" {send_user "login $ip by user:$user\n successful"}
}
interact

 

把文件命令为  fastss, 放在  ~/soft/path/fastssh, 然后修改  /etc/profile, 增加环境变量

之后就可以在任意地方使用   `fastssh node1`  的方式来登录了

 

posted @ 2022-06-07 10:43  bronk  阅读(137)  评论(0编辑  收藏  举报