Call expect file from C

2019-08-20 10:11发布

问题:

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.