## 快捷登入線上伺服器指令碼
> 背景環境:原先故障警示,登入故障機查看時非常不討好的事情,密碼強度高,禁止root登入都是只能複製粘貼密碼再登入,非常繁瑣,索性切換到mac下之後這個登入指令檔,直接go <ip最後一段即可>直接登入,省心多了,原先只能複製粘貼密碼方式進行登入故障機檢查警示
===
> 所以借鑒別人指令碼改了如下,登入線上伺服器可以直接利用:# go 210就登入192.168.199.210,root的身份,避免了複製粘貼密碼帶來的時間損耗,前提你得將指令碼所在路徑加入到環境變數中才能這樣使用
```
#!/bin/sh
#filename: go
if [ $# -eq 0 ]
then
echo "Usage: $0 [last_segment_in_ip]"
exit 1
fi
segment=$1
suffix=192.168.199
#if [ -n "$pass" ]; then
ip="$suffix.$segment"
/Users/mingmings/auto_login/login $ip
```
呼叫指令碼如下
```
#!/usr/bin/expect -f
#filename: login
trap {
set rows [stty rows]
set cols [stty columns]
stty rows $rows columns $cols < $spawn_out(slave,name)
} WINCH
set timeout 20
set ip [lindex $argv 0]
#set password [exec cat $passbook | grep -v "^#" | grep -w "$ip" | awk {{print $NF}}]
#set root_password [exec cat $root_passbook | grep -v "^#" | grep -w "$ip" | awk {{print $NF}}]
spawn ssh -p xxxx superadmin@$ip
expect {
"*yes/no" { send "yes\r"; exp_continue}
"*password:" {
# send "$password\r";
send "你懂的\r";
send "su -l\r";
exp_continue;
}
"Password:" {
# send "$root_password\r";
send "你懂的\r";
}
"密碼 " {
send "你懂的\r";
exp_continue;
}
"su: incorrect password" {
send "exit\r";
}
}
interact
```