$pip install mysql-connector-python
Defaulting to user installation because normal site-packages is not writeable
Collecting mysql-connector-python
Downloading mysql_connector_python-8.0.30-cp310-cp310-manylinux1_x86_64.whl (25.4 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 25.4/25.4 MB 2.3 MB/s eta 0:00:00
Collecting protobuf<=3.20.1,>=3.11.0
Downloading protobuf-3.20.1-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.1/1.1 MB 1.4 MB/s eta 0:00:00
Installing collected packages: protobuf, mysql-connector-python
Successfully installed mysql-connector-python-8.0.30 protobuf-3.20.1
How to install mysql-connector method for python in Debian ?
$pip3 install mysql-connector
Defaulting to user installation because normal site-packages is not writeable
Collecting mysql-connector
Downloading mysql-connector-2.2.9.tar.gz (11.9 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 11.9/11.9 MB 2.0 MB/s eta 0:00:00
Preparing metadata (setup.py) ... done
Building wheels for collected packages: mysql-connector
Building wheel for mysql-connector (setup.py) ... done
Created wheel for mysql-connector: filename=mysql_connector-2.2.9-cp310-cp310-linux_x86_64.whl size=247966 sha256=24d2cc6f0033919bd3bbe1a59ef54a72bc3f302b9c4526115dea2c95e17d4fa7
Stored in directory: /home/jeffrin/.cache/pip/wheels/76/48/9b/da67ff1a18fe8e9d428f9b1a177716d4a7d363d2bbe83bf6cf
Successfully built mysql-connector
Installing collected packages: mysql-connector
Successfully installed mysql-connector-2.2.9
$
Program to POST a form using HTTP with requests
$cat form_post_method.py
#!/usr/bin/env python3
import requests
data_dictionary = {'custname': 'customer','custtel': '323232',
'size': 'large','custemail': 'email@domain.com'}
response = requests.post("http://httpbin.org/post",data=data_dictionary)
# we then print out the http status_code
print("HTTP Status Code: " + str(response.status_code))
if response.status_code == 200:
print(response.text)
$python3 form_post_method.py
HTTP Status Code: 200
{
"args": {},
"data": "",
"files": {},
"form": {
"custemail": "email@domain.com",
"custname": "customer",
"custtel": "323232",
"size": "large"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "72",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.27.1",
"X-Amzn-Trace-Id": "Root=1-62fd14b6-46c6b0b363ae1c7571b8c3f0"
},
"json": null,
"origin": "27.57.31.195",
"url": "http://httpbin.org/post"
}
$
Program to do digest authentication using requests module
$cat digest_authentication.py
#!/usr/bin/env python3
import requests
from requests.auth import HTTPDigestAuth
url = 'http://httpbin.org/digest-auth/auth/user/pass'
response = requests.get(url, auth=HTTPDigestAuth('user', 'pass'))
print('Response.status_code:'+ str(response.status_code))
if response.status_code == 200:
print('Login successful :'+str(response.json()))
$
$python3 digest_authentication.py
Response.status_code:200
Login successful :{'authenticated': True, 'user': 'user'}
Program to do basic authentication using “requests” module in python
$python3 basic_authentication.py
Response.status_code:401
$cat basic_authentication.py
#!/usr/bin/env python3
import requests
from requests.auth import HTTPBasicAuth
requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'password'))
# requests provides a shorthand for this authentication method
response = requests.get('https://api.github.com/user', auth=('user', 'password'))
print('Response.status_code:'+ str(response.status_code))
if response.status_code == 200:
print('Login successful :'+response.text)
$
A tranquil mind gives life to the body,but jealousy rots the bones.
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 NOT the factory default)
.... ...0 .... .... .... .... = IG bit: Individual address (unicast)
Source: 00:00:01:00:00:00
.... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
.... ...0 .... .... .... .... = IG bit: Individual address (unicast)
Type: IPv4 (0x0800)
Address: 00:00:01:00:00:00
Layer IP:
0100 .... = Version: 4
.... 0101 = Header Length: 20 bytes (5)
Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
0000 00.. = Differentiated Services Codepoint: Default (0)
.... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)
Total Length: 48
Identification: 0x0f41 (3905)
Flags: 0x40, Don't fragment
0... .... = Reserved bit: Not set
.1.. .... = Don't fragment: Set
..0. .... = More fragments: Not set
...0 0000 0000 0000 = Fragment Offset: 0
Time to Live: 128
Protocol: TCP (6)
Header Checksum: 0x91eb [validation disabled]
Header checksum status: Unverified
Source Address: 145.254.160.237
Destination Address: 65.208.228.223
Layer TCP:
Source Port: 3372
Destination Port: 80
Stream index: 0
Conversation completeness: Incomplete (0)
TCP Segment Len: 0
Sequence Number: 0 (relative sequence number)
Sequence Number (raw): 951057939
Next Sequence Number: 1 (relative sequence number)
Acknowledgment Number: 0
Acknowledgment number (raw): 0
0111 .... = Header Length: 28 bytes (7)
Flags: 0x002 (SYN)
000. .... .... = Reserved: Not set
...0 .... .... = Nonce: Not set
.... 0... .... = Congestion Window Reduced (CWR): Not set
.... .0.. .... = ECN-Echo: Not set
.... ..0. .... = Urgent: Not set
.... ...0 .... = Acknowledgment: Not set
.... .... 0... = Push: Not set
.... .... .0.. = Reset: Not set
.... .... ..1. = Syn: Set
Expert Info (Chat/Sequence): Connection establish request (SYN): server port 80
Connection establish request (SYN): server port 80
Severity level: Chat
Group: Sequence
.... .... ...0 = Fin: Not set
TCP Flags: ··········S·
Window: 8760
Calculated window size: 8760
Checksum: 0xc30c [unverified]
Checksum Status: Unverified
Urgent Pointer: 0
Options: (8 bytes), Maximum segment size, No-Operation (NOP), No-Operation (NOP), SACK permitted
TCP Option - Maximum segment size: 1460 bytes
Kind: Maximum Segment Size (2)
Length: 4
MSS Value: 1460
TCP Option - No-Operation (NOP)
TCP Option - SACK permitted
Timestamps
Time since first frame in this TCP stream: 0.000000000 seconds
Time since previous frame in this TCP stream: 0.000000000 seconds
Kind: No-Operation (1)
Kind: No-Operation (1)
Kind: SACK Permitted (4)
Length: 2
TCP Option - No-Operation (NOP)
$
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 [Destination IP:]65.208.228.223
[Protocol:] HTTP [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:] 65.208.228.223 [Destination IP:]145.254.160.237
[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 [Destination IP:]65.208.228.223
[Protocol:] TCP [Source IP:] 65.208.228.223 [Destination IP:]145.254.160.237
[Protocol:] TCP [Source IP:] 65.208.228.223 [Destination IP:]145.254.160.237
[Protocol:] TCP [Source IP:] 145.254.160.237 [Destination IP:]65.208.228.223
[Protocol:] DNS [Source IP:] 145.254.160.237 [Destination IP:]145.253.2.203
[Protocol:] TCP [Source IP:] 65.208.228.223 [Destination IP:]145.254.160.237
[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:] DNS [Source IP:] 145.253.2.203 [Destination IP:]145.254.160.237
[Protocol:] HTTP [Source IP:] 145.254.160.237 [Destination IP:]216.239.59.99
[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:] 65.208.228.223 [Destination IP:]145.254.160.237
[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:] 216.239.59.99 [Destination IP:]145.254.160.237
[Protocol:] TCP [Source IP:] 145.254.160.237 [Destination IP:]65.208.228.223
[Protocol:] TCP [Source IP:] 216.239.59.99 [Destination IP:]145.254.160.237
[Protocol:] DATA-TEXT-LINES [Source IP:] 216.239.59.99 [Destination IP:]145.254.160.237
[Protocol:] TCP [Source IP:] 145.254.160.237 [Destination IP:]216.239.59.99
[Protocol:] TCP [Source IP:] 65.208.228.223 [Destination IP:]145.254.160.237
[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:] 65.208.228.223 [Destination IP:]145.254.160.237
[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 [Destination IP:]65.208.228.223
[Protocol:] TCP [Source IP:] 216.239.59.99 [Destination IP:]145.254.160.237
[Protocol:] TCP [Source IP:] 145.254.160.237 [Destination IP:]216.239.59.99
[Protocol:] XML [Source IP:] 65.208.228.223 [Destination IP:]145.254.160.237
[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 [Destination IP:]65.208.228.223
[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
$
$python3 read_pcap.py > pcap-result.txt
$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 [Destination IP:]65.208.228.223
[Protocol:] HTTP [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:] 65.208.228.223 [Destination IP:]145.254.160.237
[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 [Destination IP:]65.208.228.223
[Protocol:] TCP [Source IP:] 65.208.228.223 [Destination IP:]145.254.160.237
[Protocol:] TCP [Source IP:] 65.208.228.223 [Destination IP:]145.254.160.237
[Protocol:] TCP [Source IP:] 145.254.160.237 [Destination IP:]65.208.228.223
[Protocol:] DNS [Source IP:] 145.254.160.237 [Destination IP:]145.253.2.203
[Protocol:] TCP [Source IP:] 65.208.228.223 [Destination IP:]145.254.160.237
[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:] DNS [Source IP:] 145.253.2.203 [Destination IP:]145.254.160.237
[Protocol:] HTTP [Source IP:] 145.254.160.237 [Destination IP:]216.239.59.99
[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:] 65.208.228.223 [Destination IP:]145.254.160.237
[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:] 216.239.59.99 [Destination IP:]145.254.160.237
[Protocol:] TCP [Source IP:] 145.254.160.237 [Destination IP:]65.208.228.223
[Protocol:] TCP [Source IP:] 216.239.59.99 [Destination IP:]145.254.160.237
[Protocol:] DATA-TEXT-LINES [Source IP:] 216.239.59.99 [Destination IP:]145.254.160.237
[Protocol:] TCP [Source IP:] 145.254.160.237 [Destination IP:]216.239.59.99
[Protocol:] TCP [Source IP:] 65.208.228.223 [Destination IP:]145.254.160.237
[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:] 65.208.228.223 [Destination IP:]145.254.160.237
[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 [Destination IP:]65.208.228.223
[Protocol:] TCP [Source IP:] 216.239.59.99 [Destination IP:]145.254.160.237
[Protocol:] TCP [Source IP:] 145.254.160.237 [Destination IP:]216.239.59.99
[Protocol:] XML [Source IP:] 65.208.228.223 [Destination IP:]145.254.160.237
[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 [Destination IP:]65.208.228.223
[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
$
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 <stdio.h>
int main(void)
{
printf("A regular message on stdout\n");
/* Using streams with fprintf() */
fprintf(stdout, "Also a regular message on "
"stdout\n");
fprintf(stderr, "An error message on stderr\n");
/* Using file descriptors with dprintf().
* This requires _POSIX_C_SOURCE 200809L
* (man 3 dprintf)*/
dprintf(1, "A regular message, printed to "
"fd 1\n");
dprintf(2, "An error message, printed to "
"fd 2\n");
return 0;
}
$
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 do conversion) */
if( strspn(mph, "0123456789.-\n") ==
strlen(mph) )
{
printf("%.1f\n", (atof(mph)*1.60934) );
}
/* If mph is NOT numeric, print error
* and return */
else
{
fprintf(stderr, "Found non-numeric"
" value\n");
return 1;
}
}
return 0;
}
$