简体   繁体   English

使用python和.bat文件获取计算机序列号

[英]Getting computer serial number with python and .bat file

I have been studying for exams recently and the textbook I was reading told me that the command (in CMD) for getting a computers serial number is as follows - (as one method of lowering piracy). 我最近一直在为考试而学习,我正在阅读的教科书告诉我,获取计算机序列号的命令(以CMD表示)如下-(作为降低盗版的一种方法)。

wmic bios get serialnumber

I decided to try this for myself using python, where I wanted to get the serial number of my own computer, I created a batch file (.bat) and recorded the serial number to a txt file which I thought I could then read off in python, this created more questions if anything... here is the code I currently have: 我决定使用python自己尝试一下,我想在其中获取自己计算机的序列号,我创建了一个批处理文件(.bat),并将序列号记录到txt文件中,以为我可以在其中读取python,这会产生更多问题(如果有的话)...这是我目前拥有的代码:

SerialNumber.bat (new to bat files by the way) SerialNumber.bat(顺便说一下,这是bat文件的新增功能)

SET num=wmic bios get serialnumber

%num% > serial.txt

What serial.txt looks like (modified slightly to not give away my serial number) serial.txt是什么样的(略作修改以不泄露我的序列号)

在此处输入图片说明

Readtxt.py Readtxt.py

# Trying to read in two different ways
with open("serial.txt", "r") as file:
    print(file.read())

lines = []
for line in open("serial.txt"):
    lines.append(line)
print(lines)

which outputs: (not showing full image again) 输出:(不再显示完整图像)

在此处输入图片说明

the questions are: 问题是:

  1. How would I store the serial number as some string? 如何将序列号存储为某些字符串?
  2. Why does each different method I use to read provide different outputs? 为什么我使用的每种阅读方法都提供不同的输出?

Thanks for any answers :) 感谢您的任何回答:)

When redirecting to a file wmic writes UTF-16-LE with a BOM 重定向到文件时,wmic用BOM写入UTF-16-LE

> wmic bios get serialnumber >serial.txt

> hex.exe serial.txt
HEX:       +00 01 02 03 04 05 06 07  08 09 0a 0b 0c 0d 0e 0f  0123456789abcdef
0000000000: FF FE 53 00 65 00 72 00  69 00 61 00 6C 00 4E 00  .■S.e.r.i.a.l.N.
0000000010: 75 00 6D 00 62 00 65 00  72 00 20 00 20 00 0D 00  u.m.b.e.r. . ...

> type readtext.py
with open("serial.txt", 'rb') as file:
    print(file.read().decode('utf-16'))

> py readtext.py
SerialNumber
5xxxxyyyy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM