Sponsored Content
Top Forums Programming [Python] replicating "sha256 -C checksum_file.txt file.txt" Post 303038559 by da1 on Friday 6th of September 2019 05:02:07 AM
Old 09-06-2019
This is what I came up with so far, thank you for the hints:
Code:
workdir='/tmp'
iso_name='install65.iso'
sha_file='SHA256'

    file = open(workdir + '/' + sha_file, 'r')
    for lines in file:
        values = [x.strip('()') for x in lines.split()]
        #print(values)
        if iso_name == values[1]:
            print('file found')
            if computed_sum == values[-1]:
                print('hash OK')
                print(values[-1])
            else:
                print('hash NOT OK')
                print(values[-1])
                exit(1)
        else:
            print('file NOT found')
    file.close()


But I'm having a hard time understanding how I can search for one string (ex. iso name) and if found, use the same line and check for another string (hash sum).

The result of the code above is this;
Code:
38d1f8cadd502f1c27bf05c5abde6cc505dd28f3f34f8a941048ff9a54f9f608
file NOT found
file NOT found
file NOT found
file NOT found
file NOT found
file NOT found
file NOT found
file NOT found
file NOT found
file NOT found
file NOT found
file NOT found
file NOT found
file NOT found
file NOT found
file found
hash OK
38d1f8cadd502f1c27bf05c5abde6cc505dd28f3f34f8a941048ff9a54f9f608
file NOT found
file NOT found
file NOT found
file NOT found
file NOT found
file NOT found
file NOT found

--- Post updated at 12:02 PM ---

I spoke too soon.

I came up with this:
Code:
def check_sha256(block_size=65536):
#    fetch_sha256()
    sha256 = hashlib.sha256()
    with open(workdir + "/" + iso_name, 'rb') as f:
      for block in iter(lambda: f.read(block_size), b''):
        sha256.update(block)
    computed_sum = sha256.hexdigest()
    file = open(workdir + '/' + sha_file, 'r')
    for lines in file:
        values = [x.strip('()') for x in lines.split()]
        if iso_name in values[1]:
            if computed_sum in values[-1]:
                print('SHA256: OK')
                break
            else:
                print('SHA256: NOT OK. Exiting... ')
                exit(1)
    file.close()

It works fine from what I can see but any pointers on improvements would be welcomed!

Thank you very much!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find -name "*.txt" in Korn Shell Script

The following find command works on the Korn Shell command line: find . \( ! -name . -prune \) -type f -name "*.txt" -mtime +100 In the particular directory I'm in, the above find will list correctly the three text files that exist that haven't been modified in over 100 days: ... (3 Replies)
Discussion started by: jwperry
3 Replies

2. Shell Programming and Scripting

Sorting problem "sort -k 16,29 sample.txt > output.txt"

Hi all, Iam trying to sort the contents of the file based on the position of the file. Example: $cat sample.txt 0101020060731 ## Header record 1c1 Berger Awc ANP20070201301 4000.50 1c2 Bose W G ANP20070201609 6000.70 1c2 Andy CK ANP20070201230 28000.00... (3 Replies)
Discussion started by: ganapati
3 Replies

3. UNIX for Dummies Questions & Answers

grep/cat/more -- search in a txt file and display content from a specific "keyword"

Hi, I have a .txt file Sample: ===================== NEXT HOST ===================== AEADBAS001 ip access-list extended BLA_Incoming_Filter ip access-list extended BLA_Outgoing_Filter access-list 1 permit xxxxxxxxxxxxxx access-list 2 permit xxxxxxxxxxxxxx =====================... (4 Replies)
Discussion started by: I-1
4 Replies

4. UNIX for Dummies Questions & Answers

echo "ABC" > file1.txt file2.txt file3.txt

Hi Guru's, I need to create 3 files with the contents "ABC" using single command. Iam using: echo "ABC" > file1.txt file2.txt file3.txt the above command is not working. pls help me... With Regards / Ganapati (4 Replies)
Discussion started by: ganapati
4 Replies

5. Shell Programming and Scripting

ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt"

Hi, I don't know hot to make this command work: ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt" It should return the list of file .txt It's important to search .txt at the end of the line, becouse some file name have "txt" in their name but have other extensions (13 Replies)
Discussion started by: DNAx86
13 Replies

6. Shell Programming and Scripting

"How to get an exact string from a txt file?"

I have many Gaussian output files, which contain a string start from "HF=" but follws the different values. I'm trying to get this exact string from these txt files. example 1, 2.524075,-0.563322,-1.285286\H,0,-2.544438,-0.678834,1.199166\H,0,2.18 ... (7 Replies)
Discussion started by: liuzhencc
7 Replies

7. Shell Programming and Scripting

"Time" command and save result in a file.txt

Hi, I'am using "time" to check execution time of some script. Is there any possibility to save time command result into a file ? (2 Replies)
Discussion started by: Physix
2 Replies

8. Shell Programming and Scripting

get rid of "^" character in a .txt file

I have a file that I ran the dos2unix utility on, it cleans up pretty well, but I noticed an occasional ^M leftover, (actual characters) these may or may not be strays from my original dos file. Either way, I want to get rid of the ^ and any character that may follow. ^M ^C or whatever. How do I... (6 Replies)
Discussion started by: ajp7701
6 Replies

9. Homework & Coursework Questions

how to change this looking for mimetype "text/plain" instead of extension *.txt?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Create a Shell script that looks for all text files in your home directory (including subdirectories). List... (3 Replies)
Discussion started by: rollinator
3 Replies

10. Shell Programming and Scripting

finding the strings beween 2 characters "/" & "/" in .txt file

Hi all. I have a .txt file that I need to sort it My file is like: 1- 88 chain0 MASTER (FF-TE) FFFF 1962510 /TCK T FD2TQHVTT1 /jtagc/jtag_instreg/updateinstr_reg_1 dff1 (TI,SO) 2- ... (10 Replies)
Discussion started by: Behrouzx77
10 Replies
GREPHISTORY(1)						    InterNetNews Documentation						    GREPHISTORY(1)

NAME
grephistory - Query the INN history database SYNOPSIS
grephistory [-eilnqsv] [-f db] [message-id] DESCRIPTION
grephistory queries the INN history database for information about the specified message-ID. If no flags are given, the program prints the storage API token of the corresponding article, or "/dev/null" if the article is listed in the history database but not stored on the server. If the message-ID cannot be found in the database, grephistory will print "grephistory: not found" and exit with a non-zero status. Be sure to escape any special characters in the message ID from the shell. Single quotes are recommended for this purpose since many message-IDs contain dollar signs. OPTIONS
-e Only print the storage token if the article is stored on the system. (In other words, suppress the "/dev/null" or "not found" output for missing or remembered articles.) -f db Query the history database db rather than the default history database. -i Rather than expecting a message-ID on the command line, grephistory will read a list of message-IDs on standard input, one per line. Leading and trailing whitespace is ignored, as are any malformed lines. It will print on standard output those message-IDs which are not found in the history database. This is used when processing "ihave" control messages. -l Display the entire line from the history database, rather than just the storage API token. If the message-ID is present in the history database but has no storage API token, grephistory does not print anything. -n If the message-ID is present in the history database but has no storage API token, print "/dev/null" and exit successfully. This can happen if an article has been cancelled or expired, but history information has still been retained. This is the default behavior. -q Don't print any message, but still exit with the appropriate status. -s Rather than expecting a message-ID on the command line, grephistory will read a list of message-IDs on standard input, one per line. Leading and trailing whitespace is ignored, as are any malformed lines. It will print on standard output the storage API tokens for any articles that are still available, one per line. This flag is used when processing "sendme" control messages. -v Print out the hash of the message-ID for diagnostic purposes, as well as any other requested information. This flag is not useful with -i or -s. EXAMPLES
In case the requested article is not listed in the history database: % grephistory '<unknown.article@news.example.com>' grephistory: not found % grephistory -v '<unknown.article@news.example.com>' grephistory: not found (hash is 501C66C22932BA91131186D7218201EB) In case the requested article is listed in the history database but not stored on the server: % grephistory '<remembered.article@news.example.com>' /dev/null In case the requested article is stored on the server: % grephistory '<87fxeaay1z.fsf@windlord.stanford.edu>' @02014A2DD6231FCC00000000000000000000@ % grephistory -l '<87fxeaay1z.fsf@windlord.stanford.edu>' [B6DDF69376E3CC199246CEC949B3ACAC] 1244517923~-~1244517912 @02014A2DD6231FCC00000000000000000000@ With sm, we can retrieve the article, and get its posting date: % grephistory '<87fxeaay1z.fsf@windlord.stanford.edu>' | sm | grep Date Date: Mon, 08 Jun 2009 20:25:12 -0700 % convdate -dc 1244517912 Tue, 9 Jun 2009 03:25:12 +0000 (UTC) It matches the number recorded in history as for its posting date. HISTORY
Written by Rich $alz <rsalz@uunet.uu.net> for InterNetNews. Rewritten in POD by Russ Allbery <rra@stanford.edu>. $Id: grephistory.pod 8582 2009-08-19 08:04:32Z iulius $ SEE ALSO
history(5), inn.conf(5), sm(1). INN 2.5.2 2009-09-11 GREPHISTORY(1)
All times are GMT -4. The time now is 05:18 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy