使用expect库在unix上的c ++编程中自动执行任务。 [英] Using expect library to automate task in c++ programming on unix.
问题描述
嗨朋友们,
我正在unix上创建一个程序,它将通过ip - >(xx.xxx.xx.xx)telnet远程主机并在远程主机上运行命令并显示远程主机返回的响应。
我已经google了很多,我发现使用expect库可能有帮助,所以我使用expect库而不用tcl。
我这样做
Hi friends,
I am creating a program on unix which will telnet a remote host on ip -->(xx.xxx.xx.xx) and run a command on the remote host and display the response returned by the remote host.
I have googled a lot and I found using the expect library might help so I am using expect library without tcl.
I am doing this
string myUser="97.253.46.90";<br />
string myPORT="23";<br />
myFD = exp_spawnl("telnet","telnet",myUser,myPORT,(char *)0);<br />
这在myFD中返回4我接下来要做什么?
请帮助我们各位我是codeproject的新手所以请不要介意我是否犯过任何错误。
谢谢和问候。
rocksean30
this returns 4 in myFD what should I do next ?
Please help me guys I am new to codeproject so please dont mind if I have made any mistakes.
Thanks and regards.
rocksean30
推荐答案
参见 http://www.eso.org/projects/vlt/sw-dev/tcl8.3.3/expect5.32/libexpect.3.html [ ^ ]。
下一步是等待结果并检查它是否有你想要的结果。
这就是我做的。
enum HOST_STATE {AVAILABLE,
BUSY,
CONFIRM,
UNKNOWN,
FAILED,
REFUSED};
string myUser =97.253。 46.90;
string myPORT =23;
int connected = 0;
int myFD = exp_spawnl(telnet,telnet ,myUser,myPORT,(char *)0);
sleep(1); //等一下才能得到结果
exp_timeout = CONNECTION_EXPIRY_PERIOD;
int expresult = exp_expectl(fd ,exp_glob,权限被拒绝,未知,
exp_glob,密码无效,未知,
exp_glob,访问被拒绝,未知,
exp_glob,你确定要继续吗,确认,
exp_glob,忙,忙碌,
exp_glob,未知主机,未知,
exp_glob,无法连接到远程主机,FAILED,
exp_glob,失败,FAILED,
exp_glob,连接被拒绝 ,REFUSED,
exp_glob,外国主持人关闭,FAILED,
exp_glob,已连接,可用,
exp_end);
// c out<<connect()结果:<< expresult<< endl;
error = 0;
switch (表达)
{
案例可用:
{
cout<<Telnet host available< ;<结束;
休息;
}
case CONFIRM:
{
cout< ;<CONFIRM RSA密钥指纹<< endl;
cout<<是或否需要被解雇<< endl;
休息;
}
案例忙碌:
{
cout<<Telnet host busy;
错误= 1;
休息;
}
case UNKNOWN:
{
cout<<Telnet host unknown<< endl;
error = 1;
break;
}
case FAILED:
{
cout<<Telnet host failed<< endl;
错误= 1;
休息;
}
案例拒绝:
{
cout<<Telnet主机拒绝连接<< endl;
错误= 1;
休息;
}
案例EXP_TIMEOUT:
{
cout<<Telnet主机超时<< endl;
错误= 1;
休息;
}
案例EXP_ABEOF:
{
cout<<预计abbnormal eof<< endl;
error = 1 ;
休息;
}
默认:
{
cout<< ;Telnet主机问题<< endl;
错误= 1;
休息;
}
}
如果(错误)
{
connected = 0;
返回0;
}
其他
{
connected = 1;
返回1;
}
谢谢和问候
rocksean30
The next step is to wait for the result and check it you've got the desired results.
This is what I did.
enum HOST_STATE { AVAILABLE,
BUSY,
CONFIRM,
UNKNOWN,
FAILED,
REFUSED};
string myUser="97.253.46.90";
string myPORT="23";
int connected=0;
int myFD = exp_spawnl("telnet","telnet",myUser,myPORT,(char *)0);
sleep(1); // wait a second before get result
exp_timeout = CONNECTION_EXPIRY_PERIOD;
int expresult = exp_expectl(fd,exp_glob,"Permission denied",UNKNOWN,
exp_glob,"Invalid password",UNKNOWN,
exp_glob,"Access denied",UNKNOWN,
exp_glob,"Are you sure you want to continue",CONFIRM,
exp_glob,"busy",BUSY,
exp_glob,"Unknown host",UNKNOWN,
exp_glob,"Unable to connect to remote host",FAILED,
exp_glob,"failed",FAILED,
exp_glob,"Connection refused",REFUSED,
exp_glob,"closed by foreign host",FAILED,
exp_glob,"Connected to", AVAILABLE,
exp_end);
//cout<<"connect() Result: "<<expresult<<endl;
error = 0;
switch (expresult)
{
case AVAILABLE:
{
cout<<" Telnet host available " << endl;
break;
}
case CONFIRM:
{
cout<<"CONFIRM RSA key fingerprint"<<endl;
cout<<"yes or no need to be fired"<<endl;
break;
}
case BUSY:
{
cout<<"Telnet host busy";
error = 1;
break;
}
case UNKNOWN:
{
cout<<"Telnet host unknown"<<endl;
error = 1;
break;
}
case FAILED:
{
cout<<"Telnet host failed"<<endl;
error = 1;
break;
}
case REFUSED:
{
cout<<"Telnet host refused connection"<<endl;
error = 1;
break;
}
case EXP_TIMEOUT:
{
cout<<"Telnet host timeout"<<endl;
error = 1;
break;
}
case EXP_ABEOF:
{
cout<<"Expect abbnormal eof"<<endl;
error = 1;
break;
}
default:
{
cout<<"Telnet host problem"<<endl;
error = 1;
break;
}
}
if (error)
{
connected=0;
return 0;
}
else
{
connected=1;
return 1;
}
Thanks & Regards
rocksean30
这篇关于使用expect库在unix上的c ++编程中自动执行任务。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!