Last active
March 28, 2020 15:32
-
-
Save mittorn/035573c2a81ca0f2a129 to your computer and use it in GitHub Desktop.
Simple telnet bot example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* telnet-example.c | |
* | |
* Copyright (c) 2015 Mittorn. Use may be in whole | |
* or in part in accordance to the General Public License (GPL). | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
* SUCH DAMAGE. | |
*/ | |
/*****************************************************************************/ | |
/*** telnet-example.c ***/ | |
/*****************************************************************************/ | |
/************************************************************************** | |
* This is a simple telnet chat utility. It connects to server and | |
* sends/reads lines. Useful for some routers configuration. | |
* based on simple-client.c by Sean Walton and Macmillan Publishers | |
**************************************************************************/ | |
#include <stdio.h> | |
#include <string.h> | |
#ifdef _WIN32 | |
#include <winsock.h> | |
#pragma comment(lib,"wsock32.lib") //Winsock Library | |
#define close closesocket | |
#define errno 1 | |
#define perror(x) fprintf(stderr, "%s: failed\n", x) | |
#else | |
#include <errno.h> // errno will not work on windows | |
#include <sys/socket.h> | |
#include <resolv.h> | |
#include <stdlib.h> // exit() | |
#endif | |
#define PORT_INT 23 // telnet connection port | |
#define SERVER_ADDR "192.168.1.1" | |
#define MAXBUF 1024 | |
#define bzero(x,y) memset(x,0,y) | |
#ifdef WIN32 | |
static int inet_aton(const char *cp, struct in_addr *inp) | |
{ | |
unsigned long addr; | |
if (cp == 0 || inp == 0) | |
{ | |
return -1; | |
} | |
addr = inet_addr(cp); | |
if (addr == INADDR_NONE || addr == INADDR_ANY) | |
{ | |
return -1; | |
} | |
inp->s_addr = addr; | |
return -1; | |
} | |
#endif | |
int sockfd; | |
// read data until get string | |
void waitforstring(const char *s) | |
{ | |
int res, pos = 0; | |
char buffer[MAXBUF]; | |
bzero(buffer, MAXBUF); | |
while(res = recv(sockfd, buffer + pos, sizeof(buffer) - pos, 0) > 0) | |
{ | |
if(res > 0) | |
{ | |
pos += res; | |
buffer[MAXBUF-1] = 0; | |
printf("%s", buffer + pos); | |
if(strstr(buffer, s)) | |
return; | |
} | |
} | |
} | |
// send string | |
void sendstring(const char *s) | |
{ | |
int pos = 0, len = strlen(s); | |
// Only small messages supported now, | |
// need to improve this | |
send(sockfd, s, len, 0); | |
} | |
int main() | |
{ | |
struct sockaddr_in dest; | |
#ifdef _WIN32 | |
static WSADATA winsockdata; | |
WSAStartup( MAKEWORD( 1, 1 ), &winsockdata ); | |
#endif | |
// Open socket | |
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) | |
{ | |
perror("Create socket error"); | |
exit(errno); | |
} | |
// Initialize server address/port struct | |
bzero(&dest, sizeof(dest)); | |
dest.sin_family = AF_INET; | |
dest.sin_port = htons(PORT_INT); | |
if ( inet_aton(SERVER_ADDR, &dest.sin_addr.s_addr) == 0 ) | |
{ | |
perror("Cannot understand \""SERVER_ADDR); | |
exit(errno); | |
} | |
#if 0 // if you wish to enable non-blocking mode, enable this | |
#ifdef WIN32 | |
unsigned long arg = 1; | |
ioctlsocket(sockfd, FIONBIO, &arg); | |
#else | |
int arg = fcntl(sockfd, F_GETFL, NULL); | |
arg |= O_NONBLOCK; | |
fcntl(sockfd, F_SETFL, arg); | |
#endif | |
#endif | |
// Connect to server | |
if ( connect(sockfd, (struct sockaddr*)&dest, sizeof(dest)) != 0 ) | |
{ | |
perror("connect"); | |
exit(errno); | |
} | |
// Example: login to router and replace route | |
waitforstring("ogin: "); | |
sendstring("admin\n"); | |
waitforstring("assword: "); | |
sendstring("admin\n"); | |
waitforstring(">"); | |
sendstring("sh -c \"route del default;route add default ppp1\"\n"); | |
waitforstring(">"); | |
close(sockfd); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment