如何用Python替换debian上/etc/hosts中的主机名
我正在编写脚本,用Python在Debian系统上设置主机名。我在以下方面取得了成功:如何用Python替换debian上/etc/hosts中的主机名,python,ubuntu,debian,hostname,Python,Ubuntu,Debian,Hostname,我正在编写脚本,用Python在Debian系统上设置主机名。我在以下方面取得了成功: 从arg1获取新主机名,或者将其定义为变量值 获取当前主机名 打开/etc/hostname,将变量中的新主机名写入文件,然后将其关闭 打开/etc/hosts进行读写 我被困在那里了。我尝试将其作为字符串读取以执行str.replace(oldname,newname),但如果我不将文件内容转换为str,则会遇到问题。如果我那样做,我就不能写文件了 或者,我尝试了re.sub(),但同样在将结果写入/e
- 从arg1获取新主机名,或者将其定义为变量值
- 获取当前主机名
- 打开
,将变量中的新主机名写入文件,然后将其关闭/etc/hostname
- 打开
进行读写/etc/hosts
str.replace(oldname,newname)
,但如果我不将文件内容转换为str
,则会遇到问题。如果我那样做,我就不能写文件了
或者,我尝试了re.sub()
,但同样在将结果写入/etc/hosts
时遇到问题
任何反馈都将不胜感激
我研究了一些例子,发现。我从中吸取了教训,但没有找到解决问题的办法
Bash绝对是这项工作的正确工具。3行,如果我不连接。然而,我需要一个python解决方案
上面引用的代码写到主机名:我已经处理好了。我没有发现同样的策略适用于主机
这是工作代码,谢谢你的建议。它们需要得到考虑。我也省略了结论。但这是狭义定义的工作:
#!/usr/bin/python -ex
import os, sys, syslog
#Customize
hosts_file = '/etc/hosts'
hostname_file = '/etc/hostname'
#Check for root
if not os.geteuid()==0:
sys.exit("\nOnly root can run this script\n")
if len(sys.argv) != 2:
print "Usage: "+sys.argv[0]+" new_hostname"
sys.exit(1)
new_hostname = sys.argv[1]
print 'New Hostname: ' +new_hostname
#get old hostname
f_hostname = open('/etc/hostname', 'r')
old_hostname = f_hostname.readline()
old_hostname = old_hostname.replace('/n','')
f_hostname.close()
print 'Old Hostname: ' +old_hostname
#open hosts configuration
f_hosts_file = open(hosts_file, 'r')
set_host = f_hosts_file.read()
f_hosts_file.close()
pointer_hostname = set_host.find(old_hostname)
#replace hostname in hosts_file
set_host = set_host.replace(old_hostname, new_hostname)
set_host_file = open(hosts_file,'w')
set_host_file.seek(pointer_hostname)
set_host_file.write(set_host)
set_host_file.close()
#insert code to handle /etc/hostname
#change this system hostname
os.system('/bin/hostname '+new_hostname)
#write syslog
syslog.syslog('CP : Change Server Hostname')
然后,我希望编写一个函数来写入/替换旧主机名所在的新主机名。您提供的链接打开主机文件进行读取,将其内容保存在字符串中,调用字符串上的replace,关闭文件,打开文件进行写入并写入字符串-这究竟是如何解决您的问题的
f = open("/etc/hosts", "r") #open file for reading
contents = f.read() #read contents
f.close() #close file
contents.replace(old, new) #replace
f = open("/etc/hosts", "w") #open file for writing
f.write(contents) #write the altered contents
f.close() #close file
您也可以使用r+
模式在不关闭和重新打开文件的情况下执行此操作:
f = open("/etc/hosts", "r+") #open file with mode r+ for reading and writing
contents = f.read() #read the file
contents.replace(old, new) #replace
f.seek(0) #reset the file pointer to the start of the file
f.truncate() #delete everything after the file pointer
f.write(contents) #write the contents back
f.close() #close the file
请注意,如果不采取特殊预防措施,则使用
replace
是不安全的-例如,主机名可能是主机文件中包含的其他主机名或别名的子字符串,因此您至少应该在替换之前用空格将其包围。您还需要确保输入的内容作为主机名都是有效的。处理所有这些问题的最简单方法可能是通过subprocess.Popen
hostname
命令来更改带有域的主机名
在文件系统中的任何位置都需要名为hosts\u tail的帮助文件,该文件看起来:
帮助文件主机\u tail
Python函数:
我的debian操作系统:
cat /etc/debian_version
6.0.6
这些与Linux中操作系统配置文件的交互是使用
sed
的bash
(shell)脚本的一个很好的用例,以备您尝试。您遇到了什么错误,或者什么不应该发生?为什么不使用主机名可执行文件?您可能必须读取所有文件内容并替换所需的位。应该没那么难。您的第一个os.system
调用将覆盖主机文件。所有条目都需要在hosts\u tail文件中复制,或者通过调用函数删除。此外,捕获所有异常似乎很糟糕:如果函数部分执行,它可能会使系统处于不一致的状态-在这种情况下,我需要的信息不仅仅是False
,例如,您的代码没有在任何地方定义mypath
;像这样运行它将始终返回False
,因为出现UnboundLocalError
。发现seek(0)是我的问题。据我所知,它适用于/etc/hostname,但不适用于/etc/hosts。replace()。您必须将其重新分配给变量:contents=contents.replace(旧、新)
def set_hostname(hostname,domain):
"""
usage: set_hostname('debian','labor.local')
"""
try:
os.system('echo 127.0.0.1 localhost > /etc/hosts')
os.system('echo 127.0.1.1 '+ hostname + '.' + domain + ' ' + hostname + ' >> /etc/hosts')
os.system('cat ' + mypath + 'config/hosts_tail >> /etc/hosts')
os.system('echo ' + hostname + ' > /etc/hostname')
os.system('echo ' + hostname + '.' + domain + ' > /etc/mailname')
os.system('cat /etc/resolv.conf | grep nameserver | uniq > /tmp/resolv.tmp')
os.system("echo domain " + domain + ' > /etc/resolv.conf')
os.system("echo search " + domain + ' >> /etc/resolv.conf')
os.system('cat /tmp/resolv.tmp | uniq >> /etc/resolv.conf')
os.system('rm -rf /tmp/resolv.tmp')
os.system('/etc/init.d/hostname.sh start')
except Exception, e:
return False
return True
cat /etc/debian_version
6.0.6