Shell scripts to bulk operate Linux hosts:

Source: Internet
Author: User

Shell scripts to bulk operate Linux hosts:

I. Introduction to the Environment:

Turn on 192.168.100.150-152 (ctos1-3) and 192.168.100.100 (VSFTPD).

1.SSH Login: Operate on 192.168.100.100

1) Password login 192.168.100.150:

SSH [email protected] # #输入root密码123123

2) Configure SSH key pair login 192.168.100.150:

Ssh-keygen # #创建密钥对, prompt to enter directly

Ssh-copy-id-i/root/.ssh/id_rsa.pub [email protected] # #输入root密码上传公钥

SSH [email protected] # #登录查看是否需要密码

SSH [email protected] "ifconfig eth0" # #在192.168.100.150 Execute Command "ifconfig eth0" View IP Address

3) Delete the relevant information and prepare for the SSH script test:

192.168.100.100 on: rm-rf/root/.ssh/known_hosts

192.168.100.150 on: rm-rf/root/.ssh


Two. Scripting Bulk operations hostname:

1. Requirements Description:

1) Modify host name:

Modify the hostname of 192.168.100.150 to "www.linuxfan.cn"

Modify the hostname of 192.168.100.151 to "ca.linuxfan.cn"

Modify the hostname of 192.168.100.152 to "db.linuxfan.cn"

2) Copy the Security optimization script "security.sh" to each server and execute it.

3) security.sh Requirements: Configure Yum and Yum update system, delete unnecessary users, turn off unnecessary services, set firewall default rules, optimize SSH configuration, create Admin user, Initial password 123123 and set the next admin login must modify the password and limit only allow the user to use the SU command, through the TCP wrapper set only 192.168.100.100 and other logins.


2. Scripting:

1) Write the Security optimization script:

VI security.sh

#!/bin/bash

Completion of relevant knowledge after completion of learning

Useradd Admin

echo 123123 |passwd--stdin Admin

: Wq

2) Write the batch operation script:

[email protected] bin]# cat ssh-changename.sh

#!/bin/bash

#by linuxfan.cn 2016-9-24


# #set Variable

Export pre= "192.168.100."

Export pw= "123123"

Export hnf= "/etc/sysconfig/network"


#create and security SSH pair key for SSH connect.

For i in {150,151,152};d o

/usr/bin/expect <<eof

Spawn Ssh-copy-id [email protected] $PRE $i

Expect {

"(yes/no)?" {send "yes\r"; Exp_continue}

"Password:" {send "$PW \ r"}

}

Interact

Expect EOF

Eof


Export cmd= "ssh [email protected] $PRE $i"

# #change hostname.

Ping-c 2 $PRE $i &>/dev/null

Setval=$?

If [$i-eq] && [$SETVAL-eq 0];then

$CMD "Sed-i ' s/^host.*/hostname=www.linuxfan.cn/g ' $HNF"

# #create test file and make dir.

$CMD "Touch/tmp/public-key-test.txt;mkdir-p/root/bin" &>/dev/null

# #copy security.sh to host and exec it.

scp/root/bin/security.sh [email protected] $PRE $i:/root/bin &>/dev/null

$CMD "source/root/bin/security.sh" &>/dev/null


elif [$i-eq 151] && [$SETVAL-eq 0];then

$CMD "Sed-i ' s/^host.*/hostname=ca.linuxfan.cn/g ' $HNF"

# #create test file and make dir.

$CMD "Touch/tmp/public-key-test.txt;mkdir-p/root/bin" &>/dev/null

# #copy security.sh to host and exec it.

scp/root/bin/security.sh [email protected] $PRE $i:/root/bin &>/dev/null

$CMD "source/root/bin/security.sh" &>/dev/null


elif [$i-eq] && [$SETVAL-eq 0];then

$CMD "Sed-i ' s/^host.*/hostname=mysql.linuxfan.cn/g ' $HNF"

# #create test file and make dir.

$CMD "Touch/tmp/public-key-test.txt;mkdir-p/root/bin" &>/dev/null

# #copy security.sh to host and exec it.

scp/root/bin/security.sh [email protected] $PRE $i:/root/bin &>/dev/null

$CMD "source/root/bin/security.sh" &>/dev/null


Else

echo "$PRE $i is off, please check and try again."

Exit 1

Fi

Done

[Email protected] bin]#

3) Test:

View on three hosts, respectively:

ID Admin

Cat/etc/sysconfig/network


This article is from the "12289734" blog, please be sure to keep this source http://12299734.blog.51cto.com/12289734/1908244

Shell scripts to bulk operate Linux hosts:

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.