Skip to content

Instantly share code, notes, and snippets.

@AdoHaha
Last active August 16, 2017 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdoHaha/7bfc45cef5fb2698c9c397e9df422477 to your computer and use it in GitHub Desktop.
Save AdoHaha/7bfc45cef5fb2698c9c397e9df422477 to your computer and use it in GitHub Desktop.
change hostname through Tkinter app
[Desktop Entry]
Type=Application
Exec=python /home/robotuser/helper/change_hostname.py
Icon=/home/robotuser/Downloads/change.png # only if needed
Name=Change the hostname
Terminal=false
#!/usr/bin/python
from Tkinter import *
import subprocess
import tkMessageBox
master = Tk()
master.title('Change hostname')
e = Entry(master)
e.pack()
e.focus_set()
import socket
def hostname_resolves(hostname):
'''this function checks if hostname already exists in the network'''
try:
socket.gethostbyname(hostname)
return True
except socket.error:
return False
def callback():
''' callback from button press or enter on textarea'''
if(e.get()!=""):
name=e.get()
else:
return
hostname_full=["hostnamectl","set-hostname",name]
if(hostname_resolves(name)):
tkMessageBox.showerror("Hostname exists","Hostname exists already in the network")
return
if(tkMessageBox.askyesno("Hostname changed",
"Do you want to change hostname to {}?".format(name))):
subprocess.call(hostname_full)
tkMessageBox.showinfo("Hostname changed", "Hostname changed to {}".format(name))
master.destroy()
b = Button(master, text="change hostname", width=15, command=callback)
b.pack()
e.bind("<Return>", lambda x: callback())
mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment