我有400台路由器,通过使用控制台,我可以通过telnet访问他们每个人,并且通过输入一系列命令我可以生成备份并使用FTP将其复制到我的计算机。使用c的telnet客户端,用于自动备份一系列路由器
我想自动执行此任务,因为它真的很烦人。
我已经在互联网上阅读了关于此主题的所有信息。
我知道在C程序,我正在使用Windows。所以我读了关于WINSOCK和套接字的所有内容,并且试图实现我的程序。
我有这个问题,当我使用recv
函数时,它不断显示我奇怪的字符,我不知道这意味着什么以及如何解决它,我甚至不知道我想要做什么是可能的只是插座和C.
万一有可能你能告诉我如何发送命令我有这种方式或指向我的文件,告诉我如何做到这一点。
在这里不用我的代码的基础上,我怀疑应该了解如何在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 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;
}
`
andressanchez,欢迎来到SO。请发布您的代码,以便我们可以帮助您。我编辑了你的问题,让它看起来更好。 – 2011-08-02 16:35:10
400路由器呃?你在哪里工作?谷歌?这似乎是一个相当大的数量没有思科路由器和安全设备管理器.... http://www.cisco.com/en/US/products/sw/secursw/ps5318/index.html –
调查[期望](http://www.nist.gov/el/msid/expect.cfm)。 – nmichaels