Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Automatic batch installation and configuration of NTP services for linux servers

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report