歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

C語言檢查本機公網IP並發送郵件

這是一個用來獲取本機公網IP地址,並檢查是否是配置裡保存的IP地址,如果不是,就向指定的郵箱發送一個郵件,報告這個IP地址的一段小代碼。放到開機啟動中,電腦不設密碼的時候萬一丟了,還能有個念想..........功能不強——只簡單實現了獲取地址,發送郵件的功能,僅供練習之用,而且只能用163的郵箱來發郵件......

附上資源下載地址

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2014年資料/5月/19日/C語言檢查本機公網IP並發送郵件

下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm

發送郵件的函數是百度上copy的,只是簡單的修改了一下

#include <windows.h>
#include <stdio.h>
#include <WinSock.h>
#include <URLMON.H>
#include <FSTREAM>
#include <iostream>
#define N 500
using namespace std;
#pragma  comment(lib, "ws2_32.lib")
#pragma comment(lib,"urlmon.lib")

struct Base64Date6
{
 unsigned int d4:6;
 unsigned int d3:6;
 unsigned int d2:6;
 unsigned int d1:6;
};
// 協議中加密部分使用的是base64方法
char ConvertToBase64  (char  c6);
void EncodeBase64    (char  *dbuf, char *buf128, int len);
void SendMail        (char  *emailTo,char *emailFrom,char *emailPd,char *body);
int  OpenSocket      (struct sockaddr *addr);

int CheckIP(char currentIP[])      //0讀取文件失敗,1IP非信任 2下載文件失敗 3信任的IP地址
{
 HRESULT hr=URLDownloadToFile(0,"http://www.ip138.com/ips1388.asp","currentIP.txt",0,NULL);
 if(hr!=S_OK)
 {
  return 2;
 }
 ifstream readFile("IPlist.txt");
 if(!readFile)
 {
  cout<<"Can't find IPlist.txt"<<endl;
  return 0;
 }
 char listIP[N];
 readFile.getline(listIP,N,'\n');
 readFile.close();
 readFile.clear();
 readFile.open("currentIP.txt");
 if(!readFile)
 {
  cout<<"Can't find currentIP.txt"<<endl;
  return 0;
 }
 char readTemp[N*4];
 while(!readFile.eof())
 {
   readFile.getline(readTemp,N*4,'\n');  //getline函數參數說明:參數1-->指針頭,參數2-->最大讀取值,參數3-->讀取結束符
   if(readFile.fail()) break;
   else
   {
    char *temp=strstr(readTemp,"您的IP地址是");
    if(temp!=NULL)
    {
     temp=strchr(temp,'[');
     for(int i=0;;i++)
     {
      if(temp[i+1]==']')
      {
       currentIP[i]='\0';
       break;
      }
      else
       currentIP[i]=temp[i+1];
     }
     break;
    }
   }
 }
 readFile.close();
 readFile.clear();
 char *p=strstr(listIP,currentIP);
 if(p!=NULL)
 {
  cout<<"Been trusted!"<<endl;
  return 3;
 }
 else
 {
  cout<<"Unknown Address!Send E-mail immediately!"<<endl;
  return 1;
 }
}

int main()
{
 char currentIP[16];
 int n=0,ir;
 while(1)
 {
  ir=CheckIP(currentIP);
  if(ir==2)
  {
   if(n!=10)
   {
    cout<<"Connection failed...It's too bad!I will try again one minute later."<<endl;
    n++;
    Sleep(60*1000);
   }
   else
   {
    cout<<"Connection failed for 10 times.Application will exit immediately!"<<endl;
    return 0;
   }
  }
  else
   break;
 }
 if(ir==0)
 {
  cout<<"Read file error!Application will exit immediately!"<<endl;
  return 0;
 }
 else if(ir==3)
 {
  cout<<"Been trusted!Have fun!"<<endl;
  return 0;
 }
 else
 {
  char emailTo[100];
  char emailFrom[100];
  char emailPd[100];
  fstream readFile("email.ini");
  if(!readFile)
  {
   cout<<"Can't open email.ini"<<endl;
   return 0;
  }
  while(!readFile.eof())
  {
   readFile.getline(emailTo,100,'\n');  //getline函數參數說明:參數1-->指針頭,參數2-->最大讀取值,參數3-->讀取結束符
   readFile.getline(emailFrom,100,'\n');
   readFile.getline(emailPd,100,'\n');
  }
  readFile.close();
  readFile.clear();
  char EmailContents[N]  = "From: \"YourPC\"<[email protected]>\r\n"
   "To: \"MyMaster\"<[email protected]>\r\n"
   "Subject: IP address has been changed!\r\n\r\n";
  strcat(EmailContents,"The address is:");
  strcat(EmailContents,currentIP);
  strcat(EmailContents,"!!!Please check!");
  SendMail(emailTo,emailFrom,emailPd,EmailContents);
  return 0;
 }
}

char ConvertToBase64(char uc)
{
 if(uc < 26)
 {
  return 'A' + uc;
 }
 if(uc < 52)
 {
  return 'a' + (uc - 26);
 }
 if(uc < 62)
 {
  return '0' + (uc - 52);
 }
 if(uc == 62)
 {
  return '+';
 }
 return '/';
}

// base64的實現
void EncodeBase64(char *dbuf, char *buf128, int len)
{
 struct Base64Date6 *ddd      = NULL;
 int          i        = 0;
 char          buf[256] = {0};
 char          *tmp    = NULL;
 char          cc      = '\0';

 memset(buf, 0, 256);
 strcpy_s(buf, 256, buf128);
 for(i = 1; i <= len/3; i++)
 {
  tmp            = buf + (i-1)*3;
  cc              = tmp[2];
  tmp[2]          = tmp[0];
  tmp[0]          = cc;
  ddd            = (struct Base64Date6 *)tmp;
  dbuf[(i-1)*4+0] = ConvertToBase64((unsigned int)ddd->d1);
  dbuf[(i-1)*4+1] = ConvertToBase64((unsigned int)ddd->d2);
  dbuf[(i-1)*4+2] = ConvertToBase64((unsigned int)ddd->d3);
  dbuf[(i-1)*4+3] = ConvertToBase64((unsigned int)ddd->d4);
 }
 if(len % 3 == 1)
 {
  tmp            = buf + (i-1)*3;
  cc              = tmp[2];
  tmp[2]          = tmp[0];
  tmp[0]          = cc;
  ddd            = (struct Base64Date6 *)tmp;
  dbuf[(i-1)*4+0] = ConvertToBase64((unsigned int)ddd->d1);
  dbuf[(i-1)*4+1] = ConvertToBase64((unsigned int)ddd->d2);
  dbuf[(i-1)*4+2] = '=';
  dbuf[(i-1)*4+3] = '=';
 }
 if(len%3 == 2)
 {
  tmp            = buf+(i-1)*3;
  cc              = tmp[2];
  tmp[2]          = tmp[0];
  tmp[0]          = cc;
  ddd            = (struct Base64Date6 *)tmp;
  dbuf[(i-1)*4+0] = ConvertToBase64((unsigned int)ddd->d1);
  dbuf[(i-1)*4+1] = ConvertToBase64((unsigned int)ddd->d2);
  dbuf[(i-1)*4+2] = ConvertToBase64((unsigned int)ddd->d3);
  dbuf[(i-1)*4+3] = '=';
 }
 return;
}
// 發送郵件
void SendMail(char *emailTo,char *emailFrom,char *emailPd,char *body)
{
 int    sockfd    = {0};
 char    buf[1500]  = {0};
 char    rbuf[1500] = {0};
 char    login[128] = {0};
 char    pass[128]  = {0};
 WSADATA WSAData;
 struct sockaddr_in their_addr = {0};
 WSAStartup(MAKEWORD(2, 2), &WSAData);
 memset(&their_addr, 0, sizeof(their_addr));

 their_addr.sin_family = AF_INET;
 their_addr.sin_port  = htons(25);
 hostent* hptr        = gethostbyname("smtp.163.com");
 memcpy(&their_addr.sin_addr.S_un.S_addr, hptr->h_addr_list[0], hptr->h_length);
 printf("IP of smpt.163.com is : %d:%d:%d:%d\n",
  their_addr.sin_addr.S_un.S_un_b.s_b1,
  their_addr.sin_addr.S_un.S_un_b.s_b2,
  their_addr.sin_addr.S_un.S_un_b.s_b3,
  their_addr.sin_addr.S_un.S_un_b.s_b4);

 // 連接郵件服務器,如果連接後沒有響應,則2 秒後重新連接
 sockfd = OpenSocket((struct sockaddr *)&their_addr);
 memset(rbuf, 0, 1500);
 while(recv(sockfd, rbuf, 1500, 0) == 0)
 {
  cout<<"reconnect..."<<endl;
  Sleep(2000);
  sockfd = OpenSocket((struct sockaddr *)&their_addr);
  memset(rbuf, 0, 1500);
 }

 cout<<rbuf<<endl;

 // EHLO
 memset(buf, 0, 1500);
 sprintf_s(buf, 1500, "EHLO HYL-PC\r\n");
 send(sockfd, buf, strlen(buf), 0);
 memset(rbuf, 0, 1500);
 recv(sockfd, rbuf, 1500, 0);
 cout<<"EHLO REceive: "<<rbuf<<endl;

 // AUTH LOGIN
 memset(buf, 0, 1500);
 sprintf_s(buf, 1500, "AUTH LOGIN\r\n");
 send(sockfd, buf, strlen(buf), 0);
 memset(rbuf, 0, 1500);
 recv(sockfd, rbuf, 1500, 0);
 cout<<"Auth Login Receive: "<<rbuf<<endl;

 // USER
 memset(buf, 0, 1500);
 sprintf_s(buf, 1500, emailFrom);//你的郵箱賬號
 memset(login, 0, 128);
 EncodeBase64(login, buf, strlen(buf));
 sprintf_s(buf, 1500, "%s\r\n", login);
 send(sockfd, buf, strlen(buf), 0);
 cout<<"Base64 UserName: "<<buf<<endl;
 memset(rbuf, 0, 1500);
 recv(sockfd, rbuf, 1500, 0);
 cout<<"User Login Receive: "<<rbuf<<endl;

 // PASSWORD
 sprintf_s(buf, 1500, emailPd);//你的郵箱密碼
 memset(pass, 0, 128);
 EncodeBase64(pass, buf, strlen(buf));
 sprintf_s(buf, 1500, "%s\r\n", pass);
 send(sockfd, buf, strlen(buf), 0);
 cout<<"Base64 Password: "<<buf<<endl;

 memset(rbuf, 0, 1500);
 recv(sockfd, rbuf, 1500, 0);
 cout<<"Send Password Receive: "<<rbuf<<endl;
 // MAIL FROM
 char mailFrom[100]="MAIL FROM: <";
 strcat(mailFrom,emailFrom);
 strcat(mailFrom,">\r\n");
 memset(buf, 0, 1500);
 sprintf_s(buf, 1500, mailFrom);
 send(sockfd, buf, strlen(buf), 0);
 memset(rbuf, 0, 1500);
 recv(sockfd, rbuf, 1500, 0);
 cout<<"set Mail From Receive: "<<rbuf<<endl;
 // RCPT TO 第一個收件人
 sprintf_s(buf, 1500, "RCPT TO:<%s>\r\n", emailTo);
 send(sockfd, buf, strlen(buf), 0);
 memset(rbuf, 0, 1500);
 recv(sockfd, rbuf, 1500, 0);
 cout<<"Tell Sendto Receive: "<<rbuf<<endl;

 // DATA 准備開始發送郵件內容
 sprintf_s(buf, 1500, "DATA\r\n");
 send(sockfd, buf, strlen(buf), 0);
 memset(rbuf, 0, 1500);
 recv(sockfd, rbuf, 1500, 0);
 cout<<"Send Mail Prepare Receive: "<<rbuf<<endl;

 // 發送郵件內容,\r\n.\r\n內容結束標記
 sprintf_s(buf, 1500, "%s\r\n.\r\n", body);
 send(sockfd, buf, strlen(buf), 0);
 memset(rbuf, 0, 1500);
 recv(sockfd, rbuf, 1500, 0);
 cout<<"Send Mail Receive: "<<rbuf<<endl;

 // QUIT
 sprintf_s(buf, 1500, "QUIT\r\n");
 send(sockfd, buf, strlen(buf), 0);
 memset(rbuf, 0, 1500);
 recv(sockfd, rbuf, 1500, 0);
 cout<<"Quit Receive: "<<rbuf<<endl;

 //清理工作
 closesocket(sockfd);
 WSACleanup();
 return;
}
// 打開TCP Socket連接
int OpenSocket(struct sockaddr *addr)
{
 int sockfd = 0;
 sockfd=socket(PF_INET, SOCK_STREAM, 0);
 if(sockfd < 0)
 {
  cout<<"Open sockfd(TCP) error!"<<endl;
  exit(-1);
 }
 if(connect(sockfd, addr, sizeof(struct sockaddr)) < 0)
 {
  cout<<"Connect sockfd(TCP) error!"<<endl;
  exit(-1);
 }
 return sockfd;
}

將C語言梳理一下,分布在以下10個章節中:

  1. Linux-C成長之路(一):Linux下C編程概要 http://www.linuxidc.com/Linux/2014-05/101242.htm
  2. Linux-C成長之路(二):基本數據類型 http://www.linuxidc.com/Linux/2014-05/101242p2.htm
  3. Linux-C成長之路(三):基本IO函數操作 http://www.linuxidc.com/Linux/2014-05/101242p3.htm
  4. Linux-C成長之路(四):運算符 http://www.linuxidc.com/Linux/2014-05/101242p4.htm
  5. Linux-C成長之路(五):控制流 http://www.linuxidc.com/Linux/2014-05/101242p5.htm
  6. Linux-C成長之路(六):函數要義 http://www.linuxidc.com/Linux/2014-05/101242p6.htm
  7. Linux-C成長之路(七):數組與指針 http://www.linuxidc.com/Linux/2014-05/101242p7.htm
  8. Linux-C成長之路(八):存儲類,動態內存 http://www.linuxidc.com/Linux/2014-05/101242p8.htm
  9. Linux-C成長之路(九):復合數據類型 http://www.linuxidc.com/Linux/2014-05/101242p9.htm
  10. Linux-C成長之路(十):其他高級議題

C++ Primer Plus 第6版 中文版 清晰有書簽PDF+源代碼 http://www.linuxidc.com/Linux/2014-05/101227.htm

Copyright © Linux教程網 All Rights Reserved