How does the network server realize the NTP clock service in batches automatically?

How does the network server realize the NTP clock service in batches automatically?
How does the network server realize the NTP clock service in batches automatically?

Recently, two new clock devices have been added to the project. More than one hundred linux servers in the network need to be configured with NTP service to achieve time synchronization with the clock device. In order to avoid the trouble of configuring each device one by one, a python script was written.

The script is divided into 3 files, the first is a python script, the second is a host list, which contains two items of IP address and password, and the third is a command file, including the installation of ntp software package from yum to the configuration of ntp.conf parameter file To start the NTP service operation statement.

Execute the script, call these two files, and all servers in the network will be installed and configured to complete the service. In fact, this script can do all kinds of batch operations, only need to change the command file, the script does not need 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 the NTP software package and start the 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()

carried out:

python auto_ntp.py host.txt cmd_list.txt

Do any other operations in batches, as long as you modify the operation statements in the cmd_list.txt file

Guess you like

Origin blog.csdn.net/weixin_44990608/article/details/107627636