how to edit hostname file using fabric

2024/7/7 6:20:19

I have change my hosts file,so how to change hostname.my system is ubuntu. eg my hosts file:

192.168.0.100 host1.mydomain.com
192.168.0.101 host2.mydomain.com

I wanna the hostname file under /etc/hostname of host1 to host1.mydomain.com,the hostname file of host2 to host2.mydomain.com

how to do that using fabric? I have to ssh every host and edit the hostname file,does fabric can do this?

I didn't mean to use hostname command but to edit the /etc/hostname file. I mean how to use fabric to do that: such as:

def update_hostname():get("/etc/hosts","hosts")hosts_content = file("hosts")**hostname = ·get the hostname corespond to ip·**get("/etc/hostname","hostname")update `hostname file`put("hostname","/etc/hostname")

how get the ip? because fabric do the job on every host, and the hostname is correspond to each host. I need to know the which host the job is working and then get the ip back,then get the hostname correspond the the ip,and final update the hostname file. enter image description here

Answer

Fabric is just a SSH wrapper, so what you're looking at is LINUX specific, not frabric or python specific.

from fabric.api import run
run('hostname your-new-name')
run('echo your-new-hostname > /etc/hostname')

And just do a run(..edit..) according to your linux dist?

Or just do:

from subprocess import Popen, PIPE
hosts = open('/etc/networking/hosts', 'rb')
for hostline in hosts.readlines():ip, name = hostline.split(' ')command = ['ssh', '-t', 'root@' + host.strip('\r\n ,;), ' ', "echo " + name.strip('\r\n ,;) + " > /etc/hostname",]stdout, stderr = Popen(command, stdout=PIPE, stderr=PIPE).communicate()
hosts.close()

Note: /etc/networking/hosts might be placed somewhere else for you. The important part here is that you loop through the /hosts file, and ssh to each machine echoing the given hostname to that machine.

https://en.xdnf.cn/q/120514.html

Related Q&A

functions and indentation in python [duplicate]

This question already has answers here:How do I get ("return") a result (output) from a function? How can I use the result later?(4 answers)Closed 3 days ago.Im taking a tutorial in udemy t…

How do I define a method to store a collection (e.g., dictionary)?

Im a beginner working on a library management system and theres something I cant get my head around. So I have a Books class that creates new book records. class Books:def __init__(self, title=None, au…

Python Regex punctuation recognition

I am stumped by this one. I am just learning regular expressions and cannot figure out why this will not return punctuation marks.here is a piece of the text file the regex is parsing:APRIL/NNP is/VBZ …

cant find brokenaxes module

I want to create a histogram with broken axes and found that there must be a module doing this called brokenaxes that works together with matplotlib (source) . Anyway, when I trie to import the modul…

Python-Pandas While loop

I am having some trouble with the DataFrame and while loop:A B 5 10 5 10 10 5I am trying to have a while loop that:while (Column A < Column B):Column A = Column A + (Column B / 2)Colu…

split an enumerated text list into multiple columns

I have a dataframe column which is a an enumerated list of items and Im trying to split them into multiple columns. for example dataframe column that looks like this:ID ITEMSID_1 1. Fruit 12 oranges 2…

Python using self as an argument [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

how to shuffle questions in python

please anyone help me i have posted my whole work down can anyone tell me how to shuffle theses five questions it please i will be very thankful.print("welcome to the quiz") Validation = Fals…

Selenium python do test every 10 seconds

I am using selenium (python) testing and I need to test my application automatically every 10 seconds.How can I do this?

Type Object has no attribute

I am working on a program, but I am getting the error "Type object Card has no attribute fileName. Ive looked for answers to this, but none that Ive seen is in a similar case to this.class Card: R…