Shell script for Loop

Source: Internet
Author: User
Tags get ip stdin ssh server

Shell Loop: For

The number of cycles is fixed

=====================

Shell:

For variable name [in value list]

Do

Loop body

Done


C Language:

for (initial value; condition; step))

Do

Loop body

Done

=====================

Shell loop: While until


The number of cycles is not necessarily fixed

can be fixed

Can not be fixed


While statement:

While condition test

Do

Loop body

Done

Function: When the condition test is established (the condition test is true), the loop body is executed.

=====================

Until statement:

Until condition test

Do

Loop body

Done

Function: When the condition test is established (the condition test is false), the loop body is executed.


=====================

Case 1: The value of the cyclic output variable i. The following example can be tested directly at the command line.

For i in a B c D

Do

Echo $i

Done

=====================

Case 2: Batch ping test host, concurrency mode.

VI pi.sh script code is as follows

#!/usr/bin/env Bash

Trap "echo ok;exit 3" INT

For i in {1..254}

Do

(

ip=192.168.100. $i

PING-W.2-I.1-C1 $ip | Grep-q ' TTL '

[ $? -eq 0] && echo "$ip is up" | | echo "$ip is down"

) &

Done

Wait

echo "Finish ...."


Test efficiency: Time SH pi.sh

=====================


Case 1: Write script s1.sh. Prompts the user to enter a feature option and executes the associated command after carriage return.

VI s1.sh script code is as follows

#!/bin/bash

#trap "Echo ok;exit;" HUP INT QUIT TSTP

While:

Do

Cat <<-eof

1.web1

2.web2

3.mysql1

Eof

Read-p "Input number[1-3]:" num

Case $num in

1)

#ssh [email protected]

Ping-c 4 127.0.0.1

;;

2)

#ssh [email protected]

Ping-c 4 127.0.0.2

;;

3)

#ssh [email protected]

Ping-c 4 127.0.0.3

;;

‘‘)

True

;;

*)

echo "Error"

Break

;;

Esac

Done


----------------------------------------

Case 3: Multi-host push public key.

VI skey.sh

#!/usr/bin/env Bash

#get IP

>/tmp/ip_yes.txt

>/tmp/ip_no.txt


if [!-F ~/.ssh/id_rsa];then

Ssh-keygen-t rsa-p ""-F ~/.ssh/id_rsa

Fi


Rpm-q expect &>/dev/null

If [$?-ne 0];then

Yum Install-y expect

Fi


For i in {1..254}

Do

{

ip=192.168.100. $i

PING-W.2-I.1-C1 $ip &>/dev/null

If [$? -eq 0];then

echo "$ip is up" | Tee-a/tmp/ip_yes.txt

/usr/bin/expect <<-eof

Set Timeout 10

Spawn Ssh-copy-id $ip

Expect {

"Yes/no" {send "yes\r"; Exp_continue}

"Password:" {send "012\r"}

}

Expect EOF

Eof

Else

echo "$ip is down" | Tee-a/tmp/ip_no.txt

Fi

}&

Done

Wait

echo "Finish ...."


=====================

Case 4:{}& Concurrently batch modify the Sshd_config configuration file on the SSH server, the SELinux profile, and turn off the firewall.

1th Step: Prepare the/opt/ip.txt file.

Cat >/opt/ip.txt <<eof

192.168.10.2

192.168.10.25

192.168.10.26

Eof


2nd step: Create a script.

VI ssh.sh

#!/usr/bin/env Bash

For IP in $ (cat/opt/ip.txt)

Do

{

SSH $ip "Sed-ri '/^ #UseDNS/c\usedns no '/etc/ssh/sshd_config"

SSH $ip "Sed-ri '/^gssapiauthentication yes/c\gssapiauthentication no '/etc/ssh/sshd_config"

SSH $ip "service iptables stop;chkconfig iptables off"

SSH $ip "Sed-ri '/^selinux/c\selinux=permissive '/etc/selinux/config"

SSH $ip "Setenforce 0"

}&

Done

Wait

echo "Finish ..."


=====================

Case 4: Multi-host Change password.

VI modify_passwd.sh

#!/usr/bin/env Bash

#change Password

#v1.0 by Flyer 08/10/2017


Read-p "Please input a New Password:" Pass


For i in {1..100}

Do

{

ip=192.168.100. $i

PING-C1-W1 $ip &>/dev/null

If [$?-eq 0];then

SSH $ip "echo $pass | passwd--stdin Root "&>/dev/null

If [$? -eq 0];then

echo "$ (date +%f) $ip up" >>/tmp/ok.txt

Else

echo "$ (date +%f) $ip down" >>/tmp/fail.txt

Fi

Else

echo "$ (date +%f) $ip down" >>/tmp/fail.txt

Fi

}&

Done

Wait

echo "All OK ..."

=====================

Case: for bulk user creation.

1th Step: Prepare the user list file.

Cat >/tmp/user.txt <<eof

Tom

Jack

Lucy

Eof


2nd step: Create a script file.

VI useradd.sh

#!/usr/bin/env Bash

#useradd2

pass=111

Red_col= "\e[1;31m"

Reset_col= "\e[0m"


If [$#-eq 0];then

echo "Usage: $ (basename $) file"

Exit 1

Fi


if [!-F $];then

echo "Error file!"

Exit 2

Fi


For user in $ (cat $)

Do

ID $user &>/dev/null

If [$? -eq 0];then

echo "$user already Exites"

Else

Useradd $user

echo "$pass" | passwd--stdin $user &>/dev/null

If [$?-eq 0];then

Echo-e "${red_col}${user}${reset_col} create"

Fi

Fi

Done


3rd Step: Test the syntax and test the execution of the script.

Sh-n useradd.sh

chmod +x useradd.sh

./useradd.sh/tmp/user.txt


===============================================

Case: Create a user in bulk.

VI useradd3.sh

#!/bin/bash

While:

Do

Read-p "Please enter name & password & num & Mode[add/del]:" Name pass num mode

printf "User information:

-----------------------------

User name: $name

User passwd: $pass

User number: $num

User mode: $mode

-----------------------------

"


Read-p "Is you sure?" [y/n]: "Action

If ["$action" = "Y"];then

Break

Fi


Done


Case $mode in

Add

For I in $ (seq-w $num)

Do

User=${name}${i}

Useradd $user

echo "$pass" | passwd--stdin $user &>/dev/null

If [$?-eq 0];then

echo "$user is created"

Fi

Done

;;

Del

For I in $ (seq-w $num)

Do

User=${name}${i}

Userdel-r $user &>/dev/null

If [$?-eq 0];then

echo "$user is deleted"

Fi

Done

;;

*)

echo "Program Exit"

Exit 0

;;

Esac


-----------------------------

Case: Write a basic script for a batch ping with a for C style.

VI pi2.sh

#!/bin/bash

For ((i=1;i<=10;i++))

Do

{

ip=192.168.100. $i

Ping-c 1-w1 $ip |grep ttl

}&

Done

Wait

echo "Finish ..."


=============================

Case: While automatically switching gateways.

VI gw.sh

#!/bin/bash

gw1=192.168.100.3

gw2=192.168.100.2

While:

Do

IP r del Default

IP r add via $GW 1

While PING-C1 $GW 1 &>/dev/null

Do

Sleep 1

Done

IP r del Default

IP r add default via $GW 2

Until PING-C1 $GW 1 &>/dev/null

Do

Sleep 1

Done

Done &






This article is from the "Network Technology World" blog, please be sure to keep this source http://1364952.blog.51cto.com/1354952/1955276

Shell script for Loop

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.