ネットワークサーバーは、NTPクロックサービスをバッチで自動的にどのように実現しますか?

ネットワークサーバーは、NTPクロックサービスをバッチで自動的にどのように実現しますか?
ネットワークサーバーは、NTPクロックサービスをバッチで自動的にどのように実現しますか?

最近、2つの新しい時計デバイスがプロジェクトに追加されました。時計デバイスとの時間同期を達成するには、ネットワーク内の100台を超えるLinuxサーバーをNTPサービスで構成する必要があります。各デバイスを1つずつ構成する手間を避けるために、pythonスクリプトが作成されました。

スクリプトは3つのファイルに分かれています。1つ目はpythonスクリプト、2つ目はIPアドレスとパスワードの2つの項目を含むホストリスト、3つ目はコマンドファイルで、yumからntp.confパラメータファイルの構成までのntpソフトウェアパッケージのインストールを含みます。 NTPサービス操作ステートメントを開始します。

スクリプトを実行し、これらの2つのファイルを呼び出すと、ネットワーク内のすべてのサーバーがインストールされ、サービスを完了するように構成されます。実際、このスクリプトはあらゆる種類のバッチ操作を実行でき、コマンドファイルを変更するだけでよく、スクリプトで変更を行う必要はありません。

猫cmd_list.txt

### NTPファイルを設定する

mv /etc/ntp.conf /etc/ntp.conf.bak

echo 'サーバー192.168.5.200' >> /etc/ntp.conf

エコー 'ファッジ192.168.5.200ストラタム10' >> /etc/ntp.conf

### NTPソフトウェアパッケージをインストールし、NTPサービスを開始する

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

猫のhost.txt

192.168.5.1 pass1

192.168.5.2 pass2

192.168.5.254 pass254

猫auto_ntp.py

インポートパラミコ

インポート時間

インポートシステム

インポートソケット

ホスト= open(sys.argv [1])

host_list = []

ユーザー名=「root」

hostaのhost.readlines()の場合:

host_list.append(hosta.strip().split())

f = open( 'log.txt'、 'a')

cmd = open(sys.argv [2])

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()

実施した:

python auto_ntp.py host.txt cmd_list.txt

cmd_list.txtファイルの操作ステートメントを変更する限り、他の操作をバッチで実行します。

おすすめ

転載: blog.csdn.net/weixin_44990608/article/details/107627636