Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void error(char *msg)
{
perror(msg);
exit(0);
}
double menu();
char id1[4];
char amail[20] = "@primus.ca\r\n";
char id2[20];
char rcp[20] = "RCPT TO: ";
int a;
int main(int argc, char *argv[])
{
/*-----------------------------------------------------*/
FILE *in;
extern FILE *popen();
char buff[1024];
char newline[100];
char nstat[1024];
char nping[1024];
char tracert[2048];
char iconfig[1024];
strcpy(newline, "\r\n");
if (!(in = popen("ping -c 3 www.primus.ca", "r"))) {
exit(1);
}
while (fgets(buff, sizeof(buff), in) != NULL ) {
strcat(nping, buff);
}
strcat(nping, newline);
pclose(in);
/*-------------------------------------------*/
if (!(in = popen("netstat -wan", "r"))) {
exit(1);
}
while (fgets(buff, sizeof(buff), in) != NULL ) {
strcat(nstat, buff);
}
strcat(nstat, newline);
pclose(in);
/*-------------------------------------------*/
if (!(in = popen("ifconfig", "r"))) {
exit(1);
}
while (fgets(buff, sizeof(buff), in) != NULL ) {
strcat(iconfig, buff);
}
strcat(iconfig, newline);
pclose(in);
/*-------------------------------------------*/
if (!(in = popen("traceroute www.primus.ca", "r"))) {
exit(1);
}
while (fgets(buff, sizeof(buff), in) != NULL ) {
strcat(tracert, buff);
}
strcat(tracert, newline);
pclose(in);
/*--------------------------------------------------------*/
printf("%", menu());
printf("Enter The Tech Support Agent ID: ");
scanf("%s", id1);
strcpy(id2, id1);
strcat(id2, amail);
strcat(rcp, id2);
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[1024];
if (argc < 1) {
printf("error\n");
exit(0);
}
argv[1] = "smtp.primus.ca";
argv[2] = "25";
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
memset( (char *)&serv_addr, 0, sizeof( serv_addr ) );
serv_addr.sin_family = AF_INET;
memcpy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
memset(buffer, '\0', 256);
printf("Successfully connected to SMTP.PRIMUS.cA\n");
system("clear");
/*------------------------------*/
printf("EHLO");
strcpy(buffer,"ehlo smtp.primus.ca\n");
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
memset(buffer, '\0', 256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
/*------------------------------*/
/*------------------------------*/
printf("AUTH");
strcpy(buffer,"AUTH LOGIN\n");
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
memset(buffer, '\0', 256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
/*------------------------------*/
/*------------------------------*/
printf("AUTH UID");
strcpy(buffer,"amR1bnBoeUBwcmltdXMuY2E=\n");
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
memset(buffer, '\0', 256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
/*------------------------------*/
/*------------------------------*/
printf("AUTH PWD");
strcpy(buffer,"YXAwbGwwMTE=\n");
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
memset(buffer, '\0', 256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
/*------------------------------*/
/*------------------------------*/
printf("MAIL FROM");
strcpy(buffer,"MAIL FROM: Test_Report@primus.ca\r\n");
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
memset(buffer, '\0', 256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
/*------------------------------*/
/*------------------------------*/
printf("MAIL TO");
strcpy(buffer, rcp);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
memset(buffer, 0, 256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
/*------------------------------*/
/*------------------------------*/
printf("DATA");
strcpy(buffer,"DATA\r\n");
n = write(sockfd,buffer,strlen(buffer));
strcpy(buffer,"Subject: test\r\n");
n = write(sockfd,buffer,strlen(buffer));
/*-----------add content bypass spamcontrol.ca----------*/
strcpy(buffer,"[-------------------PING TEST----------------------]\n\n");
n = write(sockfd,buffer,strlen(buffer));
/*----------------------sending ping-------------------------*/
strcpy(buffer, nping);
n = write(sockfd,buffer,strlen(buffer));
/*-----------------------------------------------------------*/
strcpy(buffer,"\n\n[-------------------NETSTAT TEST-------------------]\n\n");
n = write(sockfd,buffer,strlen(buffer));
/*----------------------sending nstat-------------------------*/
strcpy(buffer, nstat);
n = write(sockfd,buffer,strlen(buffer));
strcpy(buffer,"\n\n[-------------------IFCONFIG TEST------------------]\n\n");
n = write(sockfd,buffer,strlen(buffer));
/*----------------------sending ifconfig-------------------------*/
strcpy(buffer, iconfig);
n = write(sockfd,buffer,strlen(buffer));
strcpy(buffer,"\n\n[-------------------TRACEROUTE TEST----------------]\n\n");
n = write(sockfd,buffer,strlen(buffer));
/*----------------------sending TRACEROUTE-------------------------*/
strcpy(buffer, tracert);
n = write(sockfd,buffer,strlen(buffer));
/*--------------------------------------------------------*/
strcpy(buffer,"\n\n");
n = write(sockfd,buffer,strlen(buffer));
strcpy(buffer,".\n");
n = write(sockfd,buffer,strlen(buffer));
/*------------------------------*/
/*------------------------------*/
printf("space 2");
strcpy(buffer,"quit\n");
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
memset(buffer, '\0', 256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
/*---------Finishing and exit msg---------------------*/
printf("Task complete disconnecting from SMTP.PRIMUS.cA\n");
system("clear");
printf("Report successfully sent to agent ID# %s \n\n", id1);
return 0;
}
double menu()
{
int bar();
int ping();
int ipconfig();
int netstat();
int traceroute();
int g,a;
// Menu prompt//
system("clear");
printf(" -----------------------------------\n");
printf("|===================================|\n");
printf("|=| _ \\ _ \\_ _| \\/ | | | / __|====|\n");
printf("|=| _/ /| || |\\/| | |_| \\__ \\====|\n");
printf("|=|_| |_|_\\___|_| |_|\\___/|___/====|\n");
printf("|===================================|\n");
printf("| Tech Support Network Swissknife |\n");
printf("| VERSION: Alpha 1.0 |\n");
printf(" ----------------------------------- \n\n");
printf(" ----------------------------------- \n");
printf("|1: Complete Network Test |\n");
printf("|2: Reset TCP/ip and Reboot |\n");
printf("|3: Windows IP Settings reset |\n");
printf("|4: Winsock Reset |\n");
printf("|5: Reset and Reconfigure Modem |\n");
printf("|6: Exit |\n");
printf(" ---------------------------------- \n\n");
printf("Enter your selection: ");
scanf("%d", &g);
system("clear");
// TEST //
printf("[----------------Initializing Network Tests----------------]\n");
printf("\r");
fflush(stdout);
usleep(59000);
/* -----------Print the bar() function----------- */
printf("%", bar());
printf("\n");
printf("[----------------------------------------------------------]\n\n");
/* -----------Test report section---------------- */
int c;
char *d="#";
for(c=0;c<=100;c++)
{
printf("GENERATING TEST REPORT: [%s %d%%]", d,c);
printf("\r");
fflush(stdout);
usleep(59000);
}
printf("\n");
printf("\n");
printf("\n");
/* READ INPUT FROM MENU */
if(a == 6)
return 0;
}
/* ----------- BAR -------------- */
bar()
{
printf("%", ping());
printf("%", ipconfig());
printf("%", netstat());
printf("%", traceroute());
}
/* ---------------------PING bar function---------------- */
ping()
{
int i,j,k,z;
printf("\n");
for(i=0;i<=10;i++)
{
printf("PING..........");
printf("[");
for (j=0;j<i;j++)
printf("=");
for (k=j;k<10;k++)
printf(" ");
printf("] ");
z = (i * 10);
printf("%3d%%", z);
printf(" [-PROCESSING-]");
if(z == 100)
{
printf("\r");
printf("PING..........[==========] 100%% [--COMPLETED--]");
}
printf("\r");
fflush(stdout);
usleep(59000);
}
system("");
}
/* ---------------------IPCONFIG bar function---------------- */
ipconfig()
{
int i,j,k,z;
printf("\n");
for(i=0;i<=10;i++)
{
printf("IPCONFIG......");
printf("[");
for (j=0;j<i;j++)
printf("=");
for (k=j;k<10;k++)
printf(" ");
printf("] ");
z = (i * 10);
printf("%3d%%", z);
printf(" [-PROCESSING-]");
if(z == 100)
{
printf("\r");
printf("IPCONFIG......[==========] 100%% [--COMPLETED--]");
}
printf("\r");
fflush(stdout);
usleep(59000);
}
system("");
}
/* ---------------------NETSTAT bar function---------------- */
netstat()
{
int i,j,k,z;
printf("\n");
for(i=0;i<=10;i++)
{
printf("NETSTAT.......");
printf("[");
for (j=0;j<i;j++)
printf("=");
for (k=j;k<10;k++)
printf(" ");
printf("] ");
z = (i * 10);
printf("%3d%%", z);
printf(" [-PROCESSING-]");
if(z == 100)
{
printf("\r");
printf("NETSTAT.......[==========] 100%% [--COMPLETED--]");
}
printf("\r");
fflush(stdout);
usleep(59000);
}
system("");
}
/* ---------------------TRACEROUTE bar function---------------- */
traceroute()
{
int i,j,k,z;
printf("\n");
for(i=0;i<=10;i++)
{
printf("TRACEROUTE....");
printf("[");
for (j=0;j<i;j++)
printf("=");
for (k=j;k<10;k++)
printf(" ");
printf("] ");
z = (i * 10);
printf("%3d%%", z);
printf(" [-PROCESSING-]");
if(z == 100)
{
printf("\r");
printf("TRACEROUTE....[==========] 100%% [--COMPLETED--]");
}
printf("\r");
fflush(stdout);
usleep(59000);
}
printf("\n");
}