- 论坛徽章:
- 0
|
太奇怪了!
我有个expect脚本,在shell下执行很正常,如:
./telnet.sh 192.168.10.60 test test
这样可以正常实现登录
然后我编程使用system()函数来执行这个脚本,结果也正常。
tt.c的内容如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
main()
{
char strCmd[512];
strcpy(strCmd,"/home/usp/bin.sh/telnet.pansky 192.168.10.57 dcc dccdcc ");
printf("cmd[%s]\n");
system(strCmd);
}
~
但是,当我fork()一个子进程来执行时,却出错了,不知道有什么原因:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
main()
{
char strCmd[512];
if( fork() > 0 ) exit(0);
strcpy(strCmd,"/home/usp/bin.sh/telnet.pansky 192.168.10.57 dcc dccdcc ");
printf("cmd[%s]\n");
system(strCmd);
}
~
错误信息:
cmd[/home/usp/bin.sh/telnet.sh 192.168.10.57 dcc dccdcc ]
[usp@fcshost usp]$ Telnet escape character is 'v'.
Trying 192.168.10.57...
Connected to 192.168.10.57.
Escape character is 'v'.
Red Hat Linux release 9 (Shrike)
Kernel 2.4.20-8 on an i686
login: dcc
Password: ioctl(raw): I/O error
===================================================
telnet.sh的内容:
#!/home/usp/bin/expect
if { $argc < 3 } {
puts stderr "Usage: $argv0 IPAdress UserId Passwd"
exit
}
set IPADDR [lindex $argv 0]
set LOGIN [lindex $argv 1]
set PASSWD [lindex $argv 2]
set timeout 10
spawn -noecho telnet -e vt100 $IPADDR
expect {
"*login:" {
send "$LOGIN\r"
}
"*登录*" {
send "$LOGIN\r"
}
timeout {exit 0}
}
expect {
"*assword:" {
send "$PASSWD\r"
interact
exit 0
}
"*密码*" {
send "$PASSWD\r"
interact
exit 0
}
timeout {exit 0}
eof {exit 0}
} |
|