Get the App
SLTechnology News&Howtos  ›  Servers  › 

Automatic batch installation and configuration of NTP services for linux servers

Shulou Source: shulou.com Published: 2022-06-02 06:04:52 09月10日 Update

Recently, two new clock devices have been installed on the project, and more than a hundred linux servers in the network need to be configured with NTP services to synchronize with the clock devices. In order to avoid the trouble of configuring each device one by one, a python script is written. The script is divided into three files, the first is the python script, the second is the host list, which contains two entries of IP address and password, and the third is the command file, which contains the operation statements to be executed from the yum installation of the ntp package to the configuration of the ntp.conf parameter file to the start of the NTP service. Execute the script, call these two files, and all the servers in the network NTP installation and configuration of the service is completed. In fact, this script can do a variety of batch operations, just need to change the command file, the script does not have to make any changes.

Cat cmd_list.txt

# configure NTP file

Mv / etc/ntp.conf / etc/ntp.conf.bak

Echo 'server 192.168.5.200' > > / etc/ntp.conf

Echo 'Fudge 192.168.5.200 stratum 10' > > / etc/ntp.conf

# install NTP package and start NTP service

Rm / etc/yum.repos.d/*.repo

Echo'[ol7_u5_base]'> > / etc/yum.repos.d/local.repo

Echo 'name=Oracle Linux' > > / etc/yum.repos.d/local.repo

Echo 'baseurl= http://192.168.5.250/redhat7' > > / etc/yum.repos.d/local.repo

Echo 'gpgkey= http://192.168.5.250/redhat7/RPM-GPG-KEY-redhat-release' > > / etc/yum.repos.d/local.repo

Echo 'gpgcheck=1' > > / etc/yum.repos.d/local.repo

Echo 'enabled=1' > > / etc/yum.repos.d/local.repo

Yum install-y ntp*

Systemctl start ntpd

Systemctl enable ntpd

Cat host.txt

192.168.5.1 pass1

192.168.5.2 pass2

...

192.168.5.254 pass254

Cat auto_ntp.py

Import paramiko

Import time

Import sys

Import socket

Host = open (sys.argv [1])

Host_list = []

Username= "root"

For hosta in host.readlines ():

Host_list.append (hosta.strip () .split ())

F = open ('log.txt', 'a')

Cmd = open (sys.argv [2])

For hostname,password in host_list:

Ssh_client = paramiko.SSHClient ()

Ssh_client.set_missing_host_key_policy (paramiko.AutoAddPolicy ())

Try:

Ssh_client.connect (hostname=hostname, username=username, password=password)

Print ("Successfull connected to", hostname)

Cmd.seek (0)

Stdin, stdout, stderr = ssh_client.exec_command ('hostname')

F.write (hostname +''+ stdout.read (). Decode ('utf-8') + "\ n")

For ccc in cmd.readlines ():

C = ccc.strip ()

Stdin, stdout, stderr = ssh_client.exec_command (c)

F.write (stdout.read () .decode ('utf-8') + "\ n")

F.write (stderr.read () .decode ('utf-8') + "\ n")

Except paramiko.ssh_exception.AuthenticationException:

Print ("User authentication failed for" + username)

Except socket.error:

Print (hostname + "is not reachable.")

Cmd.close ()

F.close ()

Ssh_client.close ()

Execute:

Python auto_ntp.py host.txt cmd_list.txt

You can do any other operation in batch, as long as you modify the operation statements in the cmd_list.txt file.

Tags: Files services scripts configurations devices servers commands clocks networks statements software packages three two hosts facts in fact parameters addresses multiple Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Xiaomi Shulou Information Apple Shulou Tech Info