shell expect简单实用
1、脚本实例
#!/usr/bin/expect -f set user xxx set privateKey /home/.ssh/id_rsa set host 127.0.0.1 set port 8080 set upFilePath [index $argv 0] # 表示传入的第一个参数 set lupFilePath [index $argv 1] # 表示传入的第二个参数 set upFileName [index $argv 2] # 表示传入的第三个参数 spawn sftp -oIdentityFile=${privateKey} -oport=${port} ${user}@${host} expect "sftp>" send "cd ${upFilePath }\r" expect "sftp>" send "lcd ${lupFilePath }\r" expect "sftp>" send "put ${upFileName }\n" expect "sftp>" send "ls -l" expect "sftp>" send "bye\r" expect eof
借助 expect 处理交互的命令,可以将交互过程如 ssh 登录, ftp 登录等写在一个脚本上,使之自动化完成。
2、expect语法
expect [选项] [ -c cmds ] [[ -[f|b] ] cmdfile] [args] 选项: -c: 从命令行执行expect脚本,默认expect是交互执行的 示例:expect -c 'expect "\n" {send "pressed enter\n"}' -d: 输出调试信息 示例:expect -d demo.exp expect中的相关命令 spawn:启动新的进程 send:向进程发送字符串 expect:从进程接收字符串 interact:允许用户交互 exp_continue:匹配多个字符串时在执行动作后加次命令

浙公网安备 33010602011771号