Python Forum

Full Version: Python Menu Help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Need help with sorting out my python menu could someone help me please i have added a bin
# 1. Add pytsk3 module
import Tkinter
import pytsk3
#from GUI_Forensic import *
# 33. Add datetime
import datetime
import hashlib

print '______________________________________'
print 'Jamshaid Alis Disk Image Analyzer'
print '______________________________________'

################################################
#menu##############################

# 1.# This is a global variable that will store
# the Img_info object for use by the functions
# in the program.  You will have to define more
# global variables for data shared between functions

disk_image = None

# 2.# Function to display the program's options
def display_menu():
    print 'Program Options Menu'
    print '--------------------'
    print '\n'
    print '1. Open an evidence file'
    print '2. List Partitions in evidence File'
    print '3. Open a partition in evidence file'
    print '4. List files in partition'
    print '5. Display details about file'
    print '6. Exit Program'
    print '\n\n'


# 3.# Function to get an option from the user
def get_options():
    option_num = input ('Please enter the menu options>: ')  
    #this statement returns the number input to 
    #the code that called this function
    return option_num


# 4.# Function to open the evidence file
def open_disk_image():
    #This nextr statementis necessary to access the evidence_file
    #global varible. If you dont do this in your functions
    #then the program will crash if you try to use disk_image
    global disk_image

    # Prompt the user to input the name of the image file
    image_filename = raw_input ('Please enter the name of the image file to process:  ')

    # 4. Display a helpful message
    print '\t'
    print 'Loading image file. Please wait... '
    print 'Loaded %s image file....' % (image_filename)
    print '\t'

    # Create an Img_Info object to represent the image file
    #open the evidence file. Stored in the global disk_image vairble
    #so other functions can access the evidence file
    disk_image = pytsk3.Img_Info(image_filename)

    # 5. Add code here to display the size of the image file and.   
    # calculate and display the total number of sectors in the image file
    print '------------------------------------------------------------------------------------------'
    print 'The image file is %d bytes in size' % (disk_image.get_size())
    print 'The total number of sectors is: %d' % (disk_image.get_size()/512)

    # 6. Create a Volume_Info object to represent the partition table in the image file
    partition_table = pytsk3.Volume_Info(disk_image)
    print 'Number of partitions in partition table is:  %d' % (partition_table.info.part_count)
    print 'Endian in used is:  %s' % (partition_table.info.endian)


    if partition_table.info.is_backup == True:
        print 'Partition table is a backup'
    
    else:
            print 'Partiion is not a backup'

    if partition_table.info.vstype == pytsk3.TSK_VS_TYPE_DOS:
        print "The partion table is MBR"
    elif partition_table.info.vstype == pytsk3.TSK_VS_TYPE_GPT:
        print "The Partition table is GPT"
#print 'Total number of allocated sectors is %d:' % (disk_image.get_size()/512 - 4096)
#print 'Total number of unallocated sectors is %d:' % (disk_image.get_size()/512-disk_image.get_size()/512-4096)
print '\t'


# 5.# Function to list the partitions
def list_partition_table():
    #Access global disk_images going to get the partitions
    #from the evidence file loaded with open_disk_image function
    global disk_image

    #Get the partition table
    # 4. Get the partitions stored in the image
    partition_table = pytsk3.Volume_Info(disk_image)

    # 7. Display partition details in a table
    print '------------------------------------------------------------------------------------------'
    print 'List of partition\n\n'
    print 'Partition number\tDesc\t\t\t\tStart Sector\tNumber of sectors'
    print '------------------------------------------------------------------------------------------'

    # 10. Loop to process the partitions in the partition table
    # 8. A counter variable to count the number of partitions both real and virtual
    # Will display this value in the table

    partition_count = 1
    for partition in partition_table:
        print '{0:<24}{1:<32}{2:<16}{3:<16}' .format(partition_count, partition.desc, partition.start, partition.len)
        
        # 13. Update the partition count
        partition_count += 1
print '\n\n'

# Function to list the files in an open partition
# Not done, you will have to implement this function
def file_system():

    global file_system

    part_num = input('Which partition do you want to display details of>')

    

    # 16. Create a file system object for the partition number input
    file_system = pytsk3.FS_Info(disk_image, partition_offsets[part_num - 1] *512)


    # 17. Display some details about the file system
    print 'File system is %s' % (file_system.info.ftype)
    print 'Block size is %d' % (file_system.info.block_size)

    # 18. Add code below to display more details about the file system
    print 'The total number of blocks is: %d' %(file_system.info.block_count)
    print 'The size of the device is:  %d' %(file_system.info.dev_bsize)
    print 'The Flags: %s' %(file_system.info.flags)
    print 'The Volume serial number is: '
    print 'The root directory recod is:  %s' % (file_system.info.root_inum)

    # 19. Display a table of files from the file system
    print 'File number\tName\t\t\tType'
    print '----------------------------------------------------'

    # 20. Open the root directory
    root_dir = file_system.open_dir(inode = file_system.info.root_inum)

    # 21. Create a counter variable for the files so we can display a number 
    # with the file so it can be selected later
    file_count = 1

    # 22. Loop to display the files
    for file_obj in root_dir:

        # 23. Assume the object is a file
        file_type_str = 'File'

        # 24. Check there is a meta object before determining whether object is a file or directory
        if file_obj.info.meta !=None:
            if file_obj.info.meta.type == pytsk3.TSK_FS_META_TYPE_DIR:
                file_type_str = 'Dir'
        else:
            file_type_str = 'Unknown'
        
    print ''

    # 25. Display formatted data about the file or directory
    print '{0:<16}{1:<64}{2:<8}' .format(file_count, file_obj.info.name.name, file_type_str)
    # 26. Update the count
    file_count += 1








    print "Not done yet"
    

# Function to display the details of a file in the opened file system
# Not done, you will have to implement this function
def display_file_details():
    print "Not done yet"

    
# 6. Main loop function
def run_main():
#
    # 7 .Stores the option input by the user
    selected_option = 0
	
	
    # 8. Define the main loop
    while selected_option != 6:

        # 9. Display the main menu
        display_menu()
		
		
        # 10. Get option number from user
        selected_option = get_options()
		
		
        # 11. Run the function associated with the input number
        if selected_option == 1:
            open_disk_image()

        elif selected_option == 2:
            list_partition_table()

        elif selected_option == 3:
            file_system()

        elif selected_option == 4:
            list_files()

        elif selected_option == 5:
            display_file_details()

        elif selected_option == 6:
            print 'Exiting Program.'
        else:
            print 'Input not recognised'

		

# 12. Start the main loop
if __name__ == '__main__':
    run_main()
Looks like you have a menu. What's the issue?
(Jan-08-2019, 06:19 PM)nilamo Wrote: [ -> ]Looks like you have a menu. What's the issue?
im trying to make my option 3 work


but my option 3 doesnt seem to work


def file_system():

    global file_system

    part_num = input('Which partition do you want to display details of>')

    

    # 16. Create a file system object for the partition number input
    file_system = pytsk3.FS_Info(disk_image, partition_offsets[part_num - 1] *512)


    # 17. Display some details about the file system
    print 'File system is %s' % (file_system.info.ftype)
    print 'Block size is %d' % (file_system.info.block_size)

    # 18. Add code below to display more details about the file system
    print 'The total number of blocks is: %d' %(file_system.info.block_count)
    print 'The size of the device is:  %d' %(file_system.info.dev_bsize)
    print 'The Flags: %s' %(file_system.info.flags)
    print 'The Volume serial number is: '
    print 'The root directory recod is:  %s' % (file_system.info.root_inum)

    # 19. Display a table of files from the file system
    print 'File number\tName\t\t\tType'
    print '----------------------------------------------------'

    # 20. Open the root directory
    root_dir = file_system.open_dir(inode = file_system.info.root_inum)

    # 21. Create a counter variable for the files so we can display a number 
    # with the file so it can be selected later
    file_count = 1

    # 22. Loop to display the files
    for file_obj in root_dir:

        # 23. Assume the object is a file
        file_type_str = 'File'

        # 24. Check there is a meta object before determining whether object is a file or directory
        if file_obj.info.meta !=None:
            if file_obj.info.meta.type == pytsk3.TSK_FS_META_TYPE_DIR:
                file_type_str = 'Dir'
        else:
            file_type_str = 'Unknown'
        
    print ''

    # 25. Display formatted data about the file or directory
    print '{0:<16}{1:<64}{2:<8}' .format(file_count, file_obj.info.name.name, file_type_str)
    # 26. Update the count
    file_count += 1








    print "Not done yet"
thats the code for my option 3 but it doesnt work

the only part in my option 3 that works is

    part_num = input('Which partition do you want to display details of>')
i get an error saying

Undefined variable 'partition_offsets'
In python 2, you should never be using input(). If you want to get something from the user, use raw_input() instead.
(Jan-08-2019, 07:35 PM)nilamo Wrote: [ -> ]In python 2, you should never be using input(). If you want to get something from the user, use raw_input() instead.

i have changed my code to
raw_input
however my program options are still not working
What does "not working" mean? Is there an error? What's the error?
If there's not an error, how is what's happening different from what you expected to happen?
Quote:i get an error saying

Undefined variable 'partition_offsets'
Where in the program do you declare partition_offsets as a variable?