Category: Uncategorized

  • 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 […]

  • 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 […]

  • 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 […]

  • 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; […]

  • Setting value to an environment variable

    $ls ascii-table avg.txt case-changer.c env-var-set.c functions_ver2.c output.c ascii-table.c avg-with-garbage.txt env-var exist.sh mph-to-kph.c ascii-table.md case-changer env-var.c functions_ver1.c mph-to-kph_v2.c $cat env-var-set.c #define _POSIX_C_SOURCE 200112L #include <stdio.h> #include <stdlib.h> int main(void) { setenv(“FULLNAME”, “Jack-Benny”, 1); printf(“Your full name is %s\n”, getenv(“FULLNAME”)); return 0; } $gcc env-var-set.c -o env-var-set $ $./env-var-set Your full name is Jack-Benny $

  • Fetching values of environment variables

    $ls ascii-table avg.txt case-changer.c exist.sh mph-to-kph.c ascii-table.c avg-with-garbage.txt env-var.c functions_ver1.c mph-to-kph_v2.c ascii-table.md case-changer env-var-set.c functions_ver2.c output.c $cat env-var.c #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { /* Using getenv() to fetch env. variables */ printf(“Your username is %s\n”, getenv(“USER”)); printf(“Your home directory is %s\n”, getenv(“HOME”)); printf(“Your preferred editor is %s\n”, getenv(“EDITOR”)); printf(“Your shell is […]

  • Program for changing case

    $ls ascii-table avg.txt env-var.c functions_ver1.c mph-to-kph_v2.c ascii-table.c avg-with-garbage.txt env-var-set.c functions_ver2.c output.c ascii-table.md case-changer.c exist.sh mph-to-kph.c $gcc case-changer.c -o case-changer $./case-changer g G v V FF ff $cat case-changer.c #include <stdio.h> #include <string.h> int main(void) { char c[20] = { 0 }; char newcase[20] = { 0 }; int i; while(fgets(c, sizeof(c), stdin) != NULL) { […]

  • The ascii table program

    $ls ascii-table.c avg-with-garbage.txt env-var-set.c functions_ver2.c output.c ascii-table.md case-changer.c exist.sh mph-to-kph.c avg.txt env-var.c functions_ver1.c mph-to-kph_v2.c $cat ascii-table.c #include <stdio.h> int main(void) { char c; for (c = 65; c<=90; c++) { printf(“%c = %d “, c, c); /* upper case */ printf(“%c = %d\n”, c+32, c+32); /* lower case */ } return 0; } $gcc ascii-table.c […]