Call expect file from C

旧街凉风 提交于 2020-01-06 19:14:09

问题


I coded a ftp.exp file which sends a log file of a C application to a server.

#!/usr/bin/expect
set timeout -1
spawn ftp xxx.xxx.xxx 21
match_max 100000
expect -re {[2]{2,}[0]{1,}}
send -- "usrxxxx"
expect -exact "usrxxxxx"
send -- "\r"
expect -re {[3]{2,}[1]{1,}}
send -- "xxxx\r"
expect -exact "\r
230 Access granted for xxxxxxxxxx !\r
Remote system type is UNIX.\r
Using binary mode to transfer files.\r
ftp> "
send -- "put /home/User/test.txt test2.txt\r"
expect -exact "put /home/User/test.txt test2.txt\r"
send -- "\r"
expect -re {[2]{2,}[6]{1,}}
send -- "quit\r"
expect eof

The script works fine but how can I execute the file from a C Application.


回答1:


You can use system(3) or popen(3) library functions to run your expect script from your C code. These functions are using syscalls like fork(2), execve(2), pipe(2), dup(2), waitpid(2) which you could also use by yourself.

I strongly suggest learning more about advanced unix programming and advanced linux programming; there are some concepts and tricks to learn.

I believe you'll better use ssh (with scp) than an expect script sending plain passwords on the wire to a distant remote FTP server (there are some security issues with your approach).




回答2:


You need to use the libexpect and call your script from its api.

I found only the php bindings for it, but I have no Debian here to tell you what package provides the base package.



来源:https://stackoverflow.com/questions/12331259/call-expect-file-from-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!