mac iterm2 自动登录服务器

由于mac还不支持xshell,xshell有按钮功能能快速登录服务器,iterm2是mac常用的ssh客户端,下面,介绍如何利用iterm2快速登录服务器。

1. 登录远程服务器

#!/usr/bin/expect
set timeout -1
spawn ssh -p [lindex $argv 0] [lindex $argv 1]@[lindex $argv 2]
expect {
        "(yes/no)?"
        {send "yes\n";exp_continue}
        "password:"
        {send "[lindex $argv 3]\n"}
}
interact #允许用户交互

2. 登录跳转机后跳开发机

#!/usr/bin/expect
set timeout -1
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
# 跳转到跳转机
spawn ssh -p 22 [email protected]
expect "password:"
send "123456\r"
expect "Opt or ID>:"
send "0\r"
sleep 2 #等待日志打印完,否则很难捕捉到信息,报各种奇怪的问题,这里踩坑最多。
# 跳转到开发机
expect "*rank_dev*" {send "ssh $user@$ip\r"}
expect "*password:*" {send -- "$password\r"}
interact

3. 添加脚本到Profile中

4. expect指令

命令 作用
send 用于向进程发送字符串
expect 从进程接收字符串
spawn 启动新的进程
interact 允许用户交互
发布了126 篇原创文章 · 获赞 219 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/yz930618/article/details/103037280