Skip to main content

Shell脚本实现ssh自动登录

·55 words·1 min
Linux Linux
Rory
Author
Rory
Step by step the ladder is ascended
Table of Contents
[root@xuxing ~]#yum -y install expect
[root@xuxing ~]# cat 123
#!/bin/expect
spawn ssh root@10.124.45.123
expect "password:"
send "cisco\r"
interact
[root@xuxing ~]# chmod 777 123
[root@xuxing ~]#
[root@xuxing ~]#
[root@xuxing ~]# ./123
spawn ssh root@10.124.45.123
root@10.124.45.123's password:
Last login: Fri Nov 20 04:53:03 2020 from 10.140.0.42
[root@nso ~]#
[root@nso ~]#

四个命令的解释
#

send:用于向进程发送字符串
expect:从进程接收字符串
spawn:启动新的进程
interact:允许用户交互,如果没有该命令会直接终端 session,返回之前的终端