作为一名coder,我们经常需要连接服务器进行一些操作,然而冗长的ssh密码登录属实有些繁琐。如果你使用了终端工具iTerm2,便可以事半功倍!
iTerm2具有很多优点:
- 智能选中,双击选中,三击选中整行,四击智能选中;
- 全文查找
command + f
; - 窗口垂直
command + d
、水平command + shift + d
拆分; command + ;
自动补齐命令- 记录历史输入命令
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后面为三个参数。
3.注意
如果密码中有特殊字符,需要使用转义符:
比如:
iterm2login.exp root 17.17.10.10 csdn@iterm2!?#$
如上配置,密码中的特殊字符则不会被sh 识别,需要改为:
iterm2login.exp root 17.17.10.10 csdn@iterm2\!\?\#\$
常见转义字符:
\'
\"
\*
\?
\\
\~
\`
\!
\#
\$
\&
\|
\{
\}
\;
\<
\>
\^