HOME BLOG

SSH Login without password from Windows to Linux

Posted on: September 20th, 2020 by Olu No Comments

Your aim

You want to use Linux and OpenSSH to automate your tasks. Therefore you need an automatic login from Windows host A / user a to Linux Host B / user b. You don’t want to enter any passwords, because you want to call ssh from a within a shell script.

How to do it

First log in on A as user a and generate a pair of authentication keys. Do this using Git Bash. Do not enter a passphrase:

a@A:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/a/.ssh/id_rsa): 
Created directory '/home/a/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/a/.ssh/id_rsa.
Your public key has been saved in /home/a/.ssh/id_rsa.pub.
The key fingerprint is:
3e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 a@A

Now use ssh to create a directory ~/.ssh as user b on B. (The directory may already exist, which is fine):

a@A:~> ssh b@B mkdir -p .ssh
b@B's password: 

Finally append a’s new public key to b@B:.ssh/authorized_keys and enter b’s password one last time:

a@A:~> cat .ssh/id_rsa.pub | ssh b@B 'cat >> .ssh/authorized_keys'
b@B's password: 

From now on you can log into B as b from A as a without password:

a@A:~> ssh b@B

A note from one of our readers: Depending on your version of SSH you might also have to do the following changes:

  • Put the public key in .ssh/authorized_keys2
  • Change the permissions of .ssh to 700
  • Change the permissions of .ssh/authorized_keys2 to 640

 

Source:

 

SSH login without password. http://linuxproblem.org/art_9.html

How to add rsync to Git Bash in Windows 10

Posted on: September 20th, 2020 by Olu No Comments

Download the package: http://www2.futureware.at/~nickoe/msys2-mirror/msys/x86_64/rsync-3.1.2-2-x86_64.pkg.tar.xz

 

Launch Git Bash and change to the download location.

 

Extract the archive using coommand:

 

tar -xf rsync-3.1.2-2-x86_64.pkg.tar.xz

 

You will get a folder usr.

 

Move usr/bin/rsync.exe to C:\Program Files\Git\usr\bin

 

Source:

How to Add rsync to Git Bash for Windows 10. https://gist.github.com/hisplan/ee54e48f17b92c6609ac16f83073dde6

 

How to do mocking in setUp in Python

Posted on: August 29th, 2020 by Olu No Comments

Hi folks,

In this post I will talk about how to mock functions in Python across all methods of your test class.

To do this, all you have to do is perform your mocking in the setUp method of your test class.

A way to do this is to create a patch object using code like:

mock_item_patch = mock.patch('some.module.path.item')

 

Next, call start() on the patch object to create your mock using code like

mock_item = mock_item_patch.start()

 

Once you do this, you can set return_value or side_effect as needed. E.g.

mock_item.foo.return_value = 'bar'

 

Don’t forget to add a call to stop the patch on clean-up using code

self.addCleanup(mock_item_patch.stop)

 

Below is code showing all these in action.

 

from unittest import mock, TestCase


class YourTestClass(TestCase):
    def setUp(self):
        mock_item_patch = mock.patch('some.module.path.item')
        mock_item = mock_item_patch.start()
        mock_item.foo.return_value = 'bar'
        self.addCleanup(mock_item_patch.stop)


    def test_something(self):
        ...

 

That’s all for now. Happy software development.

How to dump stored MySQL routines

Posted on: June 12th, 2020 by Olu No Comments

Hi folks,

In this post I will quickly share how to make a MySQL dump of stored routines (procedures and functions). Recently, I was looking to back up databases for a MySQL server which I maintain. One of the databases contained a stored function. So, I thought, heh, let’s just use the good old mysqldump the way I do it with tables. Well, it didn’t work. After a bit of Googling and searching MySQL documentation, I eventually found the answer. All you have to do is add the routines flag when invoking mysqldump.

E.g.

 

mysqldump --routines -u youruser -p yourdb > yourdb.sql

 

That’s all for now.

How to perform acions when a Python program finishes whether successfully or not

Posted on: April 25th, 2020 by Olu No Comments

Hi folks,

In this post I talk about a good way to perform actions when your Python program completes. For example you may want to clean up temporary files created by program or close database connections or other resources resources when your program terminates. You may want to do this regardless of whether the program finishes successfully or crashes due to an error. An excellent way to perform such clean-up tasks automatically is to use the atexit module. Here is an example below of how to use it in a class below.

 

import atexit


class Foo:

    def __init__(self):
        atexit.register(self.goodbye)
        x = 5

    def goodbye(self):
        print('You are leaving the program')



f = Foo()

 

If you run the following code, you will see the message ‘You are leaving the program’ just before the program exits.

Here is another example where we intentionally cause the program to crash.

import atexit


class Bar:

    def __init__(self):
        atexit.register(self.goodbye)
        x = 5 / 0 # error!

    def goodbye(self):
        print('You are leaving the program')



f = Bar()

 

If you run this program, you will see the error, but nevertheless still see the message ‘You are leaving the program’ printed before the program exits.

You can read more about atexit in the Python documentation.

That’s it for now. Happy software development.

How to run system commands in Python and get the output

Posted on: April 20th, 2020 by Olu No Comments

Hi folks,

Here is an easy way to run system commands in Python and get the output. It is by using subprocess.getoutput command. E.g.

import subprocess
output = subprocess.getoutput("ls -l ~")
print(output)

 

That’s all for now. Till next time, happy software development.

How to fix ‘Connection Refused’ error when connecting to Apache virtual host from other client ip addresses

Posted on: April 17th, 2020 by Olu No Comments

Hi folks,

In this post I talk about how to fix ‘Connection Refused’ error you may experience when trying to connect to your web application which has been set up as a virtual host on Apache server. The issue I discuss here is where you are able to access the virtual host from your local machine where the app is hosted, but not from any other machine.

It turns out that the solution to this problem lies in the server name used for the virtual host.

If you use a server name that ends in ‘.localhost’, other clients will not be able to connect to your application. So, if you have such a server name, rename it to something that does not end in ‘.localhost’.

That’s all for now. Till next time, happy software development.

How to fix ‘image not found’ error related to ‘Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib’ when you import psycopg2 on MacOS

Posted on: April 3rd, 2020 by Olu No Comments

Hi folks,

In this post we discuss how to fix an error you may come across when you run import

psycopg2. You may come across this error if you have recently updated openssl to a version greater than 1.0.0 on Mac OS.

The error looks like

 

  File "/Users/someuser/.virtualenvs/top_games/lib/python3.7/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 737, in dbapi
    import psycopg2
  File "/Users/someuser/.virtualenvs/top_games/lib/python3.7/site-packages/psycopg2/__init__.py", line 50, in 
    from psycopg2._psycopg import (                     # noqa
ImportError: dlopen(/Users/someuser/.virtualenvs/top_games/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so, 2): Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib
  Referenced from: /Users/someuser/.virtualenvs/top_games/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so
  Reason: image not found

 

Here is how to fix it:

Activate your project’s virtual environment.

Update psycopg2 using the command:

 

pip install psycopg2 --upgrade

 

If you come across an error like

 

ld: library not found for -lssl

 

then run the following two commands in your terminal

 

export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

 

Then, try the aforementioned pip command once more.

That’s it. Once psycopg2 is upgraded, the import issue should be fixed.

How to exit ipdb

Posted on: February 15th, 2020 by Olu No Comments

Hi folks,

If you have done a lot of debugging with ipdb on Python projects in a windows environment using tools like Git bash, you may notice that it’s not very easy to exit ipdb. Pressing familiar keys like Ctrl + c doesn’t work. Here’s how to exit ipdb in Windows:

Run the following command

 

import os; os._exit(0)

 

That’s all for now. Till next time.

How to pip install Python projects from source

Posted on: February 15th, 2020 by Olu No Comments

Hi folks,

In this post I talk about how to pip install Python projects from source.

Let’s say you have a project that you can normally install via pip using a command like pip install someproject, but you want to make some customization to the project.

Here’s how to go about it.

  • First, clone the project from Github.
  • Make any modifications to the project source code as desired.
  • Then change to the project’s root directory.
  • Run the following command to install the project
pip install -e . 
  • If you have the project’s library already installed and you upgrade that version to the latest from your source code using command
pip install -e . -U

 

That’s all for now. Happy software development.