Click here to Skip to main content
15,885,760 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 />

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
Posted
Updated 2-Jul-13 1:22am
v4
Comments
Richard MacCutchan 2-Jul-13 9:06am    
Did you follow the link I gave you and read the documentation?

 
Share this answer
 
Comments
Mathew Fer 28-Oct-13 19:29pm    
Hi rocksean30,

I am also interested in this and can you please share the code to interact with a unix host using C++?

My email - mathewfer@gmail.com

Thanks

Mathew
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900