我是python的新手,老实说,我从来没有从事过编码,但是来到了这里,我需要编写一些脚本,下面是我的脚本,我确实从这里到那里挑选了一些脚本,最后制作成了工作时,是否应该在当前代码中使用类和函数,以使其看起来更专业,更优化?有人可以帮助我确定这里做错了什么吗?
import csv,subprocess,paramiko,time,socket
from datetime import date
IP = {}
Type = {}
Host = {}
Username = {}
Password = {}
hostname = {}
status = {}
with open('Output.csv', 'r', newline='') as csvinput:
reader = csv.DictReader(csvinput)
for item in reader:
IP = item['IP']
Host = item['Hostname']
Username = item['Username']
Password = item['Password']
Type = item['Type']
date = date.today()
if 'Ping' in Type:
print('This is ping device')
try:
ip = socket.gethostbyname(IP)
except socket.error:
pass
name = socket.getfqdn(IP)
data = name
hostname = item['IP']
response = subprocess.Popen(['ping.exe',hostname], stdout = subprocess.PIPE).communicate()[0]
response = response.decode()
print(response)
if 'bytes=32' in response:
status = 'Up'
elif 'destination host unreachable' in response:
status = 'Unreachable'
else:
status = 'Down'
if status == 'Down':
ip = 'Not Found'
with open('Ping-%s.txt' % (date), 'a', newline='') as f:
f = csv.writer(f)
f.writerow([hostname] + [data] + [status] + [ip])
## with open('Ping-%s.csv' % (date), 'a', newline='') as csvoutput:
## output = csv.writer(csvoutput)
## output.writerow([hostname] + [data] + [status] + [ip])
elif 'COR' in Type:
# Create instance of SSHClient object
remote_conn_pre = paramiko.SSHClient()
# Add untrusted hosts
remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Initiate SSH connection
remote_conn_pre.connect(IP, username=Username, password=Password, port=22)
print("SSH connection established to %s" % IP)
# Use invoke_shell to establish an 'interactive session'
remote_conn = remote_conn_pre.invoke_shell()
print("Interactive SSH session established")
# Strip the initial router prompt
terminal_output = remote_conn.recv(1000)
# Send the router a command
remote_conn.send("\n")
remote_conn.send("terminal length 0\n")
time.sleep(1)
remote_conn.send("sh run\n")
time.sleep(5)
terminal_output = remote_conn.recv(9999999)
print(terminal_output.decode())
output1 = open('%s-%s.txt' % (Host,date), 'w')
output1.write(terminal_output.decode())
output1.close()
remote_conn_pre.close()
elif 'AP' in Type:
# Create instance of SSHClient object
remote_conn_pre = paramiko.SSHClient()
# Add untrusted hosts
remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Initiate SSH connection
remote_conn_pre.connect(IP, username=Username, password=Password, port=22)
print("SSH connection established to %s" % IP)
# Use invoke_shell to establish an 'interactive session'
remote_conn = remote_conn_pre.invoke_shell()
print("Interactive SSH session established")
# Strip the initial router prompt
terminal_output = remote_conn.recv(1000)
# Send the router a command
remote_conn.send("\n")
remote_conn.send("sh run\n")
time.sleep(10)
terminal_output = remote_conn.recv(9999999)
print(terminal_output.decode())
output1 = open('%s-%s.txt' % (Host,date), 'w')
output1.write(terminal_output.decode())
output1.close()
remote_conn_pre.close()
elif 'EFW' in Type:
# Create instance of SSHClient object
remote_conn_pre = paramiko.SSHClient()
# Add untrusted hosts
remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Initiate SSH connection
remote_conn_pre.connect(IP, username=Username, password=Password, port=22)
print("SSH connection established to %s" % IP)
# Use invoke_shell to establish an 'interactive session'
remote_conn = remote_conn_pre.invoke_shell()
print("Interactive SSH session established")
# Strip the initial router prompt
terminal_output = remote_conn.recv(1000)
# Send the router a command
remote_conn.send("\n")
remote_conn.send("show full-configuration\n")
time.sleep(10)
terminal_output = remote_conn.recv(9999999)
print(terminal_output.decode())
output1 = open('%s-%s.txt' % (Host,date), 'w')
output1.write(terminal_output.decode())
output1.close()
remote_conn_pre.close()
elif 'LUS' in Type:
# Create instance of SSHClient object
remote_conn_pre = paramiko.SSHClient()
# Add untrusted hosts
remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Initiate SSH connection
remote_conn_pre.connect(IP, username=Username, password=Password, port=22)
print("SSH connection established to %s" % IP)
# Use invoke_shell to establish an 'interactive session'
remote_conn = remote_conn_pre.invoke_shell()
print("Interactive SSH session established")
# Strip the initial router prompt
terminal_output = remote_conn.recv(1000)
# Send the router a command
remote_conn.send("\n")
remote_conn.send("terminal datadump\n")
time.sleep(1)
remote_conn.send("sh run\n")
time.sleep(10)
terminal_output = remote_conn.recv(9999999)
print(terminal_output.decode())
output1 = open('%s-%s.txt' % (Host,date), 'w')
output1.write(terminal_output.decode())
output1.close()
remote_conn_pre.close()
答案 0 :(得分:1)
Python用户通常使用称为PEP-8(https://www.python.org/dev/peps/pep-0008/)的代码编写样式
有些不正确的地方/不合时宜的地方:
1.不要在一行中导入多个软件包。 (如果他们不是来自同一父类)
import csv,subprocess,paramiko,time,socket
至:
import csv
import subprocess
import paramiko
import time
import socket
from datetime import date
2。在逗号后使用空格分隔
['ping.exe',hostname]
至:
['ping.exe', hostname]
3。文件末尾为空行。
添加类和函数看起来确实不错,但是创建类的最重要原因是其可复制性。 您使用此代码段多少次都很重要。
有很多资源可供您学习如何制作课程及其功能
https://www.programiz.com/python-programming/class
快乐编码。 :)