Last active
August 16, 2017 21:52
-
-
Save AdoHaha/7bfc45cef5fb2698c9c397e9df422477 to your computer and use it in GitHub Desktop.
change hostname through Tkinter app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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