Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Linux
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
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 18-Jan-11 2:36am
mikhilv218
Edited 18-Jan-11 3:48am
v3
Comments
CPallini at 18-Jan-11 7:53am
   
What does it mean 'a junk of bytes' exactly?
mikhilv at 19-Jan-11 1:26am
   
hey i have modified the question .I hve mentioned there what server sends me exactly .

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
SAKryukov at 18-Jan-11 9:54am
   
Useful, detailed -- my 5
--SA
mikhilv at 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)

  Print Answers RSS
0 OriginalGriff 645
1 Maciej Los 225
2 Abhinav S 171
3 Richard Deeming 160
4 Suvendu Shekhar Giri 159
0 Sergey Alexandrovich Kryukov 9,623
1 OriginalGriff 9,135
2 Peter Leow 5,042
3 Kornfeld Eliyahu Peter 3,373
4 Maciej Los 2,581


Advertise | Privacy | Mobile
Web02 | 2.8.150327.1 | Last Updated 18 Jan 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100