Skip to content

Instantly share code, notes, and snippets.

@gotnix
Created August 1, 2012 07:20
Show Gist options
  • Select an option

  • Save gotnix/3224477 to your computer and use it in GitHub Desktop.

Select an option

Save gotnix/3224477 to your computer and use it in GitHub Desktop.
Verify the system time for NTP Client.
#!/usr/bin/python
# coding=utf-8
import os, paramiko
import time
hosts = [
'172.16.16.198',
'172.16.16.200'
]
pri_key_fn = '/home/terry/.ssh/id_rsa'
user_name = 'root'
ssh_port = 22
def get_rtime(hosts):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
privatekey = os.path.expanduser(pri_key_fn)
mkey = paramiko.RSAKey.from_private_key_file(privatekey)
ssh.connect(hostname=hosts, port=ssh_port, username=user_name, \
password=None, pkey=mkey, timeout=2, allow_agent=True, \
look_for_keys=True, compress=False)
stdin, stdout, stderr = ssh.exec_command('date +%s')
rem_time = float('\n'.join(map(str, stdout.readlines())))
ssh.close()
return rem_time
def time_diff():
#loc_time = time.mktime(time.localtime())
loc_time = time.time()
for ip in hosts:
difference = get_rtime(ip) - loc_time
if difference > 0:
print("The remote server %s is later than local host %f s." \
% (ip, difference))
elif difference < 0:
print("The remote server %s is earlier than local host %f s." \
% (ip, abs(difference)))
if __name__ == '__main__':
time_diff()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment