返回

如何使用脚本获取外网IP并将其发送到指定邮箱?

电脑技巧

如何获取外网 IP 并发送到指定邮箱

在使用计算机时,我们经常会遇到外网 IP。它类似于门牌号,可以标识我们计算机在互联网上的位置。出于远程访问服务器或搭建网站等原因,有时我们需要获取外网 IP。此外,将外网 IP 发送到指定邮箱也是一种常见的需求,比如在服务器发生故障时,我们可以通过邮箱收到通知。

步骤一:获取外网 IP

获取外网 IP 有多种方法,具体取决于您使用的操作系统。

Windows 系统:

  1. 打开命令提示符:按 Win + R,输入 cmd,然后按 Enter。
  2. 使用命令获取外网 IP:键入 curl ifconfig.me

Linux 系统:

  1. 打开终端:按 Ctrl + Alt + T。
  2. 使用命令获取外网 IP:键入 curl ifconfig.me

macOS 系统:

  1. 打开终端:按 Command + 空格键,输入 Terminal,然后按 Enter。
  2. 使用命令获取外网 IP:键入 curl ifconfig.me

步骤二:编写脚本

获取外网 IP 后,我们可以编写一个脚本来将其发送到指定的邮箱。以下是使用不同编程语言编写的脚本示例:

Python 脚本:

import smtplib
import requests

def get_external_ip():
    return requests.get('https://ifconfig.me').text

def send_email(recipient, subject, body):
    smtp_server = 'smtp.gmail.com'
    smtp_port = 587
    username = 'your_email_address'
    password = 'your_email_password'
    server = smtplib.SMTP(smtp_server, smtp_port)
    server.starttls()
    server.login(username, password)
    server.sendmail(username, recipient, 'Subject: {} \n\n {}'.format(subject, body))
    server.quit()

if __name__ == '__main__':
    recipient = 'recipient_email_address'
    subject = 'External IP Address'
    body = 'Your external IP address is {}'.format(get_external_ip())
    send_email(recipient, subject, body)

Bash 脚本:

#!/bin/bash

EXTERNAL_IP=$(curl ifconfig.me)
EMAIL_ADDRESS=recipient_email_address
SUBJECT="External IP Address"
BODY="Your external IP address is $EXTERNAL_IP"

echo "$BODY" | mail -s "$SUBJECT" "$EMAIL_ADDRESS"

C++ 脚本:

#include <iostream>
#include <string>
#include <curl/curl.h>

using namespace std;

string get_external_ip() {
    CURL *curl;
    CURLcode res;
    string response;

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "https://ifconfig.me");
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, [](void *ptr, size_t size, size_t nmemb, void *stream) {
            string *str = (string *)stream;
            str->append((char *)ptr, size * nmemb);
            return size * nmemb;
        });
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
    }

    return response;
}

int main() {
    string external_ip = get_external_ip();
    string email_address = "recipient_email_address";
    string subject = "External IP Address";
    string body = "Your external IP address is " + external_ip;

    system(("echo " + body + " | mail -s " + subject + " " + email_address).c_str());

    return 0;
}

Java 脚本:

import java.net.URL;
import java.net.URLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;
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;

public class GetExternalIPAndSendEmail {

    public static void main(String[] args) {
        String externalIP = getExternalIP();
        String recipientEmailAddress = "recipient_email_address";
        String subject = "External IP Address";
        String body = "Your external IP address is " + externalIP;

        sendEmail(recipientEmailAddress, subject, body);
    }

    private static String getExternalIP() {
        try {
            URL url = new URL("https://ifconfig.me");
            URLConnection connection = url.openConnection();
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String externalIP = reader.readLine();
            reader.close();
            return externalIP;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static void sendEmail(String recipientEmailAddress, String subject, String body) {
        try {
            Properties properties = new Properties();
            properties.put("mail.smtp.host", "smtp.gmail.com");
            properties.put("mail.smtp.port", "587");
            properties.put("mail.smtp.starttls.enable", "true");
            properties.put("mail.smtp.auth", "true");

            Session session = Session.getDefaultInstance(properties);
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("your_email_address"));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipientEmailAddress));
            message.setSubject(subject);
            message.setText(body);

            Transport.send(message);
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

PHP 脚本:

<?php

function get_external_ip() {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://ifconfig.me");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $external_ip = curl_exec($ch);
    curl_close($ch);
    return $external_ip;
}

function send_email($recipient, $subject, $body) {
    $headers = "From: your_email_address" . "\r\n" .
        "Reply-To: your_email_address" . "\r\n" .
        "X-Mailer: PHP/" . phpversion();
    mail($recipient, $subject, $body, $headers);
}

$external_ip = get_external_ip();
$recipient = "recipient_email_address";
$subject = "External IP Address";
$body = "Your external IP address is $external_ip";

send_email($recipient, $subject, $body);

步骤三:运行脚本

根据您使用的操作系统,选择一种脚本并使用以下步骤运行它:

Windows 系统:

  1. 将脚本保存为文件,例如 get_external_ip_and_send_email.py
  2. 在命令提示符中,导航到脚本所在的目录。
  3. 运行脚本:python get_external_ip_and_send_email.py

Linux 系统:

  1. 将脚本保存为文件,例如 get_external_ip_and_send_email.sh
  2. 在终端中,导航到脚本所在的目录。
  3. 运行脚本:bash get_external_ip_and_send_email.sh

macOS 系统:

  1. 将脚本保存为文件,例如 get_external_ip_and_send_email.sh
  2. 在终端中,导航到脚本所在的目录。
  3. 运行脚本:bash get_external_ip_and_send_email.sh

结论

通过使用脚本,我们可以轻松地获取外网 IP 并将其发送到指定的邮箱。这对于远程管理服务器、诊断网络问题或出于其他目的非常有用。希望本文能帮助您实现这一目标。

常见问题解答

  1. 为什么我需要获取外网 IP?

    • 外网 IP 是您的计算机在互联网上的公开地址,它可以用于远程访问、网站托管等。
  2. 如何更改我的外网 IP?

    • 大多数家庭用户无法直接更改他们的外网 IP。但是,可以使用 VPN 或代理服务来掩盖您的实际 IP。
  3. **