szahn / devops-command-line-guru

Become a DevOps Command Line Guru

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Command Line Basics

"The command line is a simple text interface for interacting with your computer. At first, using a command line interface can be intimidating, but as you get more comfortable with it, you'll find that it becomes a powerful tool. — https://devops-bootcamp.liatr.io/#/1/1.3-unix-basics?id=command-line-navigation

What's the difference between Cmd, Powershell, and Bash?

Concepts

Unix Philsophy

When Unix was first implemented in the late 1960’s and early 1970’s, one of the core tenets was that (wherever possible) everything should be abstracted as a file stream. One of the key goals was to simplify the code required to access devices and peripherals: If all devices presented themselves to the OS as file-systems, then existing code could access those devices more easily. This philosophy runs deep: One can even navigate and interrogate a great deal of a *NIX-based OS & machine configuration by navigating pseudo/virtual file-systems which expose what appear to be “files” and folders, but actually represent machine configuration, and hardware. For example, in Linux, one can explore a machine’s processors’ properties by examining the contents of the /proc/cpuinfo pseudo-file. —https://devblogs.microsoft.com/commandline/windows-command-line-inside-the-windows-console/

Command Lines

Sh, Bash, Zsh

Everything is text. Pipelines. great for writing shell scripts that use command line interface (CLI) utilities, utilizing output from one command to another (piping), and executing simple tasks (up to 100 lines of code)

Usually located in /bin/bash

Cmd

DOS-based.

WSL (Windows/Linux Sybsystem)

Powershell

PowerShell is an object-oriented Shell, unlike the file/stream-based shells typically found in the *NIX world: Rather than handling streams of text, PowerShell processes streams of objects, giving PowerShell script writers the ability to directly access and manipulate objects and their properties, rather than having to write and maintain a lot of script to parse and manipulate text (e.g. via sed/grep/awk/lex/etc.) — https://devblogs.microsoft.com/commandline/windows-command-line-the-evolution-of-the-windows-command-line/

Uses Verb-Noun pattern (Get-Item)

Powershell Core

"A tool that was powerful enough to handle cross-platform administration while allowing advanced customization" –PowerShell Core for Linux Admins

Download: Powershell Core on Github.

Based on .Net Core, cross platform object oriented scripting.

Can be used as default shell in Docker

FROM mcr.microsoft.com/dotnet/core/sdk:3.0
RUN pwsh -c Get-Date
RUN pwsh -c "Get-Module -ListAvailable | Select-Object -Property Name, Path"

Installing PowerShell with one line as a .NET Core global tool

Basics

Function Cmd Bash Powershell
Print echo echo Write-Host
List Files dir ls
Transform string Sed -replace
Print Working Directory dir cwd Get-Location
Download URL `` wget Invoke-WebRequest
Search grep select-string

PowerShell equivalents for common Linux/bash commands

Bash -> PS PowerShell Beginner’s Guide

Essential Tools

Get Help

Command help: man bash

Get list of commands: Get-Command

SSH

Generate an SSH Key

ssh-keygen -f ./id_rsa -t rsa -b 4096 -q
chmod 400 ./id_rsa # Only the current user has read access
ssh -i "demo.pem" ec2-user@ec2-3-85-62-168.compute-1.amazonaws.com

Network Troubleshooting

Show port usage with netstat

Check host resolution with ping bing.com

Display network device addresses with ipconfig or ifconfig

Get hostname with hostname

Trace IP hops with traceroute

tracert http://bing.com
curl -v http://bing.com 

Archives

Compress-Archive -Path ./ compressed.zip
zip -r compressed.zip ./

IO

Check if a file exists: Test-Path

Search files: Select-String –path ./* –pattern white

Package Management

Install net tools with sudo apt install inetutils-traceroute

IP Introspection

curl -s -4 icanhazip.com

Logs

tail -f /var/log/syslog

Deploying Infastructure (IaC)

Ideally, you don't want to write complex scripts in bash. Python is an excellent replacement. If you’ve got more than about 50 lines of bash you should probably be doing python. Has better debugging tools and utilities than Bash, which makes it a great language for developing complex software applications involving many lines of code

Python Scripting

Avoid Python 2.7 as official support ends 2020. Use Python 3+. You can use boto3 python library to replace complex Bash scripts in AWS automation.

./scripts/python.py

Ansible

Ansible is about automating client/server management. Execute scripts on remote servers without having to RDP into them.

Install Ansible

sudo apt-get install ansible sshpass

Copy a file to a Linux host

  • Setup a hosts file
  • Create an ssh key
  • On the remote host, add a user and ssh key
  • Run the playbook
sudo ./hosts
ansible all -i hosts -m ping
ansible-playbook -i hosts -u demo --private-key=./keys/id_rsa ./ansible/copy-file.yml

Resources

About

Become a DevOps Command Line Guru


Languages

Language:Makefile 40.7%Language:PowerShell 38.3%Language:Python 21.0%