Jun-07-2024, 07:12 AM
Is this anything like what you want?
I plugged in an old usb stick. It is mounted at /dev/sda
Also need to run as sudo, because normal user has no access to /dev/sda
Run this in bash:
Gives this:
I plugged in an old usb stick. It is mounted at /dev/sda
#! /usr/bin/python3 """Read a single sector of a physical disk. Tested on Mac OS 10.13.3 and Windows 8.""" import os def main(usb): # Read the first sector of the first disk as example. """Demo usage of function.""" if os.name == "nt": # Windows based OS normally uses '\\.\physicaldriveX' for disk drive identification. print(read_sector(r"\\.\physicaldrive0")) else: # Linux based OS normally uses '/dev/diskX' for disk drive identification. print(read_sector(usb)) #usb = '/media/pedro/UraltUSB/' usb = '/dev/sda' def read_sector(disk, sector_no=0): """Read a single sector of the specified disk. Keyword arguments: disk -- the physical ID of the disk to read. sector_no -- the sector number to read (default: 0). """ # Static typed variable read = None # File operations with `with` syntax. To reduce file handeling efforts. with open(disk, 'rb') as fp: fp.seek(sector_no * 512) read = fp.read(512) return read if __name__ == "__main__": usb = '/dev/sda' main(usb)Make sure the Python script read_sectors is executable.
Also need to run as sudo, because normal user has no access to /dev/sda
Run this in bash:
Quote:pedro@pedro-HP:~/myPython$ sudo ./read_sectors.py
Gives this:
Output:b'3\xc0\xfa\x8e\xd8\x8e\xd0\xbc\x00|\x89\xe6\x06W\x8e\xc0\xfb\xfc\xbf\x00\x06\xb9\x00\x01\xf3\xa5\xea\x1f\x06\x00\x00R\x89\xe5\x83\xec\x1cj\x1e\xc7F\xfa\x00\x02R\xb4A\xbb\xaaU1\xc90\xf6\xf9\xcd\x13Z\xb4\x08r\x17\x81\xfbU\xaau\x11\xd1\xe9s\rf\xc7\x06Y\x07\xb4B\xeb\x13\xb4H\x89\xe6\xcd\x13\x83\xe1?Q\x0f\xb6\xc6@\xf7\xe1RPf1\xc0f\x99@\xe8\xdc\x00\x8bNV\x8bFZPQ\xf7\xe1\xf7v\xfa\x91Af\x8bFNf\x8bVRS\xe8\xc4\x00\xe2\xfb1\xf6_YXf\x8b\x15f\x0bU\x04f\x0bU\x08f\x0bU\x0ct\x0c\xf6E0\x04t\x06!\xf6u\x19\x89\xfe\x01\xc7\xe2\xdf!\xf6u.\xe8\xe1\x00Missing OS\r\n\xe8\xd2\x00Multiple active partitions\r\n\x91\xbf\xbe\x07Wf1\xc0\xb0\x80f\xab\xb0\xedf\xabf\x8bD f\x8bT$\xe8@\x00f\x8bD(f\x8bT,f+D f\x1bT$\xe8p\x00\xe8*\x00f\x0f\xb7\xc1f\xab\xf3\xa4^f\x8bD4f\x8bT8\xe8"\x00\x81>\xfe}U\xaau\x85\x89\xecZ_\x07f\xb8!GPT\xfa\xff\xe4f!\xd2t\x04f\x83\xc8\xfff\xab\xc3\xbb\x00|f`fRfP\x06Sj\x01j\x10\x89\xe6f\xf7v\xdc\xc0\xe4\x06\x88\xe1\x88\xc5\x92\xf6v\xe0\x88\xc6\x08\xe1A\xb8\x01\x02\x8aV\x00\xcd\x13\x8dd\x10far\x0c\x02~\xfbf\x83\xc0\x01f\x83\xd2\x00\xc3\xe8\x0c\x00Disk error\r\n^\xac\xb4\x0e\x8a>b\x04\xb3\x07\xcd\x10<\nu\xf1\xcd\x18\xf4\xeb\xfd\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xee\xff\xff\xff\x01\x00\x00\x00\xff\xbf\xd4\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00U\xaa'