通过从一个 csv 中读取并将其写入 Python 中的其他 csv 来执行 NSLOOKUP
Perform NSLOOKUP by reading it from one csv and writing it to other csv in Python
我一直在尝试执行 NSLOOKUP 并将值写入 csv 文件。
我的file1.csv:
Hostname
abc1
def2
fgh3
.
.
.
and so on
我想对这些主机名进行 nslookup 并将 FQDN 写入 file2.csv,如下所示:
Hostname FQDN
abc1 abc1.na.edu
def2 bcd2.sa.edu
fgh3 fgh3.ea.edu
.
.
and so on
这是我的代码:
import socket
import csv
name = []
with open('file1.csv', 'r', newline='') as csvinput:
reader = csv.reader(csvinput)
for line in csvinput:
name = socket.getfqdn(line)
with open('file2.csv', 'w', newline='') as csvoutput:
fieldnames = (['Hostname', 'FQDN'])
output = csv.writer(csvoutput)
output.writerow(fieldnames)
output.writerow([name])
我只能在输出文件中获得一个随机主机名的一个 fqdn。虽然,当我只使用 'print(name)' 时,它会在控制台中为我提供所有 FQDN。我知道我遗漏了一些东西,请帮忙!!
对代码做了一些修改:
import socket
import os
import csv
name = {}
CI = {}
with open('file1.csv', 'r', newline='') as csvinput:
reader = csv.DictReader(csvinput)
# status = [row['FQDN'] for row in rows]
for rows in reader:
CI = rows['CI_Name']
name = socket.getfqdn(CI)
with open('file2.csv', 'a', newline='') as csvoutput:
output = csv.writer(csvoutput)
#fieldnames = ['CI_Name', 'FQDN']
#output = csv.DictWriter(csvoutput, fieldnames=fieldnames)
#output.writerow(rows)
output.writerows([[CI] + [name]])
我能够打印名称和相应的 FQDN,但无法在 csv 列上打印 header,即 'CI_Name' 和 'FQDN'
import socket
import os
import csv
name = {}
CI = {}
with open('file1.csv', 'r', newline='') as csvinput:
reader = csv.DictReader(csvinput)
# status = [row['FQDN'] for row in rows]
with open('file2.csv', 'w', newline='') as csvoutput:
fieldnames = ['CI_Name', 'FQDN']
output = csv.DictWriter(csvoutput, fieldnames=fieldnames)
output.writeheader()
for rows in reader:
CI = rows['CI_Name']
name = socket.getfqdn(CI)
with open('file2.csv', 'a', newline='') as csvoutput:
output = csv.writer(csvoutput)
output.writerows([[CI] + [name]])
我一直在尝试执行 NSLOOKUP 并将值写入 csv 文件。
我的file1.csv:
Hostname
abc1
def2
fgh3
.
.
.
and so on
我想对这些主机名进行 nslookup 并将 FQDN 写入 file2.csv,如下所示:
Hostname FQDN
abc1 abc1.na.edu
def2 bcd2.sa.edu
fgh3 fgh3.ea.edu
.
.
and so on
这是我的代码:
import socket
import csv
name = []
with open('file1.csv', 'r', newline='') as csvinput:
reader = csv.reader(csvinput)
for line in csvinput:
name = socket.getfqdn(line)
with open('file2.csv', 'w', newline='') as csvoutput:
fieldnames = (['Hostname', 'FQDN'])
output = csv.writer(csvoutput)
output.writerow(fieldnames)
output.writerow([name])
我只能在输出文件中获得一个随机主机名的一个 fqdn。虽然,当我只使用 'print(name)' 时,它会在控制台中为我提供所有 FQDN。我知道我遗漏了一些东西,请帮忙!!
对代码做了一些修改:
import socket
import os
import csv
name = {}
CI = {}
with open('file1.csv', 'r', newline='') as csvinput:
reader = csv.DictReader(csvinput)
# status = [row['FQDN'] for row in rows]
for rows in reader:
CI = rows['CI_Name']
name = socket.getfqdn(CI)
with open('file2.csv', 'a', newline='') as csvoutput:
output = csv.writer(csvoutput)
#fieldnames = ['CI_Name', 'FQDN']
#output = csv.DictWriter(csvoutput, fieldnames=fieldnames)
#output.writerow(rows)
output.writerows([[CI] + [name]])
我能够打印名称和相应的 FQDN,但无法在 csv 列上打印 header,即 'CI_Name' 和 'FQDN'
import socket
import os
import csv
name = {}
CI = {}
with open('file1.csv', 'r', newline='') as csvinput:
reader = csv.DictReader(csvinput)
# status = [row['FQDN'] for row in rows]
with open('file2.csv', 'w', newline='') as csvoutput:
fieldnames = ['CI_Name', 'FQDN']
output = csv.DictWriter(csvoutput, fieldnames=fieldnames)
output.writeheader()
for rows in reader:
CI = rows['CI_Name']
name = socket.getfqdn(CI)
with open('file2.csv', 'a', newline='') as csvoutput:
output = csv.writer(csvoutput)
output.writerows([[CI] + [name]])