Tuesday, June 8, 2021

Solving problem updating Windows 10 to version 20H2 or newer

I have been trying to update Windows 10 to Windows 10 version 20H2. The problems that I  encountered are,

  1. When using Windows Update, I saw the update process stuck at 61%, eventually failed. I am not the only one, clearly. For instance, there are numerous online threads. Here are several examples, example 1, example 2, and example 3. Want to see more, just Google it. 
  2. When using Windows 10 Update Assistant, one of the recommended method, I saw it stuck at 99%, for eons. 

 Finally, I found the right answer, i.e., the method documented in this post,

This  method worked for me. The caveat is that 1) contrast of using Windows update, I have to wait for two to three hours before it finishes; 2) it updates my Windows to 21H1 directly. Regardless, updating to a new version is what I wanted since Microsoft is going to cease supporting older versions of Windows 10.

The steps as outlined in the article linked in the above are,

  1. Open File Explorer, type C:\$GetCurrent, and then press Enter.
  2. Copy and paste the Media folder to the desktop. If you don't see the folder, select View and make sure the check box next to Hidden items is selected.
    Restart your PC, open File Explorer, type C:\$GetCurrent in the address bar, and then press Enter.
  3. Copy and paste the Media folder from the desktop to C:\$GetCurrent.
  4. Open the Media folder and double-click Setup.
  5. Follow the instructions to start the upgrade. On the Get important updates screen, select Not right now, and then select Next.
  6. Follow the instructions to finish upgrading to Windows 10. After you're done, make sure to install available updates. Select the Start  button, and then select Settings  > Update & Security  > Windows Update > Check for updates.



Wednesday, May 5, 2021

Installing Python from source on a Fedora Linux system

I have a need to install a new version of Python on a dated Fedora Linux system (Fedora 31). Here is the steps that I followed,


# install neccessary packages to build Python from source
sudo dnf install gcc openssl-devel bzip2-devel libffi-devel zlib-devel

# download Python source package. You may select a different version you wish 
# to download by browsing https://www.python.org/ftp/python/
wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tgz

# extract the files from the package
tar xzf Python-3.9.5.tgz

# go to the directory where the extracted files are
cd Python-3.9.5

# enable optimization
sudo ./configure --enable-optimizations

# compile and install to /usr/local
sudo make altinstall

# show the version of the active Python
python --version

# add the newly installed Python to alternatives (I already have a Python2 Python3.7)
sudo alternatives --install /usr/bin/python python /usr/local/bin/python3.9 3

# config alternatives and select 3 as the active Python
sudo alternatives --config python

# check the version of the active Python
python --version

Saturday, April 24, 2021

How do we get the "diff" or the patch for the very first commit in Git?

This is a summary of several Stack Overflow discusses about how to get the "diff" or the patch for the very first commit in Git.

  1. To get the diff from the very first commit to the empty tree, we can use the following command.
    git diff \
        4b825dc642cb6eb9a060e54bf8d69288fbee4904 \
        $(git log --reverse | head -1 | cut -d' ' -f2)
      
    where 4b825dc642cb6eb9a060e54bf8d69288fbee4904 is the SHA of the empty tree and $(git log --reverse | head -1 | cut -d' ' -f2) retrieves the SHA of the very first commit. Alternatively, we can rewrite the above in a way that perhaps is easier to read.
  2. To get the diff from the very first commit to the empty tree, we can use the following command.
    git diff \
        $(printf '' | git hash-object -t tree --stdin) \
        $(git log --reverse | head -1 | cut -d' ' -f2)
      
  3. To get the patch from the very first commit to the empty tree, we can use the following command.
    git format-patch \
        --root --stdout \
        $(git log --reverse | head -1 | cut -d' ' -f2)
        

Reference 

  1. How to get Git diff of the first commit?  
  2. How to use git format-patch on initial commit

Monday, April 5, 2021

Accessing a SSH host via a SSH gateway?

I wrote the notes about how to synchronize two SSH hosts via a middle SSH gateway and how to access a SSH host via a SSH gateway. The assumption there is to use ssh in OpenSSH. But what if we want to do the same with PuTTY? I actually locate the answer on Stack Overflow. This post is just a bookmark to the answer.

Simply put, it is to use PuTTY's proxy setup on the user interface when we create a session for a host, and the proxy command follows the following format,


plink.exe %user@%proxyhost -P %proxyport -nc %host:%port

or


plink.exe %user@%proxyhost -pw %pass -P %proxyport -nc %host:%port

Note the two comments.

  1. plink.exe is part of the PuTTY suite.
  2. The second plink.exe example is to use the password authentication. Since the password is part of the command line, someone can reveal the password by showing the process command line arguments, such as, using the methods in the notes written here. So use it with caution.

Sunday, April 4, 2021

Synchronizing files on two SSH hosts with one in the middle (accessing a SSH host via a SSH gateway?)

I believe that this is a rather common situation -- I want to copy files from one SSH host to the other; however, the SSH host I want to copy files from or copy files to is behind a SSH gateway. To describe the situation clearly, let's consider three SSH hosts, H, M, and O. I can log on to H via SSH, from H log on M via SSH, but from H not log on to O via SSH. To get to O, I have to log on to M first, and so M is a SSH gateway. In fact, this is a typical setup for many computational cluster, to access the cluster, you SSH to a SSH gateway. How do we copy files between H and O? Of course, we can always copy files from O to M, and M to H, which is somewhat inconvenient. Can we do better? Yes, via SSH's ProxyCommand option. Below we present two two scenarios.

1. SSH to O via M from H

Below is how we get to O "directly" from H assuming the username on M is alice_on_m and that on O alice_on_o.


ssh -o 'ProxyCommand ssh alice_on_m@M -W %h:%p' alice_on_o@O

Note that although we issue this command on H, H does not have to be able to resolve O; however, M must know O.

If you prefer the public key authentication to the password authentication, do the following instead,


ssh -o \
  'ProxyCommand ssh -i ~/.ssh/private_key_for_m alice_on_m@M -W %h:%p' \
  -i ~/.ssh/pri
  vate_key_for_o alice_on_o@O

where we assume that private_key_for_m and private_key_for_o are two private keys on host H, and their respective public keys are in ~alice_on_m/.ssh/authorized_keys at M and ~alice_on_o/.ssh/authorized_keys at O.

To me,  the most convenient method is to set up ~/.ssh/config file on H, with which, we will save some typing on the command line. Below is an example for the ~/.ssh/config file where we add two entries, one for M, and the other for O.

Host M
    HostName M
    ServerAliveInterval 240
    User alice_on_m
    AddKeysToAgent yes
    IdentityFile ~/.ssh/private_key_for_m

Host O
    HostName O
    ServerAliveInterval 240
    User alice_on_o
    AddKeysToAgent yes
    ProxyCommand ssh alice_on_m@M -W %h:%p
    IdentityFile ~/.ssh/private_key_for_o

Once we set up this, we can get on O from H (via M) by simply issing the following command on H,

ssh alice_on_o@O

2. Copying Files from O to H via M using rsync

Assuming we set up ~/.ssh/config correctly, to copy files from O to H via M using rsync, we simply issue a command like the following,


rsync -avzr alice_on_o@O:./foo/ ./foo/

which copies the directory recursively ~alice_on_o/foo at host O to ./foo at host H. Here we also rely on rsync's ability to use SSH as the transport protocol transparently.

Is this neat?

Friday, March 19, 2021

Displaying Command Line Arguments on Windows without Installing Additional Software?

On Windows, can we obtain from the command line a process's command line arguments without installing any additional software like the Process Explorer of the Windows Sysinternal? As documented by a Stack Overflow discussion, the answer is Yes, via WMIC. Here are two examples,

  1. List all processes and their command line arguments
    
      WMIC path win32_process get Caption,Processid,Commandline
      
  2. List a process, e.g., chrome.exe, and its command line arguments
    
      WMIC path win32_process where "caption='chrome.exe'" get Caption,Processid,Commandline  
      

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()
>>>