1.安装expect
yum -y install expect
2.编辑一个脚本
vim ftp_upload.sh
#!/bin/bash
spawn ftp localhost #执行ftp命令
expect "Name*" #如果出现Name字符
send "ftp\n" #则输入ftp并回车
expect "ftp>*"
send "cd pub/other\n"
expect "ftp>*"
send "put /tmp/123.txt xo.txt\n"
expext{
"150 Ok to send data" { send_user "upload sucessfull!";send "quit\n" } #send_user 类型shell的echo命令
"553*" { send_user "upload error!";send "quit\n" }
}
expect eof #expect eof,与spawn对应,表示捕捉终端输出信息终止,类似if...endif
3.加权限
chmod +x ftp_upload.sh
4.执行命令
./ftp_upload.sh