Skip to content

Instantly share code, notes, and snippets.

@kubsoo
Created January 7, 2018 21:09
Show Gist options
  • Save kubsoo/36b16278ece326dacc487bc68140a4be to your computer and use it in GitHub Desktop.
Save kubsoo/36b16278ece326dacc487bc68140a4be to your computer and use it in GitHub Desktop.
Python script which automatically updates kernel to newest available version in http://kernel.ubuntu.com/~kernel-ppa/mainline/. Scripts works for Ubuntu and Debian systems.
#!/usr/bin/python
import os,commands,requests, re
get_ver = 'uname -r'
get_arch = 'dpkg --print-architecture'
url = 'http://kernel.ubuntu.com/~kernel-ppa/mainline/'
#Check processor architecture
status,arch = commands.getstatusoutput(get_arch)
#Check currently installed kernel version
status,ver = commands.getstatusoutput(get_ver)
print ("System Architecture is %s\nKernel version is %s") % (arch, ver)
r = requests.get(url)
regex = re.compile(r'href="v([^ \/]+).*(\d\d\d\d-\d\d-\d\d\s+\d\d:\d\d)')
a = regex.findall(r.text)
version = a[-1][0]
#find url for newest kernel in url = 'http://kernel.ubuntu.com/~kernel-ppa/mainline/'
url_ver = url+'v'+version
#find necessary deb files for newest version in url_ver:
#linux-headers-VERSION-NUMBER_all.deb
#linux-headers-VERSION-NUMBER_amd64.deb
#linux-image-VERSION-NUMBER_amd64.deb
w = requests.get(url_ver)
regex = re.compile(r'>(linux-headers.*_all.deb)')
a = regex.findall(w.text)
h1 = a[0]
regex = re.compile(r">(linux-headers.*-generic_.*_"+arch+".deb)")
a = regex.findall(w.text)
h2 = a[0]
regex = re.compile(r">(linux-image.*-generic_.*_"+arch+".deb)")
a = regex.findall(w.text)
h3 = a[0]
regex = re.compile(r'linux-headers-(.*)')
a = regex.findall(h1)
ver_ins = a[0].split('_')[0]
print ("Found new kernel version %s") % ver_ins
#Check if installed version is different than newest version
if ver[:len(ver_ins)] == ver_ins or ver_ins[:len(ver)] == ver:
print("Kernel is already in newest version")
else:
download = raw_input("Proceed with download Y/n: ")
#Download linux-headers-VERSION-NUMBER_all.deb, linux-headers-VERSION-NUMBER_amd64.deb, linux-image-VERSION-NUMBER_amd64.deb
if download == "Y" or download == "y":
command1 = 'wget '+url_ver+'/'+h1
os.system(command1)
command2 = 'wget '+url_ver+'/'+h2
os.system(command2)
command3 = 'wget '+url_ver+'/'+h3
os.system(command3)
print("Kernel packages downloaded successfully!!!\n")
#Install kernel packages
install = raw_input("Proceed with install Y/n: ")
if install == "Y" or install == "y":
os.system("dpkg -i *.deb")
print("Installation successfull\n")
#Reboot system
reboot = raw_input("Proceed with reboot Y/n: ")
if reboot == "Y" or reboot == "y":
os.system("reboot")
if reboot == "N" or reboot == "n":
print("Quitting!")
if install == "N" or install == "n":
print("Quitting!")
if download == "n" or download == "N":
print("Quitting!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment