开发者

telnet client using c, for automating the backup of a series of routers

开发者 https://www.devze.com 2023-03-24 10:10 出处:网络
I have 400 routers, approx. and by using the console, I can access by telnet each one of them, and by typing a series of commands I can generate a backup and copy it to my computer using FTP.

I have 400 routers, approx. and by using the console, I can access by telnet each one of them, and by typing a series of commands I can generate a backup and copy it to my computer using FTP.

I want to automate this task because its really annoying.

I have read everything I could about this topic on the Internet.

I know to program in C and I am using Windows. So I read all about WINSOCK and sockets and I am trying to implement my program.

I have this problem, when I use the recv function it keeps showing me strange characters, I don't know what this means and how to solve it and I dont even know if what I am trying to do is possible using just sockets and C.

In case it is possible could you tell me how to send the commands I have this way or point me in the way of a document that shows me how to do this.

here goes the basic of my code, i suspect i should learn about how to negotiate telnet in c

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <stdlib.h>


#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "libws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")


int main ()
{
    const char direcciones[400]="xx.x.x.xx";
    WSADATA wsaData;
    SOCKET sock;
    struct hostent *host;
    struct sockaddr_in direc;
    int conex;
    int len;
    char *sendbuf1="USER admin\n\r";
    char *sendbuf2="PASS \n\r";
    char *sendbuf3="\n\r";
    char *sendbuf4="export file=";

    char recvbuf[512];
    int cont;

    // Initialize Winsock
    WSAStartup(MAKEWORD(2,2), &wsaData);
    conex = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (conex != 0)
    {
    printf("WSAStartup failed: %d\n", conex);
    return 1;
    }

    host=gethostbyaddr(&direcciones[0],4,AF_INET);
    printf("\tOfficial address: %s\n", host->h_addr);
    system("pause");

    sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if (sock==-1)
    {
        printf("error al crear el socket\n");
        return(-1);

    }
    direc.sin_family=AF_INET;
    direc.sin_port=htons(23);
    direc.sin_addr=*((struct开发者_StackOverflow中文版 in_addr*)host->h_addr);
    memset(direc.sin_zero,0,8);

    conex = connect(sock,(SOCKADDR *) & direc,sizeof(direc));
    if (conex==SOCKET_ERROR)
    {
    printf("no se ha podido conectar con el servidor\n");
    conex = closesocket(sock);
        if (conex == SOCKET_ERROR)
            wprintf(L"closesocket function failed with error: %ld\n", WSAGetLastError());
    WSACleanup();
    return 1;
    }
    else printf("coneccion correcta a puerto telnet\n");

system("pause");                    //esto hay que quitarlo despues

conex = recv(sock,recvbuf,512, 0);
printf("se recibio %i bytes\n",conex);
printf("dato recibido: %s\n",&recvbuf);


conex = send( sock, sendbuf1,strlen(sendbuf1), 0 );


conex = recv(sock,recvbuf,512, 0);
printf("se recibio %i bytes\n",conex);
printf("dato recibido: %s\n",&recvbuf);


conex = closesocket(sock);
if(conex==SOCKET_ERROR)
{
    wprintf(L"funcion closesocket fallo con error: %ld\n", WSAGetLastError());
    WSACleanup();
    return 1;
}

WSACleanup();
return 0;

}

`


You'll need to handle the telnet negotiation.

There's a small implementation in netcat (by which I mean Hobbit's permissively licensed version, not GNU's) that is described as "Answer anything that looks like telnet negotiation with don't/won't." which you might be able to learn from, though it will take a few minutes to figure out how it works and interfaces to the rest of the code.

You can also use netcat from a script or launch it as an external process from a C program.

For that matter, it shouldn't be hard to find a telnet client under a BSD license from which you could borrow code by meeting fairly gentle compliance requirements. This is likely to be a more complicated program, but it might have clearer internal abstractions.


I know this is kind of avoiding the question, but I really think C is the wrong tool for the job (automating system administration tasks). It would be easier to use Python's telnetlib, even if you have to spend 30 minutes learning a little Python to make it run. Check out how easy it is (taken from the python docs):

import getpass
import sys
import telnetlib

HOST = "localhost"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until("login: ")
tn.write(user + "\n")
if password:
    tn.read_until("Password: ")
    tn.write(password + "\n")

tn.write("ls\n")
tn.write("exit\n")

print tn.read_all()
0

精彩评论

暂无评论...
验证码 换一张
取 消