Advertisement
Guest User

Python Telnet D-Link

a guest
Jun 6th, 2017
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. import sys
  2. import time
  3. import telnetlib
  4. from ipaddress import ip_network
  5.  
  6. f = open('switches.log', 'w')
  7.  
  8. HOSTS = list(ip_network('192.168.0.0/23').hosts())
  9. # HOSTS = ['192.168.111.55', '192.168.111.104', '192.168.111.120']
  10. LOGIN = 'USER'
  11. PASSWORD = 'PASS'
  12.  
  13.  
  14. def cmd(value):
  15.     return value.encode('ascii') + b'\n'
  16.  
  17.  
  18. def do(HOST, f):
  19.     try:
  20.         SUCCESS = False
  21.         tn = telnetlib.Telnet(host=HOST, port=23, timeout=5)
  22.         # tn.set_debuglevel(1)
  23.  
  24.         tn.expect([b'UserName:'], 2)
  25.         tn.write(cmd(LOGIN))
  26.         tn.expect([b'PassWord:'], 2)
  27.         tn.write(cmd(PASSWORD))
  28.         a, b, fail = tn.expect([b'Fail!:'], 2)
  29.         if b'Fail' in fail:
  30.             tn.expect([b'UserName:'], 2)
  31.             tn.write(cmd(LOGIN))
  32.             tn.expect([b'PassWord:'], 2)
  33.             tn.write(cmd(PASSWORD))
  34.             a, b, fail = tn.expect([b'Fail!:'], 2)
  35.             if b'Fail' in fail:
  36.                 print(HOST + ' - Fail!')
  37.                 f.write(HOST + ' - Fail!\n')
  38.                 sys.exit(0)
  39.  
  40.         tn.write(cmd('enable admin'))
  41.         tn.expect([b'PassWord:'], 2)
  42.         tn.write(cmd('ADMINPASS'))
  43.  
  44.         tn.expect([b'admin#', b'4#', b'5#'], 5)
  45.         tn.write(cmd('config igmp_snooping multicast_vlan_group_profile base add 1.2.3.4-5.6.7.8'))
  46.         a, b, success = tn.expect([b'Success'], 5)
  47.         if b'Success' in success:
  48.             SUCCESS = True
  49.             tn.write(cmd('show vlan vlanid 1'))
  50.             tn.expect([b'admin#', b'4#', b'5#'], 5)
  51.             tn.write(cmd('save all'))
  52.             tn.expect([b'admin#', b'4#', b'5#'], 5)
  53.             tn.write(cmd('logout'))
  54.             print(HOST + ' - Success!')
  55.             f.write(HOST + ' - Success!\n')
  56.             return 0
  57.         tn.write(cmd('config igmp_snooping multicast_vlan_group TV add 1.2.3.4-5.6.7.8'))
  58.         a, b, success = tn.expect([b'Success'], 5)
  59.         if b'Success' in success:
  60.             SUCCESS = True
  61.             tn.write(cmd('show vlan vlanid 1'))
  62.             tn.expect([b'admin#', b'4#', b'5#'], 5)
  63.             tn.write(cmd('save all'))
  64.             tn.expect([b'admin#', b'4#', b'5#'], 5)
  65.             tn.write(cmd('logout'))
  66.             f.write(HOST + ' - Success!\n')
  67.             print(HOST + ' - Success!')
  68.             return 0
  69.         if not SUCCESS:
  70.             tn.expect([b'admin#', b'4#', b'5#'], 5)
  71.             tn.write(cmd('logout'))
  72.             print(HOST + ' - Fail!')
  73.             f.write(HOST + ' - Fail!\n')
  74.             return 0
  75.     except:
  76.         print(HOST + ' - Fail')
  77.         f.write(HOST + ' - Fail!\n')
  78.  
  79.  
  80. for HOST in HOSTS:
  81.     do(str(HOST), f)
  82. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement