2017-12-11 6 views
0

システム情報をスプレッドシートに書き込もうとしています。私は私の変数を使用しようとすると、彼らはシステム情報をPythonのスプレッドシートに書き込む方法

インポートCSVインポート OS 輸入linecache

os.system('getmac -v > mac.txt') 
os.system("wmic bios get serialnumber > serial.txt") 
os.system("wmic computersystem get model > model.txt") 
os.system("hostname > hostname.txt") 
os.system("ipconfig > ip.txt") 


open('ip1.txt','w').writelines([line for line in open('ip.txt')if 'IPv4' in line]) 
open('mac1.txt','w').writelines([line for line in open('mac.txt')if 'Wi-Fi' in line]) 
open('mac2.txt','w').writelines([line for line in open('mac.txt')if 'Ethernet' in line]) 

serial = linecache.getline('serial.txt', 3) 
model = linecache.getline('model.txt', 3) 

mac = open("mac.txt","r") 
IP = open("ip1.txt","r") 
mac1 = open("mac1.txt","r") 
mac2 = open("mac2.txt","r") 
hostname = open("hostname.txt","r") 



Rmac = mac.read() 
Rip = IP.read() 
Rmac1 = mac1.read() 
Rmac2 = mac2.read() 
Rhostname = hostname.read() 



myData = [[model]] 

myFile = open('example2.csv', 'w') 
with myFile: 
    writer = csv.writer(myFile) 
    writer.writerows(myData) 

これはただのスプレッドシートに情報を書き込みません黒出てきますか?私は何を間違えているのですか?私はプログラミングに非常に慣れています。

答えて

0

あなたは仲介ファイルは必要ありません。コマンドを呼び出してCSVに情報を書いてみましょう。

import csv 
import subprocess 

# get the model 
model = subprocess.check_output(["WMIC", "computersystem", "get", "model"], 
           universal_newlines=True).strip().rsplit("\n", 1)[1] 
# get the serial 
serial = subprocess.check_output(["WMIC", "bios", "get", "serialnumber"], 
           universal_newlines=True).strip().rsplit("\n", 1)[1] 
# get the host name 
hostname = subprocess.check_output(["hostname"], universal_newlines=True).strip() 

# get WMI output for all addresses 
ips = subprocess.check_output(["WMIC", "NICCONFIG", "where", "IPEnabled=true", 
           "get", "IPAddress"], 
           universal_newlines=True).strip().split("\n\n")[1:] 
# post-process to get the addresses only 
ips = [ip.split(",")[0].strip('"{} ') for ip in ips] 

# etc. 

with open("example2.csv", "wb") as f: # open your CSV for writing 
    writer = csv.writer(f) # create a writer 
    # you didn't write a header but let's add it in 
    writer.writerow(["model", "serial", "hostname", "ips"]) # etc., you get the picture... 
    writer.writerow([model, serial, hostname, ",".join(ips)]) # add other columns, too 

そして、あなたは素敵含むexample2.csv取得します:

model,serial,hostname,ips 
Your Model,Your Serial,Your-Hostname,List.Of.IP.Addresses

は、他の分野についても同様ですか、あなたは完了です。

+0

私はこれをいつ使うのですか? [ip1.txt '、' w ']。writeelines([行内の' IPv4 'の場合は' ip.txt ')] –

+0

@demetriwolf - アップデートをチェックし、IPアドレスを解析しますWMI – zwer

+0

から、正確なコマンドを見つけて、WiFiとイーサネットのMACアドレスを指定する必要がありますか?私はそれがちょっとウーマンでやることができますが、それは私に何を教えてくれないことがわかりますか? –

関連する問題