Monday, February 15, 2021

Inpsecting Python Objects

It is useful to inspect objects when we run Python interactively. Below is a few commands we can use to inspect Python objects.

  • type
  • dir
  • vars
  • __dict__
Below are a few examples showing how we may use these:

$ python
>>> # This is to show the version of Pyhon I am using.
>>> import sys
>>> print(sys.version)
3.7.9 (default, Aug 19 2020, 17:05:11)
[GCC 9.3.1 20200408 (Red Hat 9.3.1-2)]
>>> 
>>> # create a tempfile.TemporaryDirectory object
>>> import tempfile
>>> d = tempfile.TemporaryDirectory()
>>>
>>> # now inspect the d object
>>> # 1. what is d's data type?
>>> type(d)
<class 'tempfile.TemporaryDirectory'>
>>> # 2. what are d's attributes?
>>> dir(d)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', 
 '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__gt__', 
 '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', 
 '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', 
 '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 
 '__weakref__', '_cleanup', '_finalizer', 'cleanup', 'name']
>>>
>>> # 3. Return the __dict__ attribute for d
>>> vars(d)
{'name': '/tmp/tmpmjddeo49', '_finalizer': }
>>> 
>>> # this is the same
>>> d.__dict__
{'name': '/tmp/tmpmjddeo49', '_finalizer': }
>>> # so d has an "attribute" called 'name', and it is a string!
>>> type(d.name)
<class 'str'>
>>> d.name
'/tmp/tmpmjddeo49'
>>> # d also has an "attribute" called 'cleanup', and it is a method!
>>> type(d.cleanup)
<class 'method'>
>>> d.cleanup()
>>>

Tuesday, January 12, 2021

Computing File Hash on Windows

Two "build-in" tools we can use to compute file hash values on Windows are Windows PowerShell and CertUtil.

In Windows PowerShell, we can compute a hash value for a file using Get-FileHash command.

C:\> PowerShell
PS C:\> help Get-FileHash

NAME
Get-FileHash

SYNTAX
Get-FileHash [-Path] <string[]> [-Algorithm {SHA1 | SHA256 | SHA384 | SHA512 | MACTripleDES | MD5 | RIPEMD160}]
[<CommonParameters>]

Get-FileHash -LiteralPath <string[]> [-Algorithm {SHA1 | SHA256 | SHA384 | SHA512 | MACTripleDES | MD5 |
RIPEMD160}] [<CommonParameters>]

Get-FileHash -InputStream <Stream> [-Algorithm {SHA1 | SHA256 | SHA384 | SHA512 | MACTripleDES | MD5 | RIPEMD160}]
[<CommonParameters>]


ALIASES
None


REMARKS
Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.
-- To download and install Help files for the module that includes this cmdlet, use Update-Help.
-- To view the Help topic for this cmdlet online, type: "Get-Help Get-FileHash -Online" or
go to https://go.microsoft.com/fwlink/?LinkId=517145.

Windows also comes with a tool called CertUtil that provides an option to compute file hash value as well.


C:\>CertUtil -hashfile -?
Usage:
  CertUtil [Options] -hashfile InFile [HashAlgorithm]
  Generate and display cryptographic hash over a file

Options:
  -Unicode          -- Write redirected output in Unicode
  -gmt              -- Display times as GMT
  -seconds          -- Display times with seconds and milliseconds
  -v                -- Verbose operation
  -privatekey       -- Display password and private key data
  -pin PIN                  -- Smart Card PIN
  -sid WELL_KNOWN_SID_TYPE  -- Numeric SID
            22 -- Local System
            23 -- Local Service
            24 -- Network Service

Hash algorithms: MD2 MD4 MD5 SHA1 SHA256 SHA384 SHA512

CertUtil -?              -- Display a verb list (command list)
CertUtil -hashfile -?    -- Display help text for the "hashfile" verb
CertUtil -v -?           -- Display all help text for all verbs
  
Below are examples.

C:\> CertUtil -hashfile C:\Windows\System32\drivers\etc\hosts sha512
SHA512 hash of C:\Windows\System32\drivers\etc\hosts:
59119e66f5945029f8652c5981589d95cace534adc6780ccea736b7e776615caa0b567c14d161271d6066f57d9bab0d4055850162f5a046c0456264b7b9e7508
CertUtil: -hashfile command completed successfully.

C:\> PowerShell
PS C:\> Get-FileHash -Path C:\Windows\System32\drivers\etc\hosts -Algorithm sha512 | Format-List


Algorithm : SHA512
Hash      : 59119E66F5945029F8652C5981589D95CACE534ADC6780CCEA736B7E776615CAA0B567C14D161271D6066F57D9BAB0D4055850162F5
            A046C0456264B7B9E7508
Path      : C:\Windows\System32\drivers\etc\hosts

Wednesday, December 2, 2020

Free SSL Certificate Authorities (CAs)

There are a few public key CAs that issue free SSL certificates via the Automated Certificate Management Environment protocol (ACME). So, this serves as a bookmark for these CAs.

More detailed discussion about these three are in this blog post. This post and the discussion of the post have some useful information about these three, such as, support of wildcard certificates and ECC certifications.

Friday, November 20, 2020

Where are my Python packages?

Sometimes when I use a Python package, I run into some problems. I want to take a look at the source code of the package. But where are the packages?

There are a couple of methods. First, we can view where Python is looking for packages. For globally installed packages, we can locate the paths as follows,


$ python
Python 3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> for p in sys.path:
...   print(p)
...

The global site packages are at,


python
Python 3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import site
>>> for p in site.getsitepackages():
...   print(p)
...

If we have pip installed and know the package name, we can use a command like


$ pip show numpy

What if we want to know the path to the locally installed pacakges (user-installed packages)?


$ python
Python 3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import site
>>> site.getusersitepackages()
...

Monday, October 26, 2020

Updating Plex Media Server Plugin from FreeNAS 11.2-RELEASE to 11.3-RELEASE

 Plex failed to update when I upgrade FreeNAS from 11.2-RELEASE to 11.3-RELEASE. After some Web searches, I found the solution given in this post worked. For convenience, I copied the solution to this post in the following,

  1. Stop the plex jail
  2. Log on to a FreeNAS shell, and renamed the jail
    
        iocage rename plex plexmediaserver
        
  3. From the FreeNAS shell, go into /mnt/__YOUR_STORAGE__/iocage/jails/plexmediaserver/ and renam plex.json to plexmediaserver.json,
    
        mv plex.json plexmediaserver.json
        
  4. Open config.json in the same directory and change the plugin name from plex to plexmediaserver
  5. Start the jail again. Everything still worked and both jail and plugin now displayed as plexmediaserver
  6. Run the following command to upgrade the jail and the plex plugin
    
        iocage upgrade plexmediaserver -r 11.3-RELEASE
        

Monday, October 5, 2020

Which ISP's routers support hairpinning (NAT loopback)?

NAT loopback, also known as hairpinning is that two hosts on the LAN behind the NAT gateway can communicate with each other via the NAT gateway's external address. Which ISP's router out of the box supports NAT loopback? Based on experience thus far in October, 2020, my experience is as follows,

  • Optimum. Yes
  • Verizon Fios. Yes
  • Spectrum. No