Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I captured and saved to a file in plain txt from wireshark, i am trying to use awk or any command to extract the time, source ip address and destination address, i tried using grep it gives me all the line containg ip address, pls can anybody help me on this
this is how the information looks when you open the file:
No. Time Source Destination Protocol Info
1617 14.30 10.49.48.95 64.191.203.30 HTTP POST /login HTTP/1.1 (application/x-www-form-urlencoded)
This looks like a simple task for AWK. Assuming that the format is always the same, you simply have to extract by field, using " " (space) as the delimiter.
Example---this print the 2nd, 3rd and 4th field, with tabs:
When I was doing my MSc project, I had to analyse tcpdump data and initially tried string parsing. It was very messy, so I just went and learned to use libpcap. Makes life easier!
As requested, here's a C++ program making use of libpcap to get source and destination addresses and ports and timestamps:
Code:
#include <pcap.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <netinet/ether.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <iostream>
using namespace std;
void packet_handler(u_char *args, const struct pcap_pkthdr *packet_header, const u_char *packet);
int main(int argc, const char *argv[])
{
const char *file = "tcpdump_file";
char error_buffer[PCAP_ERRBUF_SIZE];
pcap_t *handle = pcap_open_offline(file, error_buffer);
pcap_loop(handle, -1, packet_handler, NULL); // Loop until we reach the end of the file
return 0;
}
void packet_handler(u_char *args, const struct pcap_pkthdr *packet_header,
const u_char *packet)
{
// The IP packet is the payload of the Ethernet frame, so we need to skip the
// Ethernet frame's header to get to it.
// The TCP packet is the payload of the IP packet, so we do a similar thing.
struct ip *ip_packet = (struct ip*)(packet + sizeof(struct ether_header));
struct tcphdr *tcp_packet = (struct tcphdr*)(packet + sizeof(struct ether_header)
+ sizeof(struct ip));
cout << "Source address: " << inet_ntoa(ip_packet->ip_src) << endl;
cout << "Destination address: " << inet_ntoa(ip_packet->ip_dst) << endl;
cout << "Source port: " << ntohs(tcp_packet->source) << endl;
cout << "Destination port: " << ntohs(tcp_packet->dest) << endl;
struct timeval ts = (*packet_header).ts;
cout << "Seconds: " << ts.tv_sec << " Microseconds: " << ts.tv_usec << endl;
}
I suggest reading the pcap.h man page ("man pcap") and possibly man pages for other headers too.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.