Skip to content

Instantly share code, notes, and snippets.

@LordH3lmchen
Forked from n0ts/get_oracle_jdk_x64.sh
Last active July 8, 2018 18:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LordH3lmchen/d76e0679b6a647a195ce to your computer and use it in GitHub Desktop.
Save LordH3lmchen/d76e0679b6a647a195ce to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# encoding: utf-8
import os
import urllib
import urllib.request
import re
def regex_websearch(url, pattern):
print(urllib)
print(urllib.request)
resp = urllib.request.urlopen(url)
content = resp.read().decode('UTF-8')
resp.close()
match = re.search(pattern, content)
return match
"""
this function downloads the lates Java
thx to n0ts for the idea
https://gist.github.com/n0ts/40dd9bd45578556f93e7
"""
def download_latest_java(destination, java_version=8, package='server-jre', extension='tar.gz', architecture='linux-x64'):
valid_packages = ['jre', 'server-jre', 'jdk']
if package not in valid_packages:
print('Invalid Java package selection, valid packages are:')
for valid_package in valid_packages:
print('\t' + valid_package)
return None
url = "http://www.oracle.com"
url_1 = url + "/technetwork/java/javase/downloads/index.html"
pattern_1 = '\/technetwork\/java/\javase\/downloads\/'+ package + str(java_version) + '-downloads-.+?\.html'
match = regex_websearch(url_1, pattern_1)
if match == None:
print('Unable to download Java from ' + url_1)
print('Website is down or script is outdated')
return None
url_2 = url + match.group(0)
pattern_2 = "http\:\/\/download.oracle\.com\/otn-pub\/java\/jdk\/[7-9]u[0-9]+?-.+?\/" + package + "-[7-9]u[0-9]+?-" + architecture + "." + extension
match = regex_websearch(url_2, pattern_2)
if match == None:
print('Selected architecture.extension \"' + architecture + '.' + extension + '\" is not available')
print('Visit \"' + url_2 + '\" to see available architectures and extensions')
return None
req = urllib.request.Request(match.group(0))
req.add_header('Cookie', 'oraclelicense=accept-securebackup-cookie')
if(os.path.exists(destination) and os.path.isdir(destination)):
destination = destination + '/' + os.path.basename(req.full_url)
print('Downloading ' + match.group(0) + ' to ' + destination)
with urllib.request.urlopen(req) as f:
open(destination, "wb").write(f.read())
return destination
#!/bin/bash
# usage: get_jdk.sh <jdk_version> <rpm|tar>
# jdk_version: default 8
# rpm
JDK_VERSION="8"
EXT="rpm"
if [ -n "$1" ]; then
if [ "$1" == "7" ]; then
JDK_VERSION="7"
fi
fi
if [ -n "$2" ]; then
if [ "$2" == "tar" ]; then
EXT="tar.gz"
fi
fi
URL="http://www.oracle.com"
JDK_DOWNLOAD_URL1="${URL}/technetwork/java/javase/downloads/index.html"
JDK_DOWNLOAD_URL2=`curl -s $JDK_DOWNLOAD_URL1 | grep -Po "\/technetwork\/java/\javase\/downloads\/jdk${JDK_VERSION}-downloads-.+?\.html" | head -1`
if [ -z "$JDK_DOWNLOAD_URL2" ]; then
echo "Could not get jdk download url - $JDK_DOWNLOAD_URL1"
exit 1
fi
JDK_DOWNLOAD_URL3="${URL}${JDK_DOWNLOAD_URL2}"
JDK_DOWNLOAD_URL4=`curl -s $JDK_DOWNLOAD_URL3 | egrep -o "http\:\/\/download.oracle\.com\/otn-pub\/java\/jdk\/[7-8]u[0-9]+\-(.*)+\/jdk-[7-8]u[0-9]+(.*)linux-x64.${EXT}"`
wget --no-cookies \
--no-check-certificate \
--header "Cookie: oraclelicense=accept-securebackup-cookie" \
$JDK_DOWNLOAD_URL4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment