自動批量對linux伺服器安裝配置NTP服務
最近專案上新上了2臺時鐘裝置,需要把網路內的一百多臺linux伺服器配置NTP服務,實現和時鐘裝置時間同步,為了避免每臺裝置挨個配置的麻煩,寫了一個python指令碼。指令碼分3個檔案,第一個是python指令碼,第二個是主機清單,裡面包含IP地址和密碼兩項,第三個是命令檔案,包含從yum安裝ntp軟體包到配置ntp.conf引數檔案到啟動NTP服務所要執行的操作語句。執行指令碼,呼叫這個兩個檔案,網路內的所有伺服器NTP安裝配置起服務就完成了。事實上這個指令碼可以做各種批量操作,只需要改一下命令檔案即可,指令碼不用做任何更改。
cat cmd_list.txt
###配置NTP檔案
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
###安裝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
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()
執行:
python auto_ntp.py host.txt cmd_list.txt
批量做其他任意操作均可,只要修改cmd_list.txt檔案中的操作語句即可
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/51077/viewspace-2699211/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Linux NTP服務配置 for Oracle RACLinuxOracle
- Linux 之 NTP 服務 伺服器Linux伺服器
- Kali Linux常用服務配置教程安裝及配置DHCP服務Linux
- linux下安裝、配置samba服務LinuxSamba
- Linux伺服器配置---ntpLinux伺服器
- 如何在 Linux 上安裝、配置 NTP 伺服器和客戶端?Linux伺服器客戶端
- LINUX下VNC服務的安裝配置LinuxVNC
- linux下Samba服務配置與安裝LinuxSamba
- CentOS 配置阿里雲 NTP 服務CentOS阿里
- Nginx服務安裝配置Nginx
- RabbitMQ服務安裝配置MQ
- Linux伺服器---郵件服務postfix安裝Linux伺服器
- 【NTP】Redhat5.1環境下網路時間服務NTP的安裝部署Redhat
- 服務端常見服務安裝及配置服務端
- linux 配置ntp時間伺服器Linux伺服器
- Linux環境VNC服務安裝、配置與使用LinuxVNC
- Linux 實現自動安裝服務元件以及優化核心引數Linux元件優化
- linux 安裝git服務LinuxGit
- Linux平臺下snmp服務的安裝和配置Linux
- 【VNC】Linux環境VNC服務安裝、配置與使用VNCLinux
- Linux 安裝Mosquitto服務LinuxUI
- CentOS6.7安裝配置nfs服務CentOSNFS
- Kali Linux常用服務配置教程啟動DHCP服務Linux
- Linux伺服器---郵件服務postfix配置Linux伺服器
- Cobbler自動化批量安裝Linux作業系統 - 運維總結Linux作業系統運維
- Linux系統安裝Redis服務LinuxRedis
- Linux下安裝SVN服務端Linux服務端
- 在Linux上安裝Memcached服務Linux
- ubuntu系統samba服務的安裝配置UbuntuSamba
- centos8安裝和配置redis服務CentOSRedis
- CentOS7下RabbitMQ服務安裝配置CentOSMQ
- 編寫安裝配置mail服務指令碼AI指令碼
- 13. 自動化運維——批量關閉tomcat服務運維Tomcat
- Linux伺服器配置——安裝CentOS以及源配置Linux伺服器CentOS
- Linux下安裝、配置Apache伺服器LinuxApache伺服器
- Linux 伺服器安裝配置 TimeMachineLinux伺服器Mac
- 【VNC】Linux環境VNC服務安裝、配置與使用(精簡版)VNCLinux
- cobbler 批量安裝linux薦Linux