2017-08-24 19 views
-1

下午好,腳本讀取1行然後移動到第二個腳本完成並重復

我有一個很奇怪的請求。我正在嘗試腳本來幫助配置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() 

我想它做的是從int.txt

  • 輸出默認INT XXX
  • 繼續第二部分

    1. 拉readline1
    2. line 1
    3. whe再次readlines1並輸出所有需要的垃圾。電流輸出的

    例如:

    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 
    ! 
    

    ,以此類推,直到它耗盡線。

    我是新來的,所以試圖仍然得到我的Barrings。

  • +0

    這個問題可能寫得不好,但不添加註釋downvoting它,解釋什麼是缺失以及如何編輯問題只是粗魯。 – ands

    +0

    你可以解釋一下這些步驟(1.從int.txt中拉出readline1,2.輸出默認的int xxx,3. ...,4. ....,5. ....)稍微好一點。我不明白你想要什麼。 – ands

    回答

    0

    您可以用兩個不同的變量pullinterface打開相同的文件。 只需在一個變量中打開一次即可。

    您不需要將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) 
    
    相關問題