Help us understand the problem. What is going on with this article?

Junos On-box Python: IPv4からのCommitを禁止する(ネタ)

More than 1 year has passed since last update.

もうすぐIPv6元年!?ということで() 一発ネタです。
IPv4からのコミットなんて禁止しちゃいましょう。

Junos 16.1R1以降で動作します。
参考: JunosのOn-box Pythonを使ってみる #2 Commit Script

設定

commit-script.junos
set system scripts language python
set system scripts commit file commit-script.py

スクリプト

/var/db/scripts/commit/commit-script.py
from junos import Junos_Context
from jnpr.junos import Device
import jcs

def main():
  login_name = Junos_Context['user-context']['login-name']
  host_name = Junos_Context['hostname']
  product_name = Junos_Context['product']
  tty_dev = Junos_Context['tty'].replace('/dev/','')

  dev = Device()
  dev.open()

  sessions = dev.rpc.get_system_users_information(no_resolve=True)

  user_ip = None

  for session in sessions.getiterator("user-entry"):
    if session.find("tty").text.strip() == tty_dev:
      user_ip = session.find("from").text.strip()

  if user_ip:
    if user_ip.count('.'):
      jcs.emit_error("Commit through IPv4 is prohibited!!")

if __name__ == '__main__':
  main()

結果

output.txt
[edit]
root@vmx1# run show system users no-resolve
 2:18PM  up 22 days,  4:15, 1 users, load averages: 0.36, 0.35, 0.33
USER     TTY      FROM                              LOGIN@  IDLE WHAT
root     pts/0    192.168.0.10                     2:18PM      - cli

[edit]
root@vmx1# commit
error: Commit through IPv4 is prohibited!!
error: 1 error reported by commit scripts
error: commit script failure
output-v6.txt
[edit]
root@vmx1# run show system users no-resolve
 2:47PM  up 22 days,  4:44, 1 users, load averages: 0.34, 0.30, 0.27
USER     TTY      FROM                              LOGIN@  IDLE WHAT
root     pts/1    fe80::250:560f:fcb6:4397         2:47PM      - cli

[edit]
root@vmx1# commit
commit complete

さいごに

オチはありません。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away