Python を使用して SAP 一時キーを取得する方法

SAP 一時キーとは何ですか?

SAP 一時キーは、SAP ソフトウェアをアクティブ化するために使用されるライセンスであり、限られた期間のみ使用できます。これらのキーは、評価目的またはシステム移行中に使用できます。これらは一時的な解決策であり、通常は一定期間後に期限切れになります。

SAP の一時キーを入手するにはどうすればよいですか?

一時キー: 永久ライセンス キーの作成中にエラーが発生した場合、SAP サポート ポータルは一時的な Business Objects ライセンス キーを提供します。これらのキーをダウンロードするには、リンクwww.service.sap.com/licensekeyでサポート ポータルにログインし、[一時ライセンス キーを取得する] リンクを選択します。これらの一時ライセンス キーは最大 90 日間有効です。

方法 1: 手動ダウンロード

  1. URL www.service.sap.com/licensekeyを開いた後、ここをクリックして「bobj-temp-license-key.zip」という名前のファイルをダウンロードします。
    ここに画像の説明を挿入します
  2. 解凍後、「SAP Analytics Emergency License Key Process」という名前の PDF ファイルが表示されます。ここで、必要なライセンス キーを見つけることができます。
    ここに画像の説明を挿入します

方法 2: Python を使用して SAP 一時キーを取得するプロセスを自動化する

必要なパッケージをインポートする

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 ファイルから必要なライセンス キーを取得します。

SAP BusinessObjects 一時ライセンス キーを例に挙げます。ライセンス キーは、PDF の 2 ページ目の「CPU 4.2」フィールドの後にあります。テスト後、SAP BusinessObjects 一時ライセンス キーを取得する機能は次のように決定されます。

# 获取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 日ごとにライセンス キーを更新する必要がある場合は、ライセンス キーを txt ファイルに保存できます。最新のライセンス キーを正常に取得した後、それをローカルに保存されたライセンス キーと比較できます。latest_key != current_key の場合、current_key を置き換えます最新のライセンス キーが常に txt ファイルに保存されるようにするために、latest_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不存在")

Guess you like

Origin blog.csdn.net/xili1342/article/details/132342882