expect shell自动连接ssh服务器

2022-09-09 09:43:26 浏览数 (3)

代码语言:javascript复制
#!/usr/bin/expect

set PORT 2222
set HOST 13.214.147.196
set USER root
set PASSWORD DT8ajY0lFudf6D9Y


spawn ssh -p $PORT $USER@$HOST
expect {
        "yes/no" {send "yesr";exp_continue;}
        "*password:*" {send "$PASSWORDr"}
}
interact

将以上文件保存为 test_client

打开items,将文件位置填到command,前面加上expect 空格

expect脚本解读:

下载安装:

代码语言:javascript复制
$ apt-get install expect
代码语言:javascript复制
$ yum install expect

spawn

Starts a script or a program.

expect

Waits for program output.

send

Sends a reply to your program.

interact

Allows you to interact with your program.

  • The spawn command starts a script or a program like the shell, FTP, Telnet, SSH, SCP, and so on.
  • The send command sends a reply to a script or a program.
  • The Expect command waits for input.
  • The interact command allows you to define a predefined user interaction.

1 人点赞