2
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?

スイッチの設定でハイフンで省略されているVLAN番号を省略しない形にして見やすくするスクリプト

Posted at

なぜ作ったか?

ネットワーク機器でVLANの設定をする際に、"3-10,13,17-22" のような形式で書かれているが、これをほどきたくなる瞬間がよくあるからw

スクリプト本体

vlan-split.py
#!/usr/local/bin/python3

import sys
import argparse

def expand_ranges(input_string):
    # 結果を格納するリスト
    result = []
    # カンマで区切って処理
    for part in input_string.split(','):
        # 範囲のチェック
        if '-' in part:
            start, end = map(int, part.split('-'))
            # 範囲内の数字を追加
            result.extend(range(start, end + 1))
        else:
            # 単一の数字を追加
            result.append(int(part))
    return result

if __name__ == "__main__":
    # 引数パーサーの設定
    parser = argparse.ArgumentParser(description="Expand ranges in input string")
    parser.add_argument("-n", action="store_true", help="Use newline instead of comma as separator")
    args = parser.parse_args()

    # 標準入力からデータを取得
    input_string = sys.stdin.read().strip()
    expanded_list = expand_ranges(input_string)

    # 出力を選択
    separator = "\n" if args.n else ","
    print(separator.join(map(str, expanded_list)))

実行例

オプションなし
% echo "3-5,10" | vlan-split.py
3,4,5,10
-n オプション
% echo "3-5,10" | vlan-split.py -n
3
4
5
10
%

2
1
0

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
2
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?