Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """Sample controller with all its actions protected."""
  3. from tg import expose, flash, redirect, url, response, tmpl_context, validate
  4. from tg.decorators import with_trailing_slash
  5. from pylons.i18n import ugettext as _, lazy_ugettext as l_
  6. from repoze.what.predicates import has_permission, in_group, in_any_group
  7.  
  8. from supporttools.lib.base import BaseController
  9. from supporttools.widgets.dslcheck_form import dslcheck_form
  10.  
  11. from sqlalchemy.sql.functions import current_date
  12.  
  13. from pexpect import spawn
  14. from re import search
  15.  
  16. __all__ = ['DSLCheckController']
  17.  
  18.  
  19. class DSLCheckController(BaseController):
  20. """Sample controller-wide authorization"""
  21.  
  22. # The predicate that must be met for all the actions in this controller:
  23. allow_only = in_any_group('Admin', 'Accounting', 'Support', 'Operations',
  24. msg=l_('This controller restricted to the following groups: Admin/Accounting/Support/Operations'))
  25. prompt = 'Router#'
  26.  
  27. @with_trailing_slash
  28. @expose('supporttools.templates.dslcheck')
  29. def index(self, **kw):
  30. tmpl_context.form = dslcheck_form
  31.  
  32. p = self.Router()
  33.  
  34. p.sendline('show vpdn tunnel')
  35. p.expect(self.prompt)
  36. tunnel = p.before
  37.  
  38. showint = showipint = user = None
  39. if 'username' in kw:
  40. p.sendline('show vpdn session all username ' + kw['username'])
  41. p.expect(self.prompt)
  42. user = p.before
  43.  
  44. # look to see if we have a Vi
  45. rxp = search('Vi[0-9]*', user)
  46. if rxp:
  47. interface = rxp.group()
  48. p.sendline('show interface ' + interface)
  49. p.expect(self.prompt)
  50. showint = p.before
  51.  
  52. p.sendline('show ip interface ' + interface)
  53. p.expect(self.prompt)
  54. showipint = p.before
  55.  
  56. return dict(page='index', tunnel=tunnel, user=user, showint=showint, showipint=showipint, value=kw)
  57.  
  58. def Router(self):
  59. prompt = self.prompt
  60. passwd = 'password:'
  61. sshkey = 'Are you sure you want to continue connecting (yes/no)?'
  62. p = spawn('/usr/bin/ssh -l Username Router')
  63. loggedin = 0
  64. while not loggedin:
  65. i = p.expect([ sshkey , passwd , prompt ])
  66. if i == 0: # sshkey
  67. p.sendline('yes')
  68. elif i == 1: # passwd
  69. p.sendline('Password')
  70. elif i == 2: # prompt
  71. loggedin = 1
  72. else: # oh the humanity
  73. flash('Something bad happened. Tell The Administrator.')
  74. redirect('/')
  75. # so now we're logged in
  76. p.sendline('terminal length 0')
  77. p.expect(prompt)
  78. p.sendline('terminal width 0')
  79. p.expect(prompt)
  80. return p
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement