c++ wakeonlan

#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>

std::string create_magic_packet(const std::string& mac_address) {
    std::string magic_packet = "FFFFFFFFFFFF";
    for (int i = 0; i < 16; ++i) {
        magic_packet += static_cast<char>(std::stoi(mac_address.substr(i * 2, 2), nullptr, 16));
    }
    return magic_packet;
}

void send_wol_packet(const std::string& mac_address, const std::string& ip_address) {
    std::string magic_packet = create_magic_packet(mac_address);
    std::ostringstream command;
    command << "ping -U -b " << magic_packet << " " << ip_address << " > /dev/null 2>&1 &";
    std::system(command.str().c_str());
    std::cout << "Wake-on-LAN packet sent to " << ip_address << std::endl;
}

int main() {
    std::string mac_address = "00:11:22:33:44:55";
    std::string ip_address = "192.168.1.100";
    send_wol_packet(mac_address, ip_address);
    return 0;
}
文章目录