- 论坛徽章:
- 0
|
小弟是python新手,今天写了个脚本,运行的时候没有什么报错信息,但结果是没有成功的
#!/usr/bin/env python
import string
import re
import paramiko
import os
import socket
def file_fun():
log=open('/root/mss-python-log.txt','w')
filentp="/etc/ntp.conf"
try:
f1=open(filentp,'r')
except IOError,e:
log.write('there is no ntp.conf file')
allline1=f1.readlines()
f1.close()
#alllines.remove('0.rhel.pool.ntp.org')
allline1.insert(16,'server time.abc.com prefer\n')
myfile1=file("/etc/ntp.conf",'w')
for eachline in allline1:
print >> myfile1,eachline,
def exe_ssh():
s=paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
s.connect(hostname,port,username,password)
file_fun()
stdin,stdout,stderr=s.exec_command("service ntpd start")
stdin,stdout,stderr=s.exec_command("chkconfig ntpd on")
print hostname,":","on line and NTP have been configured!"
except socket.error:
print hostname,":","is NOT online!"
s.close()
hostname="192.168.1.1"
port=22
username="root"
password="shichunda"
exe_ssh()
我的目的是想给远端的一个服务器设置ntp服务。
我想给192.168.1.1这台服务器的/etc/ntp.conf文件添加一行,之后执行service ntpd restart和chkconfig ntpd on。测试结果是在192.168.1.1上ntpd.conf文件没有成功添加server time.abc.com prefer这行!!!
但我在192.168.1.1本地用这个脚本的file_fun()部分就没问题,能成功修改ntp.conf文件
请高手们帮我看一下哪出了问题
还有, stdin,stdout,stderr=s.exec_command("service ntpd restart")也没有成功执行,这是为什么呢?
|
|