GAGLA

      Woggle-Bug: "I say I can see her in my mind's eye"
      "The mind has no eye," declared the Scarecrow. "It's blind."

      L. Frank Baum, Dorothy and the Wizard in Oz, 1908
      http://www.gutenberg.org/ebooks/420

SSH based hosts administration
==============================

If task seems to huge to solve it in one piece, split it apart, then split each part and so on, until peaces seems to be simple enough.

I. Creating VM: XUbuntu-Desktop 16.10 Yakkety Yak x86_64
--------------------------------------------------------

  sudo apt update
  sudo apt upgrade
  apt install openssh-server vim build-essential man-db
  cd /media/virtu/VBOXADDITIONS_5.1.6_110634/
  ./VBoxLinuxAdditions.run
  sudo apt-get remove libreoffice*
  sudo apt-get remove cups*
  sudo apt-get remove blue*
  sudo apt-get remove transmission pidgin
  sudo apt-get autoremove
  dd if=/dev/zero of=biig; rm biig
  adduser hacker
  cat /etc/passwd
  cat /etc/passwd | grep 666
  sudo usermod -u 666 hacker
  cat /etc/passwd | tail -n 5
  hplip:x:120:7:HPLIP system user,,,:/var/run/hplip:/bin/false
  virtu:x:1000:1000:virtu,,,:/home/virtu:/bin/bash
  sshd:x:121:65534::/var/run/sshd:/usr/sbin/nologin
  vboxadd:x:999:1::/var/run/vboxadd:/bin/false
  hacker:x:666:1001:hacker,,,:/tmp:/bin/bash
  vi /etc/sudoers
  ..
  # User privilege specification
  root	ALL=(ALL:ALL) ALL
  hacker	ALL=(ALL) NOPASSWD:ALL
  ..
  sudo adduser hacker
  sudo apt install nmap

II. Tasks solved using GUI
--------------------------

  - Allow English (US), English (GB), Estonian, Russian keyboard layouts.
  - Switch them using Win+Space
  - Show current keyboard layout as flag on xfce4 desktop's menu bar (top-right)
  - Set xfce4-terminal default font to Monospace 16
  - Set xfce4-terminal default color scheme to black  text on white background

Discover and publish how to do it with scripts.  Tip: Use "find" changes made since "time" in "etc" folder.

III. Create "wrapper" script to detach from long tasks immediately
------------------------------------------------------------------

  #!/bin/bash
  $1 &

IV. Clone VM
------------

Use VirtualBox -> Media manager -> Copy disk

V. Scanning subnet segment
--------------------------

NAT. One host up

  root@XUBOX:~# nmap -sP 10.0.2.0/24
  Starting Nmap 7.12 ( https://nmap.org ) at 2016-10-16 21:27 EEST
  Nmap scan report for 10.0.2.2
  Host is up (-0.20s latency).
  MAC Address: 52:54:00:12:35:02 (QEMU virtual NIC)
  Nmap scan report for 10.0.2.3
  Host is up (-0.20s latency).
  MAC Address: 52:54:00:12:35:03 (QEMU virtual NIC)
  Nmap scan report for 10.0.2.4
  Host is up (-0.13s latency).
  MAC Address: 52:54:00:12:35:04 (QEMU virtual NIC)
  Nmap scan report for 10.0.2.15
  Host is up.
  Nmap done: 256 IP addresses (4 hosts up) scanned in 5.48 seconds

NAT Network. 3 Hosts up

  ...

VI. Bash scripting visible hosts
--------------------------------

  # cat segs

  #!/bin/bash
  Seg=(` nmap -sP 10.0.2.0/24 | tr '\n' '$' | sed 's/MAC/\nMAC/g' \
    | grep Oracle | sed 's/.*for //' | sed 's/\$.*$//' `)
  echo ${Seg[@]}

  # ./segs

  10.0.2.6 10.0.2.7 10.0.2.15

VII. Admin in remote host
-------------------------

Opening a X-terminal on a remote host

  $ sshpass -p "virtu" ssh virtu@10.0.2.6 "export DISPLAY=:0; xfce4-terminal"

Echoing to an X-terminal on a remote host

  hacker@BOX2:~$ who
  virtu    tty7         2016-10-16 21:36 (:0)
  virtu    pts/1        2016-10-16 22:20 (:0.0)
  hacker   pts/6        2016-10-16 22:25 (10.0.2.15)

  hacker@BOX2:~$sudo sh -c 'echo BlahBlah > /dev/pts/1'

VIII. Conclusion
----------------

The worst thing is -- that in such scripts you describe the steps that must be done to bring the system in to particular state, rather than describe the state itself. So, if the system was originally not in the same condition that you expected, then everything necessarily goes wrong. Puppet Manifests declaratively describe the desired state of the system, and calculate how to come to it from its current state -- this is the main task of configuration management system [7].

Tips and Links
--------------

1. http://askubuntu.com/questions/2471
   /how-to-hide-users-from-the-gdm-login-screen
  Hacky but you can modify the user's id so they don't show in the list:
  sudo usermod -u 999 <username>

2. http://askubuntu.com/questions/334318
   /sudoers-file-enable-nopasswd-for-user-all-commands
  superuser ALL=(ALL) NOPASSWD:ALL

3. $ sudo apt install putty
   $ pscp -pw password myfile user@host:/path/to

4. http://stackoverflow.com/questions/1346509
   /automate-scp-file-transfer-using-a-shell-script
  why don't you try this?
  password="your password"
  username="username"
  Ip="<IP>"
  sshpass -p "$password" scp /<PATH>/final.txt $username@$Ip:/root/<PATH>

5. http://www.thegeekstuff.com/2010/06/bash-array-tutorial

6. http://askubuntu.com/questions/380550
   /xubuntu-how-to-set-the-wallpaper-using-the-command-line

7. https://habrahabr.ru/post/163811/

8. http://unix.stackexchange.com/questions/30400
   /execute-remote-commands-completely-detaching-from-the-ssh-connection

9. http://rizloff.com/linux
   /ssh-login-without-password-use-authorized-keys
  > А как быть если к одному серверу нужно подключиться с
  разными authorized_keys как прописать в конфиге,,,
  Добавлять каждый ключик новой строчкой в ~/.ssh/authorized_keys

A. http://tldp.org/LDP/abs/html/colorizing.html

B. http://askubuntu.com/questions/496914
   /write-command-in-one-terminal-see-result-on-other-one

C. https://www.liberiangeek.net/2014/08
   /enable-password-less-ssh-logon-ubuntu-servers/

D. http://stackoverflow.com/questions/82256/how-do-i-use-sudo-
   to-redirect-output-to-a-location-i-dont-have-permission-to-wr
