从C呼叫期望文件 [英] Call expect file from C
问题描述
我编写了一个 ftp.exp 文件,该文件将C应用程序的日志文件发送到服务器.
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
脚本工作正常,但如何从 C应用程序执行文件.
The script works fine but how can I execute the file from a C Application.
推荐答案
您可以使用 system(3 )或 popen(3)库函数从您的计算机上运行您的期望脚本C代码.这些功能正在使用系统调用,例如 execve(2), dup(2),
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.
我强烈建议您详细了解高级unix编程和
I strongly suggest learning more about advanced unix programming and advanced linux programming; there are some concepts and tricks to learn.
我相信您最好使用ssh
(与scp
一起使用),而不是期望脚本将网络上的普通密码发送到远程FTP服务器(您的方法存在一些安全问题).
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).
这篇关于从C呼叫期望文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!