mac OS版本为Mojave (10.14.1)
一、有私钥登陆
1.生成公、私钥
1.1 密钥类型为“RSA”,密钥长度“2048”(自定义,根据远程服务器需求生成)
1.2 passphrase可设置为空,这样则自动登陆,免密码
# ssh-keygen -b 2048 -t rsa -C catayi@test #-C为审计做准备,在公钥的末尾加上 “空格”+“备注”
Generating public/private rsa key pair.
Enter file in which to save the key (~/.ssh/id_rsa): ~/.ssh/id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in ~/.ssh/id_rsa.
Your public key has been saved in ~/.ssh/id_rsa.pub.
The key fingerprint is:
10:ce:a1:5e:cc:c6:da:49:c8:e1:dc:30:63:41:60:18 zhangshumin@ops
The key's randomart image is:
+--[ RSA 2048]----+
|EoooO.o |
|.. = # o |
| * % |
| . * o |
| o o S |
| |
| |
| |
| |
+-----------------+
2.公钥pub给远程ssh服务器配置,私钥找个地方保存起来待每次登陆验证
3.写登陆脚本
~/.ssh/config文件下写入以下配置,没有则创建一个
Host catayitest #别名
HostName 192.168.0.1 #远程host或ip
Port 22 #远程端口
User catayi #远程登陆用户
IdentityFile ~/.ssh/id_rsa #私钥位置
4.终端输入#ssh catayitest
(刚才写的别名)然后回车,显示要输入passphrase(创建公、私钥的时候有则输入,没有则继续回车),登陆成功!
二、无私钥登陆
1.首先mac要安装expect、spawn-fcgi(homebrew一键安装)
2.找方便自己的路径下写一下脚本,保存为.sh后缀的文件,如catayitest2.sh
#!/usr/bin/expect -f
set timeout 3
spawn ssh username@hostname #username:远程服务器登陆用户名;hostname:远程服务器地址或ip
expect "*yes/no*" {send "yes\n"}
expect "*passphrase*" {send "\r"} #这里若是共存有私钥登陆则可以跳过验证
expect "*password:*" {send "xxx\r";interact} #xxx:登陆密码
- ~/.ssh/config文件下追加以下配置,没有则创建一个
Host catayitest2 #别名
HostName 192.168.0.1 #远程host或ip
Port 22 #远程端口
User catayi #远程登陆用户
4.终端输入#expect catayitest2.sh
然后一路回车,
自动填密码登陆成功!