⬆️ ⬇️

Now how do I automate



So, are you tired of typing passwords, writing commands, looking at boring wallpapers? Do you want to conquer the universe and rest on your laurels? Make the robots work instead of yourself!





Putting a robot



To train tolerance and loyalty, Yoda's teacher allowed us to log into all 1000 servers of the Imperial Ewok Breeding Committee in weightless conditions on SSH, to hammer in username and password and check who created the file size /tmp/yoda_is_awesome.txt

We write a script that will collect passwords and commands for us. From pre-installed / easy-to-deliver to unix / linux there are: Bash / Shell, Perl, Python.

There are more specialized modules / programs for a task with SSH in Bash / Perl / Python. But tomorrow Yoda’s teacher will ask to do it via Telnet too, then sudo echo “Yoda is superman!”. Consider the more general Expect in conjunction with Bash, the “Expect.pm” module for Perl, the “pexpect” module for Python.

Listing scripts at the end.

We begin to meditate on the running lines:

$ ./sshauto.sh|pl|py 'ls -l /tmp/yoda_is_awesome.txt' 


Output:

 Password: pavlo@10.10.10.10's password: -rw-r--r-- 1 root root 56 Jul 19 2006 /tmp/yoda_is_awesome.txt pavlo@20.20.20.20's password: -rw-r--r-- 1 root root 56 Jul 19 2006 /tmp/yoda_is_awesome.txt pavlo@30.30.30.30's password: -rw-r--r-- 1 root root 56 Jul 19 2006 /tmp/yoda_is_awesome.txt <  > 


Meditation is over. Finish the file “grep” to taste.



We study foreign robots



Public key . Spread out all 1000 SSH Public key servers for your user. This is time consuming, and under other conditions (another user, the need to use sudo, ...) may be useless.

cssh (Cluster SSH) . Opens the console by the number of servers, and one additional window to execute commands on all servers simultaneously. A good solution for 3 servers, but not for 1000.

sshpass . Passing a password for ssh in clear view to STDIN is not supported. But there is for example sshpass: `sshpass -p 'PASSWORD' ssh pavlo@10.10.10.10 'ls -l / tmp / temp_file.txt'` which solves this problem, but will not help if additional sudo or user changes are required.

Microscopes . (CFEngine, Puppet, own version). Provided that it is everywhere. He can also entrust such a task. But this is no longer the intended use, “Hammer nails with a microscope.”

')

Dark side



None of the listed solutions met me working out of the box with the usual installation of the operating system. Each of them requires the installation of at least one package.



Galaxy capture



Here are examples of the implementation / use of "Expect" in Bash, Perl, Python.

Script requirements:

a. ip_addresses.txt is a file with a list of all IP addresses.

b. The password must be entered at the start of the hidden script.



BASH

Installation required: expect

 $ cat ./sshauto.sh #!/bin/bash echo -n "Password:" read -s passw; echo stty echo while read IP do ./sshlogin.exp $passw $IP "$1" 2> /dev/null done < ip_addresses.txt $ cat ./sshlogin.exp #!/usr/bin/expect -f set password [lrange $argv 0 0] set ip_address [lrange $argv 1 1] set command [lindex $argv 2] spawn ssh -q -t -o StrictHostKeyChecking=no pavlo@$ip_address $command expect "*?assword:*" send -- "$password\r" expect eof 




PERL

Requires installation of modules: Expect, Term :: ReadKey

 $ cat ./sshauto.pl #!/usr/bin/perl use Expect; use Term::ReadKey; print "Password:"; ReadMode 'noecho'; $password = ReadLine(0); chomp($password); print "\n"; ReadMode 'normal'; $command = $ARGV[0]; open (IP_list, 'ip_addresses.txt'); foreach $IP (<IP_list>) { chomp($IP); $cli = "/usr/bin/ssh -q -t -o StrictHostKeyChecking=no et0362\@$IP $command"; $exp = new Expect; $exp->raw_pty(1); $exp->spawn($cli) or die "Cannot spawn $cli: $!\n"; $exp->expect(5, [ qr /ssword:*/ => sub { my $exph = shift; $exph->send("$password\n"); exp_continue; }] ); }; close (IP_list); 




Python

Module installation required: pexpect

 $ cat ./sshauto.py #!/usr/bin/python import pexpect import getpass import sys command = sys.argv[1] password = getpass.getpass() IP_list = open('ip_addresses.txt') IP = IP_list.readline() while IP: print IP, cli="ssh pavlo@%s %s" % (IP,command) exp = pexpect.spawn(cli) exp.expect('password:') exp.sendline(password) exp.expect(pexpect.EOF) print exp.before IP = IP_list.readline() IP_list.close() 


Well that's all. Successful to you achievements in the field of automation.



* The article used a picture from here.

Source: https://habr.com/ru/post/131835/



All Articles