Pesquisar este blog

domingo, 16 de maio de 2021

sshpass: Login no SSH Server inserindo a senha via Shell Script

 sshpass is a utility designed for running ssh using the mode referred to as “keyboard-interactive” password authentication, but in non-interactive mode.

ssh uses direct TTY access to make sure that the password is indeed issued by an interactive keyboard user. Sshpass runs ssh in a dedicated tty, fooling it into thinking it is getting the password from an interactive user.

The command to run is specified after sshpass’ own options. Typically it will be “ssh” with arguments, but it can just as well be any other command. The password prompt used by ssh is, however, currently hardcoded into sshpass.

WARNING! These examples considered the least secure as simple ps command can expose password to all users on the same host. I highly recommend using ssh’s public key authentication or keychain software to set up secure passwordless SSH access.

Install sshpass under Debian / Ubuntu Linux

Type the following command:
$ sudo apt-get install sshpass
Sample outputs:

Fig.01: Installing sshpass on Debian/Ubuntu Linux

Fig.01: Installing sshpass on Debian/Ubuntu Linux

Install sshpass under RHEL/CentOS Linux

First, enable EPEL repo and type the following yum command:
$ sudo yum install sshpass
If you are using Fedora Linux, type:
$ sudo dnf install sshpass

Install sshpass under Arch Linux

$ sudo pacman -S sshpass

Install sshpass under OpenSUSE Linux

$ sudo zypper install sshpass

Install sshpass under FreeBSD Unix

To install the port, enter:
# cd /usr/ports/security/sshpass/ && make install clean
To add the package, run:
# pkg install sshpass

How do I use sshpass in Linux or Unix?

Login to ssh server called server.example.com with password called t@uyM59bQ:
$ sshpass -p 't@uyM59bQ' ssh username@server.example.com
For shell script you may need to disable host key checking:
$ sshpass -p 't@uyM59bQ' ssh -o StrictHostKeyChecking=no username@server.example.com

Security unwise warning: The -p option should be considered the least secure of all of sshpass’s options. I recommend that you use ssh’s public key authentication.

A bash shell script example with SSHPASS

The syntax is:

SSHPASS='t@uyM59bQ' sshpass -e ssh vivek@server42.cyberciti.biz
SSHPASS='t@uyM59bQ' sshpass -e ssh vivek@server42.cyberciti.biz date
SSHPASS='t@uyM59bQ' sshpass -e ssh vivek@server42.cyberciti.biz w
SSHPASS='t@uyM59bQ' sshpass -e ssh -o StrictHostKeyChecking=no vivek@server42.cyberciti.biz

The password is passed as environment variable called SSHPASS.

Reading password from file

Another option is to read password from file using the -f option. The syntax is:
sshpass -f fileNameHere ssh user@server
Create a file as follows:

$ echo 'myPassword' > myfile
$ chmod 0400 myfile
$ sshpass -f myfile ssh vivek@server42.cyberciti.biz

How do I backup /var/www/html using rsync?

Run rsync over SSH using password authentication, passing the password on the command line:
$ rsync --rsh="sshpass -p myPassword ssh -l username" server.example.com:/var/www/html/ /backup/
OR
$ SSHPASS='yourPasswordHere' rsync --rsh="sshpass -e ssh -l username" server.example.com:/var/www/html/ /backup/

How do I use sshpass with gpg encrypted file?

First, create a file as follows:
$ echo 'mySshPasswordHere' > .sshpassword
Now, encrypt a file using gpg command:
$ gpg -c .sshpassword
$ rm .sshpassword

Finally, use it as follows:
$ gpg -d -q .sshpassword.gpg > fifo; sshpass -f fifo ssh vivek@server1.cyberciti.biz

If you just type sshpass, you will see help screen as follows:

Fig.02: sshpass command in action

Referencia - https://www.cyberciti.biz/faq/noninteractive-shell-script-ssh-password-provider/

Retirado em: 16/05/2021

sábado, 15 de maio de 2021

Comandos Shell Bash - Legais

Watch 

Com o Watch do Linux, podemos acompanhar a mudanças nos resultados de tempos em tempos. Ele é útil para refletir em tempo-real os eventos que acontecem em um sistema operacional. Por padrão, ele executa o programa a cada dois segundos, porém, utilizar as opções –n ou –interval permite especificar um período diferente. O watch também permite que o usuários monitorem a criação e exclusão de arquivos, se utilizado com vários comandos pipeline. O watch é executado simultaneamente no terminal até ser interrompido pressionando CTRL+C.

O Watch do Linux também é útil quando você precisa identificar um problema ou monitorar o resultado de um script continuamente.

O comando Watch vem instalado por padrão em todas as distribuições Linux com uma grande variedade de ferramentas para seus usuários.

 watch arquivo

Ou, se queremos ver a data atual, podemos usar este comando:

watch date

Desta maneira, a data muda a cada dois segundos, seguidos pela data atual e horário do sistema.

Muitas vezes queremos que o comando Watch seja encerrado após alguma mudança no resultado. Podemos fazer isso com a ajuda da opção -g, que interrompe o comando Watch sempre que houver alterações no uso da memória. O comando fica assim:

watch –g free

Também é possível desabilitar o cabeçalho, mostrando a linha vazia, hora atual, comando e intervalo. Para isso é utilizada a opção -t. Por exemplo:

watch –t date

Para observar estatísticas de largura de banda, você pode usar a opção ifconfig com o comando Watch Linux:

watch ifconfig

Agora você consegue ver o tráfego de bytes RX e TX, juntamente com o endereço de IP do seu sistema. Ele também mostra a velocidade da rede a cada dois segundos.

Como Destacar Diferenças entre Atualizações com Watch

O Watch regularmente atualiza o resultado de um comando específico após 2 segundos. Para visualizar o resultado em alteração podemos usar as opções –d ou –difference. Esta opção irá destacar as mudanças. O comando fica da seguinte maneira:

watch –d date

Aqui, as mudanças do tempo atual são destacadas no terminal.

Como Mudar Intervalos de Tempo no Comando Watch

A duração entre resultados pode facilmente mudar no terminal Linux com a opção -n seguida de um dígito que representa o número de segundos que você deseja.

Por exemplo:

watch –n 5 date

Este comando irá refletir o resultado atualizado a cada 5 segundos.

Quando usamos o Linux não podemos observar intervalos em menos que 0.1 segundos.

Como Usar o Comando Watch Linux com Outras Pipelines

O Watch também pode ser útil com outros comandos de pipelines:

Como Monitorar Arquivos com Comando Watch

Para ver o status de um arquivo específico ou mudanças que foram feitas, use o seguinte comando no terminal:

watch -d 'ls -l | fgrep devisers'

Neste caso, devisers é o usuário e ls –l são os arquivos presentes neste diretório.

Como Usar o Comando Watch como Relógio

Para usar o watch como relógio, executamos o comando abaixo:

watch –t date

Como Visualizar o Status do Hardware com o Watch Linux

Neste exemplo, vamos monitorar a temperatura da CPU de uma máquina Linux:

watch -n 1 sensors

Ele irá mostrar o resultado do comando e você poderá ver o status da temperatura do seu computador!

 

 Nome e a versão da distribuição, versão do kernel

· cat /etc/*-release. Ou então:

· cat /etc/*-release | grep PRETTY. ...

· lsb_release -a. ...

· uname -a. ...

· uname -mrs. ...

· uname --help. ...

· cat /proc/version.

 

 

Referencia:https://www.hostinger.com.br/tutoriais/comando-watch-linux

 

 

 

Repetir o mesmo comando várias vezes no shell - Shell Script

Autor: nayamonia

Retirado de : https://arquivogabrielfernandes.wordpress.com/2011/06/17/repetir-o-mesmo-comando-varias-vezes-no-shell/
Em: 15/05/2021

Por vezes queremos acompanhar a cópia de um arquivo na console do Linux e o caminho mais normal é abrir um outro terminal e ficar repetitivamente executando o comando ls, ou algum outro comando, haja dedo para apertar a seta pra cima e enter, seta pra cima e enter, seta pra cima e enter, coisa bem chatinha esta, sem contar o fato de encher o histórico de comandos com linhas repetidas, dificultando encontrar um comando feito anteriormente.

Podemos resolver isto usando o comando while de forma bem simples, por exemplo se quisermos executar um ls por várias vezes, podemos fazer assim:

while true; do ls; done;

Isto vai executar o comando ls até pressionarmos CTRL + C para quebrá-lo, entretanto o comando desta forma, não terá muita utilidade, pois vai mostrar o resultado do ls e em seguida fazer outro ls e assim sucessivamente, logo o resultado será ilegível para humanos normais, digamos assim.

Para resolver podemos dar uma pequena pausa entre cada iteração do while valendo-se do comando sleep. No exemplo que segue, vamos executar o comando ls, aguardar 10 segundos (sleep 10) e executar novamente o ls e depois a pausa novamente e assim por diante.

while true; do ls; sleep 10; done;

Agora já temos um resultado mais interessante e podemos apenas observar o que esta acontecendo, economizando bastante tecladas !!!

Por fim, para deixar o coisa mais bonita, podemos usar o comando clear para limpar a tela antes de executar novamente o ls, ai nosso resultado será bem legível para humanos normais. 

Veja como ficou nosso comando agora:

while true; do ls; sleep 10; clear; done;

quinta-feira, 13 de maio de 2021

Bash For Loop Examples - Loops no Linux


How do I use bash for loop to repeat certain task under Linux / UNIX operating system? How do I set infinite loops using for statement? How do I use three-parameter for loop control expression?

A ‘for loop’ is a bash programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement i.e. it is the repetition of a process within a bash script. For example, you can run UNIX command or task 5 times or read and process list of files using a for loop. A for loop can be used at a shell prompt or within a shell script itself.

for loop syntax

Numeric ranges for syntax is as follows:

for VARIABLE in 1 2 3 4 5 .. N
do
	command1
	command2
	commandN
done

OR

for VARIABLE in file1 file2 file3
do
	command1 on $VARIABLE
	command2
	commandN
done

OR

for OUTPUT in $(Linux-Or-Unix-Command-Here)
do
	command1 on $OUTPUT
	command2 on $OUTPUT
	commandN
done

Examples


This type of for loop is characterized by counting. The range is specified by a beginning (#1) and ending number (#5). The for loop executes a sequence of commands for each member in a list of items. A representative example in BASH is as follows to display welcome message 5 times with for loop:

#!/bin/bash
for i in 1 2 3 4 5
do
   echo "Welcome $i times"
done

Sometimes you may need to set a step value (allowing one to count by two’s or to count backwards for instance). Latest bash version 3.0+ has inbuilt support for setting up ranges:

#!/bin/bash
for i in {1..5}
do
   echo "Welcome $i times"
done

Bash v4.0+ has inbuilt support for setting up a step value using {START..END..INCREMENT} syntax:

#!/bin/bash
echo "Bash version ${BASH_VERSION}..."
for i in {0..10..2}
  do 
     echo "Welcome $i times"
 done

Sample outputs:

Bash version 4.0.33(0)-release...
Welcome 0 times
Welcome 2 times
Welcome 4 times
Welcome 6 times
Welcome 8 times
Welcome 10 times

The seq command to create standard bash for Loop (outdated method)

WARNING! The seq command print a sequence of numbers and it is here due to historical reasons. The following examples is only recommend for older bash version. All users (bash v3.x+) are recommended to use the above syntax.

The seq command can be used as follows. A representative example in seq is as follows:

#!/bin/bash
for i in $(seq 1 2 20)
do
   echo "Welcome $i times"
done

There is no good reason to use an external command such as seq to count and increment numbers in the for loop, hence it is recommend that you avoid using seq. The builtin command are fast.

Three-expression bash for loops syntax

This type of for loop share a common heritage with the C programming language. It is characterized by a three-parameter loop control expression; consisting of an initializer (EXP1), a loop-test or condition (EXP2), and a counting expression/step (EXP3).

for (( EXP1; EXP2; EXP3 ))
do
	command1
	command2
	command3
done
## The C-style Bash for loop ##
for (( initializer; condition; step ))
do
  shell_COMMANDS
done

A representative three-expression example in bash as follows:

#!/bin/bash
for (( c=1; c<=5; c++ ))
do  
   echo "Welcome $c times"
done

Sample output:

Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times

How do I use for as infinite loops?

Infinite for loop can be created with empty expressions, such as:

#!/bin/bash
for (( ; ; ))
do
   echo "infinite loops [ hit CTRL+C to stop]"
done

Conditional exit with break

You can do early exit with break statement inside the for loop. You can exit from within a FOR, WHILE or UNTIL loop using break. General break statement inside the for loop:

for I in 1 2 3 4 5
do
  statements1      #Executed for all values of ''I'', up to a disaster-condition if any.
  statements2
  if (disaster-condition)
  then
	break       	   #Abandon the loop.
  fi
  statements3              #While good and, no disaster-condition.
done

Following shell script will go though all files stored in /etc directory. The for loop will be abandon when /etc/resolv.conf file found.

#!/bin/bash
for file in /etc/*
do
	if [ "${file}" == "/etc/resolv.conf" ]
	then
		countNameservers=$(grep -c nameserver /etc/resolv.conf)
		echo "Total  ${countNameservers} nameservers defined in ${file}"
		break
	fi
done

Early continuation with continue statement

To resume the next iteration of the enclosing FOR, WHILE or UNTIL loop use continue statement.

for I in 1 2 3 4 5
do
  statements1      #Executed for all values of ''I'', up to a disaster-condition if any.
  statements2
  if (condition)
  then
	continue   #Go to next iteration of I in the loop and skip statements3
  fi
  statements3
done

This script make backup of all file names specified on command line. If .bak file exists, it will skip the cp command.

#!/bin/bash
FILES="$@"
for f in $FILES
do
        # if .bak backup file exists, read next file
	if [ -f ${f}.bak ]
	then
		echo "Skiping $f file..."
		continue  # read next file and skip the cp command
	fi
        # we are here means no backup file exists, just use cp command to copy file
	/bin/cp $f $f.bak
done

For loop with array elements

In this example, we use the for loop to iterate over an array of elements defined as follows:

DB_AWS_ZONE=('us-east-2a' 'us-west-1a' 'eu-central-1a')
 
for zone in "${DB_AWS_ZONE[@]}"
do
  echo "Creating rds (DB) server in $zone, please wait ..."
  aws rds create-db-instance \
  --availability-zone "$zone"
  --allocated-storage 20 --db-instance-class db.m1.small \
  --db-instance-identifier test-instance \
  --engine mariadb \
  --master-username my_user_name \
  --master-user-password my_password_here
done

Loop with a shell variable

Sometimes we store important data in the shell variable, and we can use for a loop as follows to read the data:

_admin_ip="202.54.1.33|MUM_VPN_GATEWAY 23.1.2.3|DEL_VPN_GATEWAY 13.1.2.3|SG_VPN_GATEWAY"
for e in $_admin_ip
do
   ufw allow from "${e%%|*}" to any port 22 proto tcp comment 'Open SSH port for ${e##*|}'
done

Loop with a number

We can specify a range in loops as follows:

for i in {START..END}
do
   commands
done
## step value ##
for i in {START..END..STEP}
do
   commands
done
## example: ping cbz01, cbz02, cbz03, and cbz04 using a loop ##
for i in 0{1..4}
do
    h="cbz${i}"
    ping -c 1 -q "$h" &>/dev/null 
    if [ $? -eq 0 ]
    then
        echo "server $h alive" 
    else
        echo "server $h dead or can not ping."
    fi
done

Loop with strings

Say we have a variable named PKGS, and we need to loop through a list of strings to install those packages:

PKGS="php7-openssl-7.3.19-r0  php7-common-7.3.19-r0  php7-fpm-7.3.19-r0  php7-opcache-7.3.19-r0 php7-7.3.19-r0"
for p in $PKGS
do
   echo "Installing $p package"
   sudo apk add "$p"
done

Command substitution

Command substitution means run a shell command and store its output to a variable. For example:

up=$(uptime)
echo "Server uptime is $up"

The for Loop argument list also workes command substitution as follows:

for var in $(command)
do
  print "$var"
done
## example ##
for f in $(ls /nas/*.pdf)
do
  print "File $f"
done

Command-line arguments

command line argument is nothing but an argument sent to a program being called. A program can take any number of command line arguments. For example, we are going to use the grep command to search for user names in the /etc/passwd file:
$ grep 'vivek' /etc/passwd
grep is the name of an actual command and shell executed this command when you type command at shell prompt. The first word on the command line is:

  • grep – name of the command to be executed.
  • Everything else on command line is taken as arguments to this command.

The for Loop argument list also accpents Command-line arguments/paramenters as follows:

## $@ expands to the positional parameters, starting from one.  ##
for i in $@
do
    echo "Script arg is $i"
done

You run it as follows:
./script one foo bar

Putting all together

Bash for loop is useful for automating repetitive tasks in IT. Let us see how to run a simple command (such as uptime) on multiple Linux or Unix servers:

for s in server1 server2 server3
do
    ssh vivek@${s} "uptime"
done

OR combine the echo command along with command substitution as follows:

for s in server1 server2 server3
do
    echo "Server ${s}: $(ssh vivek@${s} uptime)"
done

Sample outputs:

Server server1:  09:34:46 up 12 days, 21:57,  0 users,  load average: 0.08, 0.09, 0.09
Server server2:  09:34:50 up 17 days,  2:30,  0 users,  load average: 0.03, 0.03, 0.00
Server server3:  09:34:53 up 17 days,  2:31,  0 users,  load average: 0.04, 0.04, 0.00

In this standard bash for loop example we are going to update all CentOS/RHEL based servers using the yum command or apt command/apt-get command in case we have a Debian/Ubuntu based servers:

## CENTOS/RHEL example (for fedora replace yum with dnf) ##
for s in server0{1..8}
do
    echo "*** Patching and updating ${s} ***"
    ssh root@${s} -- "yum -y update"
done

Here is simple but useful shell script example:

#!/usr/bin/env bash
# Purpose: Update all my Linode servers powered by Debian/Ubuntu Linux
# Author: Vivek Gite under GPL v2.x+
# ----------------------------------------
log="/tmp/apt-get.log"
>"${log}"
for s in ln.cbz0{1..5}
do 
   echo "Updating and patching $s, please wait..." | tee -a "${log}"
   ssh root@${s} -- apt-get -q -y update >/dev/null
   ssh root@${s} -- DEBIAN_FRONTEND=noninteractive apt-get -y -q upgrade >>"${log}"
done
echo "Check $log file for details."

See why we used DEBIAN_FRONTEND apt-get variable to avoid any prompts duding updates. It would be best if you set up ssh keys for automation purposes or running scripts from Linux/Unix cron jobs.

Finding ping time for multiple IP addresses

Here is my sample code:

#!/bin/bash
ips="$(host -t a www.cyberciti.biz | awk '{ print $4}')"
for i in $ips; do ping -q -c 4 "$i"; done

This will produce average stats as follows, indicating that multiple load balancer ICMP ECHO_REQUEST average time was 19-20 milliseconds

PING 104.22.10.214 (104.22.10.214) 56(84) bytes of data.

--- 104.22.10.214 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3006ms
rtt min/avg/max/mdev = 20.612/21.255/22.054/0.624 ms
PING 172.67.7.239 (172.67.7.239) 56(84) bytes of data.

--- 172.67.7.239 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 19.199/20.279/21.433/0.862 ms
PING 104.22.11.214 (104.22.11.214) 56(84) bytes of data.

--- 104.22.11.214 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 20.232/20.710/21.500/0.479 ms

Check out related media

This tutorial is also available in a quick video format. The video shows some additional and practical examples such as converting all flac music files to mp3 format, all avi files to mp4 video format, unzipping multiple zip files or tar balls, gathering uptime information from multiple Linux/Unix servers, detecting remote web-server using domain names and much more.

Conclusion

You learned how to use the bash for loop with various example. For loops can save time and help you with automation for tiny tasks. However, for complicated IT automation tasks, you should use tools like Ansible, Salt, Chef, pssh and others. See the following resources for more info.

  • See all sample for loop shell script in our bash shell directory
  • Bash for loop syntax and usage page from the Linux shell scripting wiki
  • man bash
  • $ help for
  • # help {
  • $ help break
  • $ help continue

  

Retirado de: https://www.cyberciti.biz/faq/bash-for-loop/#Examples