Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends ,

I am willing to connect to linux system from windows & execute some commands on it . I am writting tcp program which will take telnet of linux machine & excute commands on it . I am connecting on port 23 successfully . but Sever sends me something junk value as ²↑ ² ²# ²' . I dont understand what it is exactly .When I sends telnet command as
MIDL
strcpy(lv_send_buffer,"telnet 111.1.112.11 -l user");
	lv_NoOfBytes = send(lv_clientSocket,lv_send_buffer,strlen(lv_send_buffer),0); 

Server does not sends anything . Please guide me how can I send username / password through program & execute commands on it .
Code is as follow .. Thanks ...

int main()
{
	char lv_recv_buffer[512]	=	{""};
	char lv_send_buffer[256]	=	{""};
	int lv_ret		=		0;
	int lv_clientSocket = -1 ;
	struct sockaddr_in lv_clientAddr ; 
	struct hostnet *lp_host ;
	int lv_NoOfBytes		=	-1	;
	WSADATA	lv_wsdata ;		
	lv_ret = WSAStartup(0x0101,&lv_wsdata);
	if (lv_ret != 0 )
	{
		return (0);
	}
	lv_clientSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); 
	
	if (lv_clientSocket == -1 )
	{
		printf("socket creation failed");
		WSACleanup();
		return 0 ;
	}
	else
	{
		printf("socket creation success");
	}

	lv_clientAddr.sin_family = AF_INET ;
	lv_clientAddr.sin_addr.s_addr = inet_addr("192.9.202.31");
	lv_clientAddr.sin_port=htons(23);
	
	if (connect(lv_clientSocket, (struct sockaddr *) &lv_clientAddr,sizeof(lv_clientAddr)))
	{
		int lv_err = WSAGetLastError();
		printf("error while connecting [%d] ",lv_err);
		return 0 ;
	}
	else
	{
		printf("connection success");
	}
	
	strcpy(lv_send_buffer,"telnet 111.1.112.11 -l user");
	
	
	lv_NoOfBytes = recv(lv_clientSocket,lv_recv_buffer,512,0);
	if (lv_NoOfBytes <=0 )
	{
		printf("no data received ");
		closesocket(lv_clientSocket);
		WSACleanup();
	}
	else
	{
		printf("from server :[%s] ",lv_recv_buffer);
	}

	lv_NoOfBytes = send(lv_clientSocket,lv_send_buffer,strlen(lv_send_buffer),0);
	printf("bytes send [%d]",lv_NoOfBytes );

	memset( lv_recv_buffer,0,sizeof(lv_recv_buffer));
	Sleep(1000);
	while(1)
	{
		lv_NoOfBytes = recv(lv_clientSocket,lv_recv_buffer,512,0);
		if (lv_NoOfBytes <=0 )
		{
			printf("no data received ");
			closesocket(lv_clientSocket);
			WSACleanup();
		}
		else
		{
			printf("from server :[%s] ",lv_recv_buffer);
		}
	}
			
		getch();
	return  0;

}
Posted
Updated 18-Jan-11 2:48am
v3
Comments
CPallini 18-Jan-11 7:53am    
What does it mean 'a junk of bytes' exactly?
mikhilv 19-Jan-11 1:26am    
hey i have modified the question .I hve mentioned there what server sends me exactly .

1 solution

Thats is because you dont send "telnet 111.1.112.11 -l user" to the server, that is what you tpye into your console.

that runs the telnet program which interprets the heap of junk that goes back and forth.

Either read up on the telnet protocol, or get a packet sniffer such as Wireshark[^] and record the session to see what is sent.

To view the session in Wireshark:
1. Open a console window
2. Enter the telnet command (for mine I used "telnet towel.blinkenlights.nl") (Don't hit enter yet)
3. Open Wireshark and select the network adapter that the communication will go over to start listening
4. Go back to the console and hit enter, type stuff have a session then close the console
5. Hit stop capture in Wireshark.
6. Filter the packets to remove random things like DNS lookup and keepalives. For my filter I used "ip.src==94.142.241.111||ip.dst==94.142.241.111", because towel.blinkenlights.nl resolves to 94.142.241.111

Wireshark will present the data at the bottom of the window and tell you what each byte is.

A quick view and you will find that the control messages are sent in binary, and other stuff is sent in plain text.

I would also suggest using Wireshark for checking the dataflow of your own application to ensure communications are going smoothly
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Jan-11 9:54am    
Useful, detailed -- my 5
--SA
mikhilv 19-Jan-11 1:19am    
Well.. I am not allowed to install this software in office . Is there any other alternatives ..?

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