Monthly Archives: August 2022
Counting and printing packets using pyshark
#!/usr/bin/env python3 import pyshark packets_array = [] def counter(*args): packets_array.append(args[0]) def count_packets(): cap = pyshark.FileCapture(‘http.cap’, keep_packets=False) cap.apply_on_packets(counter, timeout=10000) return len(packets_array) print(“Packets number:”+str(count_packets())) for packet in packets_array: print(packet) break $python3 count_packets.py Packets number:43 Packet (Length: 62) Layer ETH: Destination: fe:ff:20:00:01:00 Address: fe:ff:20:00:01:00 …. ..1. …. …. …. …. = LG bit: Locally administered address (this is …
Continue reading “Counting and printing packets using pyshark”
Read on packet capturing using pyshark
$python3 read_pcap.py > pcap-result.txt $cat read_pcap.py #!/usr/bin/env python3 import pyshark #cap = pyshark.FileCapture(‘http.cap’, display_filter=”dns”) #for pkt in cap: # print(pkt) cap = pyshark.FileCapture(‘http.cap’, keep_packets=False) def print_info_layer(packet): print(“[Protocol:] “+packet.highest_layer+” [Source IP:] “+packet.ip.src+” [Destination IP:]”+packet.ip.dst) cap.apply_on_packets(print_info_layer) $cat pcap-result.txt [Protocol:] TCP [Source IP:] 145.254.160.237 [Destination IP:]65.208.228.223 [Protocol:] TCP [Source IP:] 65.208.228.223 [Destination IP:]145.254.160.237 [Protocol:] TCP [Source IP:] 145.254.160.237 …
Using streams and file descriptors with fprintf and dprintf respectively
$ls ascii-table.c avg.txt case-changer.c env-var-set.c functions_ver1.c mph-to-kph.c output.c ascii-table.md avg-with-garbage.txt env-var.c exist.sh functions_ver2.c mph-to-kph_v2.c $gcc output.c -o output $./output A regular message on stdout Also a regular message on stdout An error message on stderr A regular message, printed to fd 1 An error message, printed to fd 2 $cat output.c #define _POSIX_C_SOURCE 200809L #include …
Continue reading “Using streams and file descriptors with fprintf and dprintf respectively”
Check if a string has only numerals and do conversion if it is true
$ls ascii-table avg-with-garbage.txt env-var.c functions_ver1 mph-to-kph ascii-table.c case-changer env-var-set functions_ver1.c mph-to-kph.c ascii-table.md case-changer.c env-var-set.c functions_ver2 mph-to-kph_v2.c avg.txt env-var exist.sh functions_ver2.c output.c $./mph-to-kph 564 907.7 $cat mph-to-kph.c #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char mph[10] = { 0 }; while(fgets(mph, sizeof(mph), stdin) != NULL) { /* Check if mph is numeric * (and …
Continue reading “Check if a string has only numerals and do conversion if it is true”
Exiting a program using exit() function
$ls ascii-table avg.txt case-changer.c env-var-set functions_ver1 mph-to-kph.c ascii-table.c avg-with-garbage.txt env-var env-var-set.c functions_ver1.c mph-to-kph_v2.c ascii-table.md case-changer env-var.c exist.sh functions_ver2.c output.c $zile functions_ver2.c $gcc functions_ver2.c -o functions_ver2 $./functions_ver2 Inside main Calling function one Inside function one Calling function two Inside function two Returning with (error) from function two $cat functions_ver2.c #include <stdio.h> #include <stdlib.h> int func1(void); int …
Exiting a program with relevant return value
$ls ascii-table avg.txt case-changer.c env-var-set functions_ver1.c mph-to-kph_v2.c ascii-table.c avg-with-garbage.txt env-var env-var-set.c functions_ver2.c output.c ascii-table.md case-changer env-var.c exist.sh mph-to-kph.c $cat functions_ver1.c #include <stdio.h> int func1(void); int func2(void); int main(int argc, char *argv[]) { printf(“Inside main\n”); printf(“Calling function one\n”); if (func1()) { printf(“Everything ok from function one\n”); printf(“Return with 0 from main – all ok\n”); return 0; …
Continue reading “Exiting a program with relevant return value”