下午好,
我的请求非常奇怪。我正在尝试编写脚本以帮助配置vlans工作。我想要它做的是阅读第1行完整的脚本。然后返回并阅读第2行...完成脚本的其余部分,依此类推,直到完成。
这是我当前的配置
file=open("C:\\Users\\KM003308023\\final.txt","w")
file.write("")
file=open("C:\\Users\\KM003308023\\final.txt","a")
interface= open ("C:\\Users\\KM003308023\\int.txt")
pull= open ("C:\\Users\\KM003308023\\int.txt")
y= str(input("enter Port description:"))
p= str(input("Enter DATA vlan:"))
x= str(input("Enter voice vlan:"))
portconfig= "\n switchport port-security \n switchport port-security maximum 3 \n switchport port-security aging time 30 \n switchport port-security violation restrict \n switchport port-security aging type inactivity \n no logging event link-status \n no cdp enable \n spanning-tree portfast \n spanning-tree bpduguard enable \n no shut\n!\n"
output=""
for j in pull.readlines():
output=(output + "default int " + j + "!\n")
for f in interface.readlines():
output =(output + "int " + f +"description " + y + "\n switchport \n switchport access vlan " + p + "\n switchport mode access \n switchport voice vlan " + x + portconfig)
file.write(output)
file.close()
我希望它能做什么
当前输出的示例:
default int fa 2/0/15
!
default int fa 7/0/1
!
default int fa 7/0/8
!
int fa 2/0/15
description UHC_VLAN2
switchport
switchport access vlan 52
switchport mode access
switchport voice vlan 53
switchport port-security
switchport port-security maximum 3
switchport port-security aging time 30
switchport port-security violation restrict
switchport port-security aging type inactivity
no logging event link-status
no cdp enable
spanning-tree portfast
spanning-tree bpduguard enable
no shut
!
int fa 7/0/1
description UHC_VLAN2
switchport
switchport access vlan 52
switchport mode access
switchport voice vlan 53
switchport port-security
switchport port-security maximum 3
switchport port-security aging time 30
switchport port-security violation restrict
switchport port-security aging type inactivity
no logging event link-status
no cdp enable
spanning-tree portfast
spanning-tree bpduguard enable
no shut
!
int fa 7/0/8
description UHC_VLAN2
switchport
switchport access vlan 52
switchport mode access
switchport voice vlan 53
switchport port-security
switchport port-security maximum 3
switchport port-security aging time 30
switchport port-security violation restrict
switchport port-security aging type inactivity
no logging event link-status
no cdp enable
spanning-tree portfast
spanning-tree bpduguard enable
no shut
!
What i would like to see:
default int fa 2/0/15
!
int fa 2/0/15
description UHC_VLAN2
switchport
switchport access vlan 52
switchport mode access
switchport voice vlan 53
switchport port-security
switchport port-security maximum 3
switchport port-security aging time 30
switchport port-security violation restrict
switchport port-security aging type inactivity
no logging event link-status
no cdp enable
spanning-tree portfast
spanning-tree bpduguard enable
no shut
!
default int fa 7/0/1
!
int fa 7/0/1
description UHC_VLAN2
switchport
switchport access vlan 52
switchport mode access
switchport voice vlan 53
switchport port-security
switchport port-security maximum 3
switchport port-security aging time 30
switchport port-security violation restrict
switchport port-security aging type inactivity
no logging event link-status
no cdp enable
spanning-tree portfast
spanning-tree bpduguard enable
no shut
!
等等,直到它用完了。
我是新手,所以试着继续拿到我的戒指。
答案 0 :(得分:0)
您在两个不同的变量pull
和interface
中打开相同的文件。
只需在一个变量中打开一次。
您不需要将input
的结果类型转换为str
,因为它会返回一个字符串。
您无需打开文件" final.txt"两次(一次用" w",第二次用" a")。
您不应在变量中累积输出,然后立即全部写入。
最后,避免写很长的句子;拆分它们以提高可读性。
这给了(我无法测试它,因为我无法访问您的文件)以下代码:
y= input("enter Port description:")
p= input("Enter DATA vlan:")
x= input("Enter voice vlan:")
portconfig = "\n".join([
" switchport port-security ",
" switchport port-security maximum 3 ",
" switchport port-security aging time 30 ",
" switchport port-security violation restrict ",
" switchport port-security aging type inactivity ",
" no logging event link-status ",
" no cdp enable ",
" spanning-tree portfast ",
" spanning-tree bpduguard enable ",
" no shut",
"!\n"
])
with open("C:\\Users\\KM003308023\\final.txt","w") as out_file:
out_file.write("")
with open ("C:\\Users\\KM003308023\\int.txt","r") as inp_file:
for j in inp_file.readlines():
out_file.write("default int " + j)
out_file.write("!\n")
out_file.write("int " + j)
out_file.write("description " + y)
out_file.write("\n switchport \n switchport access vlan " + p)
out_file.write("\n switchport mode access \n switchport voice vlan " + x)
out_file.write(portconfig)