1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

スイッチのIFに設定するマクロを生成するpythonプログラム

Posted at
IF_setting.py
class Interface(object):
    def __init__(self, int_id):
        self.__int_id = int_id
        self.__command_list = []

    @property
    def int_id(self):
        pass
    @int_id.getter
    def int_id(self):
        return self.__int_id

    @property
    def command_list(self):
        pass
    @command_list.setter
    def command_list(self, command):
        self.__command_list.append(command)
    pass
    @command_list.getter
    def command_list(self):
        return self.__command_list


import os
current_dir = os.getcwd()
read_file_name = '/コマンド.txt' 
output_file_name = '/IFに設定するマクロ.ttl'

read_file = current_dir + read_file_name
output_file = current_dir + output_file_name

replace_list = []
context_list = []
with open(read_file, mode='r', encoding='utf-8') as f:
    line = f.readlines()
    #コマンド.txtから読み取り、改行コードを削除
    for s in line:
        if '\n' in s:
            text = s.replace('\n','')
            replace_list.append(text)
        else:
            replace_list.append(s)

interface_list = []
for b in replace_list:
    context_list = b.split('\t')
    interface = Interface(context_list[0])
    #コマンドが入っているのは二つ目から
    for com in context_list[1:]:
        interface.command_list = com
    interface_list.append(interface)

with open(output_file, mode='w', encoding='utf-8') as f:
    for int in interface_list:
        f.write('sendln \'int ')
        f.write(int.int_id)
        f.write('\'')
        f.write('\n')

        for command in int.command_list:
            f.write('wait \'(config-if)#\'')
            f.write('\n')
            f.write('sendln \'')
            f.write(command)
            f.write('\'')
            f.write('\n')
        f.write('wait \'(config-if)#\'')
        f.write('\n')
        f.write('sendln \'exit\'')
        f.write('\n')
        f.write('wait \'(config)#\'')
        f.write('\n')

#注意事項
コマンド.txtのインターフェース名とコマンド、コマンドとコマンドの間はTAB(タブ)で区切られていないといけない。
下記写真のようにエクセルを使用して、コマンド.txtにコピペすれば、TABで区切ることができる。
スクリーンショット 2021-12-11 23.06.07.png

#OUTPUT
コマンド.txtとpythonプログラムを同じフォルダに配置し、下記コマンドを実行

bash
python3 IF_setting.py
コマンド.txt
Gi1/1	ip address 192.168.0.1	no shut
Gi1/2	ip address 192.168.0.2	no shut
Gi1/3	ip address 192.168.0.3	no shut
IFに設定するマクロ.ttl
sendln 'int Gi1/1'
wait '(config-if)#'
sendln 'ip address 192.168.0.1'
wait '(config-if)#'
sendln 'no shut'
wait '(config-if)#'
sendln 'exit'
wait '(config)#'
sendln 'int Gi1/2'
wait '(config-if)#'
sendln 'ip address 192.168.0.2'
wait '(config-if)#'
sendln 'no shut'
wait '(config-if)#'
sendln 'exit'
wait '(config)#'
sendln 'int Gi1/3'
wait '(config-if)#'
sendln 'ip address 192.168.0.3'
wait '(config-if)#'
sendln 'no shut'
wait '(config-if)#'
sendln 'exit'
wait '(config)#'

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?