日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Linux網(wǎng)絡調(diào)試必備:抓網(wǎng)卡出口數(shù)據(jù)(linux抓網(wǎng)卡出口數(shù)據(jù))

在網(wǎng)絡調(diào)試過程中,有很多情況下需要抓取網(wǎng)絡流量數(shù)據(jù)來分析網(wǎng)絡問題。抓取網(wǎng)絡數(shù)據(jù)包是一種非常有效的分析網(wǎng)絡問題的方法,在Linux系統(tǒng)中,我們可以使用類似Tcpdump或Wireshark的工具來抓取網(wǎng)絡數(shù)據(jù)包。但是僅僅使用這些工具僅僅能夠抓取入口數(shù)據(jù),對于調(diào)試網(wǎng)絡問題來說,往往還需要抓取網(wǎng)卡出口的數(shù)據(jù)包。接下來,我們將介紹如何在Linux系統(tǒng)中抓取網(wǎng)卡出口數(shù)據(jù)。

創(chuàng)新互聯(lián)建站專注于中大型企業(yè)的網(wǎng)站建設、成都做網(wǎng)站和網(wǎng)站改版、網(wǎng)站營銷服務,追求商業(yè)策劃與數(shù)據(jù)分析、創(chuàng)意藝術(shù)與技術(shù)開發(fā)的融合,累計客戶近千家,服務滿意度達97%。幫助廣大客戶順利對接上互聯(lián)網(wǎng)浪潮,準確優(yōu)選出符合自己需要的互聯(lián)網(wǎng)運用,我們將一直專注高端網(wǎng)站設計和互聯(lián)網(wǎng)程序開發(fā),在前進的路上,與客戶一起成長!

抓取網(wǎng)卡出口數(shù)據(jù)

抓取網(wǎng)卡出口數(shù)據(jù)的常見場景是調(diào)試網(wǎng)絡防火墻或者路由器等設備時。在這種情況下,我們需要抓取從網(wǎng)卡中發(fā)出的數(shù)據(jù),可以使用libpcap庫中的pcap_sendpacket函數(shù)發(fā)送一個數(shù)據(jù)包到網(wǎng)卡中,并通過數(shù)據(jù)包捕獲工具抓取數(shù)據(jù)包。

我們需要打開一個網(wǎng)卡并捕獲它的數(shù)據(jù)包,可以使用以下命令:

“`

sudo tcpdump -i eth0 -w /tmp/capture.pcap

“`

這樣,我們就可以在/tmp目錄下生成一個捕獲網(wǎng)卡eth0數(shù)據(jù)的文件。接下來,我們就可以使用libpcap庫中的pcap_sendpacket函數(shù)向網(wǎng)卡中發(fā)送數(shù)據(jù)包并捕獲。下面是一個捕獲出口數(shù)據(jù)包的示例:

“`C

#include

#include

#include

#define ETHER_ADDR_LEN 6

/* Ethernet header */

struct sniff_ethernet {

u_char ether_dhost[ETHER_ADDR_LEN]; /* destination host address */

u_char ether_shost[ETHER_ADDR_LEN]; /* source host address */

u_short ether_type; /* IP? ARP? RARP? etc */

};

/* IP header */

struct sniff_ip {

u_char ip_vhl; /* version > 2 */

u_char ip_tos; /* type of service */

u_short ip_len; /* total length */

u_short ip_id; /* identification */

u_short ip_off; /* fragment offset field */

#define IP_RF 0x8000 /* reserved fragment flag */

#define IP_DF 0x4000 /* don’t fragment flag */

#define IP_MF 0x2023 /* more fragments flag */

#define IP_OFFMASK 0x1fff /* mask for fragmenting bits */

u_char ip_ttl; /* time to live */

u_char ip_p; /* protocol */

u_short ip_sum; /* checksum */

struct in_addr ip_src,ip_dst; /* source and dest address */

};

void send_packet(char *iface, u_char *pkt, int pkt_len)

{

char errbuf[PCAP_ERRBUF_SIZE];

pcap_t *iface_handle;

u_char* pkt_ptr;

struct pcap_pkthdr *hdr;

struct sniff_ethernet *eth_hdr;

struct sniff_ip *ip_hdr;

iface_handle = pcap_open_live(iface, 65535 /* snaplen */, 0 /* non-promiscuous */, 1000 /* read timeout, ms */, errbuf);

if (iface_handle == NULL) {

printf(“Fled to open device %s: %s\n”, iface, errbuf);

exit(1);

}

pkt_ptr = pkt;

memset((void *)pkt_ptr, 0, pkt_len);

memcpy((void *)pkt_ptr, “\x00\x11\x22\x33\x44\x55”, ETHER_ADDR_LEN); // destination MAC

memcpy((void *)(pkt_ptr+ETHER_ADDR_LEN), “\x66\x55\x44\x33\x22\x11”, ETHER_ADDR_LEN); // source MAC

eth_hdr = (struct sniff_ethernet *)pkt_ptr;

eth_hdr->ether_type = htons(0x0800);

pkt_ptr += sizeof(struct sniff_ethernet);

ip_hdr = (struct sniff_ip *)pkt_ptr;

ip_hdr->ip_vhl = 0x45; // version 4 and header length 5

ip_hdr->ip_tos = 0x00; // type of service

ip_hdr->ip_len = htons(pkt_len – sizeof(struct sniff_ethernet)); // length of the packet without the Ethernet header

ip_hdr->ip_id = 0x0000; // identification

ip_hdr->ip_off = 0x0000; // no fragmentation

ip_hdr->ip_ttl = 0xff; // time to live

ip_hdr->ip_p = 0x11; // protocol: ICMP

ip_hdr->ip_src.s_addr = inet_addr(“192.168.1.1”); // source IP address

ip_hdr->ip_dst.s_addr = inet_addr(“192.168.1.2”); // destination IP address

pkt_ptr += sizeof(struct sniff_ip);

/* dummy payload, 48 bytes */

memset((void *)pkt_ptr, ‘A’, 48);

pcap_sendpacket(iface_handle, pkt, pkt_len);

printf(“Packet sent to %s\n”, iface);

hdr = NULL;

pkt_ptr = NULL;

pkt_ptr = pcap_next(iface_handle, hdr);

if (pkt_ptr != NULL) {

printf(“Packet captured from device %s, length %d\n”, iface, hdr->len);

/* print ethernet and IP headers */

eth_hdr = (struct sniff_ethernet *)pkt_ptr;

printf(“source MAC address: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n”, eth_hdr->ether_shost[0], eth_hdr->ether_shost[1], eth_hdr->ether_shost[2], eth_hdr->ether_shost[3], eth_hdr->ether_shost[4], eth_hdr->ether_shost[5]);

printf(“destination MAC address: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n”, eth_hdr->ether_dhost[0], eth_hdr->ether_dhost[1], eth_hdr->ether_dhost[2], eth_hdr->ether_dhost[3], eth_hdr->ether_dhost[4], eth_hdr->ether_dhost[5]);

ip_hdr = (struct sniff_ip *)(pkt_ptr + sizeof(struct sniff_ethernet));

printf(“source IP address: %s\n”, inet_ntoa(ip_hdr->ip_src));

printf(“destination IP address: %s\n”, inet_ntoa(ip_hdr->ip_dst));

}

return;

}

int mn() {

char iface[] = “eth0”;

u_char pkt[1024];

memset((void *)pkt, 0, sizeof(pkt));

send_packet(iface, pkt, 1024);

return 0;

}

“`

在這個例子中,我們使用的是pcap_sendpacket函數(shù)發(fā)送一個數(shù)據(jù)包到網(wǎng)卡中??梢钥吹轿覀兿仁褂胮cap_open_live打開了一個網(wǎng)卡句柄iface_handle,然后我們構(gòu)造了一個Ethernet和IP數(shù)據(jù)包并使用pcap_sendpacket函數(shù)將該數(shù)據(jù)包發(fā)送到網(wǎng)卡中。最后使用pcap_next函數(shù)捕獲從該網(wǎng)卡中發(fā)出的數(shù)據(jù)包。

相關(guān)問題拓展閱讀:

  • 在RHEL5.5下,如何做到從一個網(wǎng)卡讀取數(shù)據(jù),從另外一個網(wǎng)卡發(fā)送出去?

在RHEL5.5下,如何做到從一個網(wǎng)卡讀取數(shù)據(jù),從另外一個網(wǎng)卡發(fā)送出去?

你可以派態(tài)這樣試試:

1.開啟Linux的路由功粗孫能

2.在Linux中設置巖羨鏈一條默認路由,出口選擇eth1

關(guān)于linux 抓網(wǎng)卡出口數(shù)據(jù)的介紹到此就結(jié)束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關(guān)注本站。

創(chuàng)新互聯(lián)成都網(wǎng)站建設公司提供專業(yè)的建站服務,為您量身定制,歡迎來電(028-86922220)為您打造專屬于企業(yè)本身的網(wǎng)絡品牌形象。
成都創(chuàng)新互聯(lián)品牌官網(wǎng)提供專業(yè)的網(wǎng)站建設、設計、制作等服務,是一家以網(wǎng)站建設為主要業(yè)務的公司,在網(wǎng)站建設、設計和制作領(lǐng)域具有豐富的經(jīng)驗。


網(wǎng)頁名稱:Linux網(wǎng)絡調(diào)試必備:抓網(wǎng)卡出口數(shù)據(jù)(linux抓網(wǎng)卡出口數(shù)據(jù))
本文鏈接:http://www.dlmjj.cn/article/ccdpehe.html