Other labs: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12.

Přeložit do češtiny pomocí Google Translate ...

Lab #4 (Mar 9 – Mar 13)

Before class

  • What is asymmetric cryptography? What is a public and a private key?
  • What is SSH?
  • On which port is SSH daemon typically listening?
  • What does git config --global user.name and git config --global user.email set?
  • Get yourself familiar with the following program. It uses argparse (including custom Action class) and csv.DictReader.
  #!/usr/bin/env python3

  import argparse
  import csv
  from pathlib import Path
  import sys


  def parse_filter_description(filter_description):
        """
        Filter has the general form of COLUMN OPERATOR ARGS where COLUMN
        refers to column name as given in CSV and OPERATOR with ARGS denotes
        which condition to evaluate.

        Currently, OPERATOR can be only 'greaterthan' where ARGS is single
        integer to compare value with.
        """

        parts = filter_description.split()
        (column_name, operator, args) = (parts[0], parts[1], parts[2:])
        if operator == 'greaterthan':
            limit = int(args[0])
            return lambda row: int(row[column_name]) > limit
        raise ValueError('Unknown operator')


  class RowFilterAction(argparse.Action):
        def __init__(self, *args, **kwargs):
            argparse.Action.__init__(self, *args, **kwargs)

        def __call__(self, parser, namespace, values, option_string=None):
            try:
                result = parse_filter_description(values)
                setattr(namespace, self.dest, result)
            except Exception as ex:
                parser.error('Invalid filter specification ({})'.format(ex))


  def main(argv):
        args = argparse.ArgumentParser(description='Simple CSV templater')
        args.add_argument('-t', '--template',
                          required=True,
                          dest='template_filename',
                          metavar='PATH',
                          help='Template file (with Python formatting).')
        args.add_argument('-o', '--output',
                          default=None,
                          dest='output_filename',
                          metavar='TEMPLATED_PATH',
                          help='Output filename (with Python formatting).')
        args.add_argument('-F', '--filter',
                          required=False,
                          default=lambda _: True,
                          dest='record_filter',
                          metavar='CONDITION',
                          action=RowFilterAction,
                          help='Filter for each row.')
        config = args.parse_args(argv)

        template = Path(config.template_filename).read_text()
        records = csv.DictReader(sys.stdin)

        for rec in records:
            if not config.record_filter(rec):
                continue
            output = template.format(**rec)
            if config.output_filename:
                with open(config.output_filename.format(**rec), 'w') as out:
                     print(output, file=out)
            else:
                print(output)

  if __name__ == '__main__':
        main(sys.argv[1:])

Topic

  • Remote shell with SSH.
  • Public/private key generation, its use with SSH (and Git/GitLab).
  • Git: basic concepts (fork, clone, commit, patch, pull & push).

Exercises

1.
Ensure you set variable $EDITOR to your favourite TUI editor in your .bashrc. Verify the setting by running $EDITOR ~/.bashrc. Solution.
2.
Use SSH to login to machine u-pl1.ms.mff.cuni.cz. Run w to check who is using it. Solution.
3.
Run hostname -f and hostname -i to see hostname and IP address of your computer. Run the same commands on u-pl1 and u-pl2. What is the difference? Now run the same commands without running interactive shell on the remote machine.
4.
Check that your home is really shared across all the Rotunda machines. Make a file modification on one machine and check via SSH that the change is visible on the other machine too.
5.
Generate a public/private RSA key pair. Do not use a passphrase at the moment. Where and how is the key stored? Hint.
6.
SSH to gitlab.mff.cuni.cz as user git. What is the response?
7.
Upload your SSH key to GitLab. Again, try to SSH to gitlab.mff.cuni.cz as user git. What is the response this time? Hint.
8.
Login to unixadmin.ms.mff.cuni.cz. Try to SSH from this machine to GitLab again. SSH from there to u-pl1.ms.mff.cuni.cz.
9.
Explore possibilities of ~/.ssh/config file. Edit it for more comfortable SSH usage. I.e. be able to write just ssh lab5 and you will connect via ssh to u-pl5.ms.mff.cuni.cz under your login. When you write ssh linux-intro-machine you will connect to unixadmin.ms.mff.cuni.cz under your login name. Solution.
10.
Copy your public key to linux-intro-machine and check that you can login there without entering your password. (Note that public-key authentication does not work for Rotunda machines.) Hint.
11.
Generate a key pair on linux-intro-machine from previous step too and add it to GitLab as well.
12.
Learn where are man pages (or help) for individual Git commands. Solution.
13.
Setup your local Git configuration (user.name and user.email). Hint.Solution.
14.
Fork repository teaching/nswi177/2020-summer/upstream/csv-templater to your own namespace (in GitLab GUI).
15.
Clone your fork of the project to the local machine. Hint.
16.
Investigate, which files were created by the clone. Are there any hidden files?
17.
Fix typos on line 11 in the Python script and in the README and run git status before and after the change. Read carefully the whole output of this command to understand what it reports.
18.
Create a new file, demo/people.csv with at least three columns and 4 rows. Again, check how git status reports this change in your project directory.
19.
Verify that you fixed the typos correctly using git diff.
20.
Prepare for your first commit: add the typo fix in the Python script (we will take care of the second typo later on) to the next commit using git add. How git status differs from the previous state?
21.
Make your first commit via git commit. Use a descriptive commit message! Solution.
22.
Propagate your changes back to GitLab by using git push. Check which changes you can see on GitLab.
23.
Add the second typo fix as a second commit and push it to GitLab too.
24.
Clone your project to unixadmin.ms.mff.cuni.cz. Implement operator equals in a similar fashion to greaterthan. Edit the file remotely and test it remotely too. Use multiple SSH connections to simplify your work. Do not forget to push your changes to GitLab.
25.
Retrieve the changes from GitLab back to your local computer using git pull. Use git log to explore the changes made to the project.
26.
Improve the program behaviour when user enters bad formatting string. Push the change as a new commit.