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文件中的操作語句即可
更多關于云服務器,域名注冊,虛擬主機的問題,請訪問三五互聯官網:m.shinetop.cn