如何使用Python获取SAP Temporary key 临时密钥

什么是Sap 临时密钥?

SAP 临时密钥是用于激活 SAP 软件的一种许可证,只能在有限的时间内使用。这些密钥可用于评估目的或在系统迁移期间使用。它们是临时解决方案,通常在一定时间后过期。

如何获取Sap Temporary keys?

临时密钥: 如果创建永久许可证密钥时出现任何错误,SAP支持门户网站提供临时的Business Objects许可证密钥。您可以通过登录到支持门户网站上的链接 www.service.sap.com/licensekey并选择“获取临时许可证密钥”链接来下载这些密钥。这些临时许可证密钥的有效期最长为90天。

方法1:手动下载

  1. www.service.sap.com/licensekey 打开网址之后点击这里下载一个名为“bobj-temp-license-key.zip”的文件:
    在这里插入图片描述
  2. 解压之后有一名为“SAP Analytics Emergency License Key Process”的pdf文件,在这里面可以找到你需要的License Key
    在这里插入图片描述

方法2:使用Python自动化获取Sap Temporary keys的过程

导入需要的包

from selenium import webdriver
import time
import zipfile
import PyPDF2
import os
import shutil
import warnings

下载bobj-temp-license-key.zip

# 定义文件夹路径,替换成你想保存zip文件的路径
download_zip_path = r"C:\\Users\\USERNAME\\Projects\\download_zip"

# 下载zip文件
options = webdriver.ChromeOptions()
prefs = {
    
    'profile.default_content_settings.popups': 0,
         'download.default_directory': download_zip_path}
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://support.sap.com/content/dam/support/en_us/library/ssp/my-support/keys/bobj-temp-license-key.zip')

time.sleep(5)
driver.quit()

解压zip文件以获取pdf

# zip the zip file into 'bobj-temp-license-key'
file_path_zip = r"{}/bobj-temp-license-key.zip".format(download_zip_path)

if os.path.exists(file_path_zip):
    zip_file = zipfile.ZipFile(file_path_zip)
    zip_file.extractall(keys_path)
    zip_file.close()
    print("解压成功")
else:
    print("解压失败")

# 解压成功之后即可获得pdf的路径
file_path_pdf = "..."

从pdf文件中获取需要的Licence Key

以SAP BusinessObjects Tempory Licence Key为例,Licence Key位于pdf第二页的“CPU 4.2”字段之后,经过测试确定获取SAP BusinessObjects Tempory Licence Key的function:

# 获取SAP BusinessObjects Tempory Licence Key
def get_key_from_text(text):
    start = text.find('CPU 4.2') + 8
    return text[start:start + 32]

file_path_pdf为上一步中获取的pdf的路径:

if os.path.exists(file_path_pdf):
    pdf_file = open(file_path_pdf, 'rb')
    pdf_reader = PyPDF2.PdfReader(pdf_file)
    page = pdf_reader.pages[1]
    text = page.extract_text()
    latest_key = get_key_from_text(text)
    pdf_file.close()

如果每隔90天就需要更新一次license key,可以把licence key保存在一个txt文件里,成功获取最新的licence key之后可以和本地保存的licence key对比,如果latest_key != current_key,则将latest_key 替换current_key,以确保txt文件里永远保存最新的licence key。

if os.path.exists(file_path_pdf):
    pdf_file = open(file_path_pdf, 'rb')
    pdf_reader = PyPDF2.PdfReader(pdf_file)
    page = pdf_reader.pages[1]
    text = page.extract_text()
    latest_key = get_key_from_text(text)
    pdf_file.close()
    if latest_key != current_key:
        with open(current_key_txt_path, 'w') as file:
            file.write(latest_key)
        print("license已更新, latest license", latest_key)
    else:
        print("license未更新, current license:", latest_key)
else:
    print("PDF不存在")

完整代码

from selenium import webdriver
import time
import zipfile
import PyPDF2
import os
import shutil
import warnings

warnings.filterwarnings("ignore")

download_zip_path = r"C:\\Users\\USERNAME\\Projects\\download_zip" #zip文档下载到哪个文件夹
keys_path = r"C:\\Users\\USERNAME\\Projects\\bobj-temp-license-key"#zip文件解压到哪里
current_key_txt_path = r"C:\\Users\\USERNAME\\Projects\\BO_KEY.txt" #保存最新的licence的txt文件路径

# 读取txt文件,定义current_key
with open(current_key_txt_path, 'r') as file:
    current_key = file.read()


# 清空文件夹
## 清空download_zip_path和keys_path文件夹,清空之后再保存文件,方便后续读取
### 定义清空文件夹的function
def delete_folder_contents(folder_path):  
    for filename in os.listdir(folder_path):
        file_path = os.path.join(folder_path, filename)
        try:
            if os.path.isfile(file_path) or os.path.islink(file_path):
                os.unlink(file_path)
            elif os.path.isdir(file_path):
                shutil.rmtree(file_path)
        except Exception as e:
            print(f"Failed to delete {
      
      file_path}. Reason: {
      
      e}")

### 清空download_zip_path和keys_path文件夹
delete_folder_contents(download_zip_path)
delete_folder_contents(keys_path)

# download the latest zip
options = webdriver.ChromeOptions()
prefs = {
    
    'profile.default_content_settings.popups': 0,
         'download.default_directory': download_zip_path}
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://support.sap.com/content/dam/support/en_us/library/ssp/my-support/keys/bobj-temp-license-key.zip')

time.sleep(5)
driver.quit()

# zip the zip file into 'bobj-temp-license-key'
file_path_zip = r"{}/bobj-temp-license-key.zip".format(download_zip_path)

if os.path.exists(file_path_zip):
    zip_file = zipfile.ZipFile(file_path_zip)
    zip_file.extractall(keys_path)
    zip_file.close()
    print("zip file unzipped successfully")
else:
    print("zip File does not exist")

time.sleep(5)

file_path_pdf = r'{}\SAP Analytics Emergency License Key Process.pdf'.format(keys_path)


def get_key_from_text(text):
    start = text.find('CPU 4.2') + 8
    return text[start:start + 32]


if os.path.exists(file_path_pdf):
    pdf_file = open(file_path_pdf, 'rb')
    pdf_reader = PyPDF2.PdfReader(pdf_file)
    page = pdf_reader.pages[1]
    text = page.extract_text()
    latest_key = get_key_from_text(text)
    pdf_file.close()
    if latest_key != current_key:
        with open(current_key_txt_path, 'w') as file:
            file.write(latest_key)
        print("license已更新, latest license", latest_key)
    else:
        print("license未更新, current license:", latest_key)
else:
    print("PDF不存在")

猜你喜欢

转载自blog.csdn.net/xili1342/article/details/132342882