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
Updated 22-Jul-13 21:03pm
v2