mac 终端登陆linux,Mac终端自动登录服务器
效果
輸入命令,選擇一個序號登錄服務器
$ aoel
(1) first 192.168.1.1
(2) 第二臺機器 192.168.1.2
配置文件
1. 填寫服務器信息 computerInfo.ini
#ip port user password description
192.168.1.1 22 root 123456 first machine
192.168.1.2 22 root 123456 第二臺機器
2. 使用 expect 自動回復 yes 輸入密碼 core.ex
#!/usr/bin/expect
set ip [lindex $argv 0]
set port [lindex $argv 1]
set username [lindex $argv 2]
set password [lindex $argv 3]
set timeout -1
spawn ssh -p $port $username@$ip
expect {
"password" {send "$password\r";}
"yes/no" {send "yes\r";exp_continue}
}
interact
3. 登錄腳本 login.sh
#!/bin/bash
file="computerInfo.ini"
#顯示機器信息 過濾第一行和空行
awk '{if (NR > 1 && $1 != ""){printf "%-2s %-45s %-15s \n","("NR-1")",$5,$1}}' $file
echo "please choose which machine to login:"
read number
number=$[number+1]
#將信息存入變量
read ip port user password <<< $(echo `awk 'NR=="'$number'"{print $1,$2,$3,$4}' $file`)
./core.ex $ip $port $user $password
4. 使用 alias 定義自己的命令
./bashrc # 僅當前用戶有效
/etc/bashrc # 所有的用戶都有效
source .bashrc # 讓我們的環境生效
alias aoel='/login_server/login.sh'
參考文章
Linux Expect 簡介和使用實例
Linux自定義命令指令 | alias
Linux alias命令的使用
總結
以上是生活随笔為你收集整理的mac 终端登陆linux,Mac终端自动登录服务器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 三维坐标求两点距离
- 下一篇: Linux C高级编程——网络编程之以太