shell脚本实现ssh自动登录远程服务器示例:
1 | #!/usr/bin/expect |
expect “hi\n”
send “hello there!\n”
这两行代码的意思是:从标准输入中等到hi和换行键后,向标准输出输出hello there。
1 | 模式-动作 |
expect “hi” {send “You said hi”}
匹配到hi后,会输出”you said hi”
1 | 多分支模式语法: |
expect “hi” { send “You said hi\n” }
“hello” { send “Hello yourself\n” }
“bye” { send “That was unexpected\n” }
1 | ### spawn命令 |
set timeout -1
spawn ftp ftp.test.com //打开新的进程,该进程用户连接远程ftp服务器
expect “Name” //进程返回Name时
send “user\r” //向进程输入anonymous\r
expect “Password:” //进程返回Password:时
send “123456\r” //向进程输入don@libes.com\r
expect “ftp> “ //进程返回ftp>时
send “binary\r” //向进程输入binary\r
expect “ftp> “ //进程返回ftp>时
send “get test.tar.gz\r” //向进程输入get test.tar.gz\r
1 | ### interact |
spawn ftp ftp.test.com
expect “Name”
send “user\r”
expect “Password:”
send “123456\r”
interact
```