sshpass is a well known tool that everybody loves and hates at the same time, is very handy when you need to create scripts that use ssh and the hosts do not support ssh keys, since you can pass the password to the ssh password prompt, but sshpass is also a security risk because you can see the password invoked on the command line parameters, and even if you use the -f switch that reads the password from a file, the file its self is in clear text, so the password is exposed to all people who have access to this file. …


Image for post
Image for post

What is critical thinking?

Critical thinking is the human capacity to analyze and evaluate information regarding a subject, verify the the veracity of information, ignore external biases and reach to a justified idea by focusing only in facts and rationality.

Why is critical thinking important?

  • Have a better perception of reality which can reduce anxiety and fear.
  • Make better decisions in work and personal life.
  • Be a better person, being a critical thinker requires to set aside your personal beliefs to analyze a problem, this way you can understand what the other is going through.

What do you need to develop critical thinking?

Be curious

Being curious, always ask questions, seek more knowledge, being eager to learn more.


Scenario

you are in ~/scripts and you have several subdirectories like the following image:

Image for post
Image for post

You want for each directory of scripts to execute a command, which in our case the command is “git init”, the long and borring way is to cd each directory and execute the command. If you dont have many directories this borring but still feasible, if the directories where lets say 100, would be a very hard task, of course you can do some scripting but bash has some very nice one liners for such tasks

Solution

$ find . -maxdepth 1 -type d \( ! -name . \) -exec bash -c "cd '{}' && git init"…


Scenario: we have two files in two remote servers and we want to find differences between them without copying files etc, the servers are server1 and server2, the filename is common on both servers and its name is file.txt

Assumptions:

  • I assume that you have SSH connectivity on both server1 and server2
  • I assume that the user you will use to connect has read access on the File system of server1 and server2

How to do it:

$ diff <(ssh user@server1 'cat /usr/local/files/file1.txt') <(ssh user@server2 'cat /usr/local/files/file1.txt')

I hope you found the article useful :)


Scenario: you have just landed to a company which does not have a an automation tool to copy your key to all hosts, to make things worse the hosts are more than 500, what do you do?

Some hints

  • the resolving of the hosts is not done by a DNS, but using /etc/hosts
  • the password is the same for all hosts

Modify /etc/ssh/ssh_config

open with sudo the /etc/ssh/ssh_config file and change “StrictHostKeyChecking yes” to “StrictHostKeyChecking no”

The one-liner

The actual task is done by this one-liner

$ cat /etc/hosts | grep -i '.' | grep -v '#\|:' | awk '{ print $2}' | xargs -I{} sshpass -p password ssh-copy-id username@{} -p…


Scenario: every day we run a report that produce a csv file with some IDs and we want to check the differences that those two files might have, we need to check if there are missing IDs from the previous day CSV vs the current day CSV and vice versa.

The contents of the previous day file: previous.txt

AAA1
BBB1
CCC1
DDD1

The contents of the current day file: current.txt

AAA1
BBB1
DDD1
EEE1
  1. To find if there is an ID in the previous day file that does not exist in the current day file.
$ grep -Fxv -f current.txt previous.txt …


In this scenario we have a simple flask application that we want to dockerize, our application consists of a single file named server.py.

Directory Structure

Our application directory structure is the following

./app/
├── Dockerfile
├── requirements.txt
└── src
└── server.py
1 directory, 3 files

server.py

src/server.py: this directory holds the server.py file, which is our application, its content are:

from flask import Flask
server = Flask(__name__)
@server.route("/")
def hello():
return "Hello World"
if __name__ == "__main__":
server.run(host='0.0.0.0')

Its a simple application, it only returns “hello world” but its great for our demo purposes.

requirements.txt

requirements.txt: the libraries that our application needs, will be added to the image, its content…


What is List Comprehension?

List comprehension is used to create new lists from existing iterables, usually the list comprehension code is an one liner that is more readable than a long function. The generic syntax of a list comprehension is

[ expression context condition ]

Syntax Explanation

  • []: The surrounding brackets mean that the result is a new list
  • context: elements of an object that is iterable.
  • expression: defines how each element of the object to modified before added to the new list
  • condition: its optional, defines which element of the context to be modified

Example: Multiply each element of a list by 2

new_list = [ i * 2 for i in [1,2,3,"b",4,5] ]…


$ sed -n '/pre/,/<\/pre>/p' ./dgm.html


We have the following scenario, we have a file with the following contents

aaa1,bbb1,ccc1,ddd1,aaa2,bbb2,ccc2,ddd2,aaa3,bbb3,ccc3,ddd3

We want to replace each fourth occurrence of comma “,” with a new line character, to do this:

$ sed 's/,/\n/4; P; D' example.txt
aaa1,bbb1,ccc1,ddd1
aaa2,bbb2,ccc2,ddd2
aaa3,bbb3,ccc3,ddd3

I hope you found this helpful :)

About

Konstantinos Patronas

DevOps engineer, loves Linux, Python, cats and Amiga computers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store