-
November 29th, 2010, 03:27 PM
#1
Telnet Help
Hi i am starting a personal project and i would like someone to point me in the right direction. Basically what i am tryin to do is to get a computer to telnet to another. So the program would therefore run and then i would enter the ip address and port of the target computer i am trying to connect to, then enter the username and password to logon, and then i can run the telnet commands within it.....e.g - telnet 127.0.0.1 100
I was discussing it with a friend of mine and he was telling me about looking into the use of sockets so thats what i am currently doing. I would like for anyone who is willing to at least let me know if i am going in the right direction or if there is a simpler way to do this besides the socket method. Also any snippets of code are always welcomed.
Thanks in advance for any help.
-
November 29th, 2010, 03:52 PM
#2
Re: Telnet Help
Yes you're on the right track but why write something that already exists in so many different flavours?
See here for instance http://sourceforge.net/search/?q=telnet
-
November 30th, 2010, 12:45 AM
#3
Re: Telnet Help
Originally Posted by S_M_A
Well as i said its a personal project and im doing it just because....and dont want one that is finished or anything i just want to do one and see if i myself can do as i am still a newbie in programming and then there are other things i plan to do after i finish this step.
-
November 30th, 2010, 01:00 AM
#4
Re: Telnet Help
Now the following code i got form http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx and after i ran it in my VS 2010 it compiled without errors but when i ran it the output window flashed and disappeared... can anyone tell me why?
Also i saw that a guy mentioned that
"Annoyingly, this wouldn't compile properly with default Visual C++ 2010 project settings. This is because project default to using Unicode.
Instead of {struct addrinfo ...} you must use {ADDRINFOT ...}$0 $0Instead of getaddrinfo() you must use GetAddrInfo()
I guess this is just old documentation, but an update so it will compile with a default install of VC would greatly assist beginning programmers like myself "
However i dont quite understand what hes gettin at or if its even the solution to my problem but any help would be greatly appreciated. Thanks
_________________________________________________________________________________________________________________________________________________
UPDATE: well looking at it a second time i think it might be working but im still not sure it builds without errors but then it exits and says
'Telnet Software.exe': Loaded 'C:\Users\Rod\Documents\Visual Studio 2010\Projects\Telnet Software\Debug\Telnet Software.exe', Symbols loaded.
'Telnet Software.exe': Loaded 'C:\Windows\System32\ntdll.dll', Symbols loaded (source information stripped).
'Telnet Software.exe': Loaded 'C:\Windows\System32\kernel32.dll', Symbols loaded (source information stripped).
'Telnet Software.exe': Loaded 'C:\Windows\System32\ws2_32.dll', Symbols loaded (source information stripped).
'Telnet Software.exe': Loaded 'C:\Windows\System32\msvcrt.dll', Symbols loaded (source information stripped).
'Telnet Software.exe': Loaded 'C:\Windows\System32\advapi32.dll', Symbols loaded (source information stripped).
'Telnet Software.exe': Loaded 'C:\Windows\System32\rpcrt4.dll', Symbols loaded (source information stripped).
'Telnet Software.exe': Loaded 'C:\Windows\System32\nsi.dll', Symbols loaded (source information stripped).
'Telnet Software.exe': Loaded 'C:\Windows\System32\msvcp100d.dll', Symbols loaded.
'Telnet Software.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded.
'Telnet Software.exe': Loaded 'C:\Windows\System32\mswsock.dll', Symbols loaded (source information stripped).
'Telnet Software.exe': Loaded 'C:\Windows\System32\WSHTCPIP.DLL', Symbols loaded (source information stripped).
'Telnet Software.exe': Unloaded 'C:\Windows\System32\WSHTCPIP.DLL'
The program '[0x139C] Telnet Software.exe: Native' has exited with code 1 (0x1).
Code:
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "23"
int main() {
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
char *sendbuf = "this is a test";
char recvbuf[DEFAULT_BUFLEN];
int iResult;
int recvbuflen = DEFAULT_BUFLEN;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo("127.0.0.1", DEFAULT_PORT, &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed: %d\n", iResult);
WSACleanup();
return 1;
}
// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
// Connect to server.
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}
// Send an initial buffer
iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
if (iResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
printf("Bytes Sent: %ld\n", iResult);
// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
// Receive until the peer closes the connection
do {
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 ){
printf("Bytes received: %d\n", iResult);
std::cout << "Received data = " << recvbuf;
}else if ( iResult == 0 ){
printf("Connection closed\n");
}else{
printf("recv failed: %d\n", WSAGetLastError());
}
} while( iResult > 0 );
// cleanup
closesocket(ConnectSocket);
WSACleanup();
system("pause");
return 0;
}
Last edited by Roddy87; November 30th, 2010 at 07:59 AM.
Reason: New info added
-
November 30th, 2010, 07:58 AM
#5
Re: Telnet Help
Below is the output im gettin now after making a few changes but im not exactly sure what this means. I hope this means that a connection was actually established because then all i would have to do is maybe change the IP address to the address of a computer on the network and then i can begin the telnet process of sending commands. Does anyone have any input?
OUTPUT:
Bytes Sent: 14
Bytes received: 21
Received data =  ²% ¹☺ ¹♥ ²' ²▼ ²Connection closed
Press any key to continue . . .
-
November 30th, 2010, 08:22 AM
#6
Re: Telnet Help
That's probably the telnet server querying the client for supported options. These are established before the session ever gets to a prompt. See the telnet RFC:
http://www.faqs.org/rfcs/rfc854.html
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|