3
2

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 5 years have passed since last update.

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

Last updated at Posted at 2017-01-17

もうすぐ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

さいごに

オチはありません。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?