Reputation: 1837
I need to emulate a network with n hosts connected by a switch. The perfect tool for this seems to be mininet. The problem is that I need to run a python script in every host that makes use of the hostname. The skript acts different depending on the hostname, so this is very important for me :)
But the hostname seems to be the same in every host! Example:
h1 hostname
outputs "simon-pc"
h2 hostname
outputs "simon-pc"
"simon-pc" is the hostname of my "real" underlying ubuntu system.
I don't find a possibility to change the hostname on the host.
Is this even possible? And if yes, how? If no, why not?
I read about mininet using one common kernel for every host. Might this be the problem?
Upvotes: 2
Views: 2094
Reputation: 274
I finally figure out how to do it , first you need to run the "ifconfig" command from inside the program and storing it in a variable , second you use regular expressions 're' to grab the text , I use it to grab the addressees of my hosts...you can do the same for the hostname
code:
getip():
ifconfig_output=(subprocess.check_output('ifconfig')).decode()
ip=re.search(r".\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}",ifconfig_output)
return((str(ip))[47:-2])
The first line will get the output from the terminal (in binary use decode to represent it in utf-8 encoding string)
The second line will grab the ip in the text using regular expressions
The Third will grab the ip from the search object and eliminate info about the position of the grabbed text in the given string
Upvotes: 1
Reputation: 453
I don't think you can get different names by running the "hostname" on each host. Only networking-related commands will produce different results on different hosts because the hosts run on separated namespaces.
So perhaps one way to get the hostname is to run ifconfig and intepret the hostname from the interfaces' name.
Upvotes: 1