iterm2 保存ssh账号自动连接

作为一名coder,我们经常需要连接服务器进行一些操作,然而冗长的ssh密码登录属实有些繁琐。如果你使用了终端工具iTerm2,便可以事半功倍!

iTerm2具有很多优点

  1. 智能选中,双击选中,三击选中整行,四击智能选中;
  2. 全文查找 command + f
  3. 窗口垂直command + d、水平command + shift + d 拆分;
  4. command + ;自动补齐命令
  5. 记录历史输入命令 command + shift + h等等。

官网地址

1. expect脚本

首先,我们写一个expect 脚本,放在/usr/local/bin目录下,

cd /usr/local/bin

创建expect 脚本文件,

touch iterm2login.exp

vim iterm2login.exp,编辑脚本内容,内容如下,

#!/usr/bin/expect

set timeout 30
spawn ssh [lindex $argv 0]@[lindex $argv 1]
expect {
       "(yes/no)?"
       {send "yes\n";exp_continue}
       "password:"
       {send "[lindex $argv 2]\n"}
}
interact

wq 保存退出,然后还有最后一步。 给脚本添加读写权限,在当前目录执行

sudo chmod -R 777 iterm2login.exp

注意,如果没有给脚本添加权限,你可能会提示以下报错
permission denied: iterm2login.exp

OK,脚本完成。

2. 配置profile

command + o打开profile,配置Send text at start如下,

iterm2login.exp username ipAddress password

如,

iterm2login.exp root 192.168.1.1 test123

iterm2login.exp后面为三个参数。

profile配置描述
3.注意
如果密码中有特殊字符,需要使用转义符:

比如:

iterm2login.exp root 17.17.10.10 csdn@iterm2!?#$

如上配置,密码中的特殊字符则不会被sh 识别,需要改为:

iterm2login.exp root 17.17.10.10 csdn@iterm2\!\?\#\$

常见转义字符:

\'
\"
\*
\?
\\
\~
\`
\!
\#
\$
\&
\|
\{
\}
\;
\<
\>
\^

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逸尘️

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值