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'
zsh

修改 ~/.zshrc
或者 /etc/zshrc (系统升级后配置可能会丢失)

参考文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值