ping通谷歌後發送QQ郵件通知

前言

  國慶期間,聽說是爲了防止有人在重大節日發表不正當言論,不少能夠kxsw的ip都被封了,但是何時纔會解封呢,不能沒事就去ping一下吧,因此我寫了個定時任務,定時ping谷歌服務器,若是ping通則發郵件通知,來看看是怎麼作的吧! java

Ping工具類

  首先保證你的電腦以前是能夠訪問谷歌的(ss),這個類是專門用來ping谷歌的,至關於手動輸入ping www.google.comspring

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
* @author: zp
* @Date: 2019-10-08 11:31
* @Description:
*/
public class PingUtils {

        public static boolean ping02(String ipAddress){
                // 讀取的行信息
                String line =line = null;
                // 至關於cmd服務
                Process exec = null;
                // ping 的結果
                boolean res = true;
                try {
                        exec = Runtime.getRuntime().exec("ping " + ipAddress);
                        BufferedReader br = new BufferedReader(new InputStreamReader(exec.getInputStream()));
                        // 最多執行三秒
                        long endTime = System.currentTimeMillis()+3000;
                        // 測試輸出行中是否有ttl字符串,有就說明ping通了
                        while ((res=true)==true&&(line = br.readLine()).indexOf("ttl")<0){
                                System.out.println("line = " + line);
                                res = false;
                                // 三秒仍是ping不通則放棄嘗試
                                if(System.currentTimeMillis()>endTime){
                                        break;
                                }
                        }
                        System.out.println("line = " + line);
                } catch (IOException e) {
                        e.printStackTrace();
                } finally {
                        exec.destroy();
                        return res;
                }
        }

}
複製代碼

發送郵件的工具類

發送郵件首先須要引入兩個jar包:api

  • maven
<dependency>
        <groupId>javax.mail</groupId>
        <artifactId>javax.mail-api</artifactId>
        <version>1.6.2</version>
</dependency>
<dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.6.2</version>
</dependency>複製代碼

  • gradle
compile group: 'javax.mail', name: 'javax.mail-api', version: '1.6.2'
compile group: 'com.sun.mail', name: 'javax.mail', version: '1.6.2'複製代碼

值得注意的是:郵箱密碼是 QQ郵箱開通的stmp服務後獲得的客戶端受權碼!!!相似於sjhabsjhdabasf這種字串。安全

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
* @author: zp
* @Date: 2019-10-08 14:13
* @Description:
*/
public class MailUtils {
        public static void sendQQMail() throws AddressException, MessagingException {
                Properties properties = new Properties();
                // 鏈接協議
                properties.put("mail.transport.protocol", "smtp");
                // 主機名
                properties.put("mail.smtp.host", "smtp.qq.com");
                // 端口號
                properties.put("mail.smtp.port", 465);
                properties.put("mail.smtp.auth", "true");
                // 設置是否使用ssl安全鏈接 ---通常都使用
                properties.put("mail.smtp.ssl.enable", "true");
                // 設置是否顯示debug信息 true 會在控制檯顯示相關信息
                properties.put("mail.debug", "true");
                // 獲得回話對象
                Session session = Session.getInstance(properties);
                // 獲取郵件對象
                Message message = new MimeMessage(session);
                // 設置發件人郵箱地址
                message.setFrom(new InternetAddress("1226364229@qq.com"));
                // 設置收件人郵箱地址,能夠有多個收件人
                message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("313694179@qq.com")});
                // 設置郵件標題
                message.setSubject("ping結果");
                // 設置郵件內容
                message.setText("您的IP解封了,成功ping通谷歌!");
                // 獲得郵差對象
                Transport transport = session.getTransport();
                // 鏈接本身的郵箱帳戶,密碼爲QQ郵箱開通的stmp服務後獲得的客戶端受權碼!!!
                transport.connect("1226364229@qq.com", "**********");
                // 發送郵件
                transport.sendMessage(message, message.getAllRecipients());
                transport.close();
        }
}複製代碼

定時任務類

這個類採用的是基於SchedulingConfigurer接口的,在ping通後,會修改cron表達式的值,防止重複發送郵件。(以前一分鐘測一次,如今一天測一次)服務器

import com.example.demojpa.utils.MailUtils;
import com.example.demojpa.utils.PingUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @author: zp
* @Date: 2019-10-08 16:22:20
* @Description:
*/
@Configuration
@EnableScheduling
public class TaskBasedInterface implements SchedulingConfigurer {

        /**
        * 每小時執行一次
        */
        private static String cron = "0 */1 * * * ?";

        @Override
        public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
                taskRegistrar.addCronTask(()-> {
                        // 測試鏈接
                        boolean connected = PingUtils.ping02("www.google.com");
                        if(connected){
                                try {
                                        // 修改cron表達式,天天凌晨執行給我發郵件
                                        cron = "0 0 0 * * ? *";
                                        MailUtils.sendQQMail();
                                        log("已成功ping通!");
                                        return;
                                } catch (Exception e) {
                                        e.printStackTrace();
                                }
                        }
                        log("ping不通");
                },cron);
        }

        public static void log(String message) {
                DateFormat df = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
                String date = df.format(new Date(System.currentTimeMillis()));
                System.out.println(date + " "+message);
        }
}
複製代碼

結果

file

file

後語

  能用已有的知識來作一些有趣的事真的能提升你對技術的興趣呀,反正我是體會到了。session

file

若是以爲有用就關注我吧~maven

相關文章
相關標籤/搜索