Network Automate - 07. JSON and getpass

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ม.ค. 2025

ความคิดเห็น • 45

  • @srawoorkar2010
    @srawoorkar2010 5 หลายเดือนก่อน

    Simply superb !!! Many thanks for sharing the knowledge.

  • @Chris-qg2un
    @Chris-qg2un 5 ปีที่แล้ว

    These have been a really big help for breaking through my mental barrier with network automation. Thanks so much.

  • @stargategoku
    @stargategoku 4 ปีที่แล้ว

    many thanks for sharing your video and knowledge

  • @fantasylla4385
    @fantasylla4385 6 ปีที่แล้ว +1

    One of the best video in scripting.
    thanks for the video and hope to see more

  • @AhmedAli-dz1hy
    @AhmedAli-dz1hy 4 ปีที่แล้ว

    Please keep up the good job. I am waiting for more videos from you about automation

  • @alekseibespalov5942
    @alekseibespalov5942 6 ปีที่แล้ว

    can't be better... Thank you Greg a lot!!!

  • @sagimor8646
    @sagimor8646 4 ปีที่แล้ว

    Well explained, Thank you Greg.

  • @fiftysix1957
    @fiftysix1957 6 ปีที่แล้ว

    Found these really helpful, Kudos.

  • @dustindean7240
    @dustindean7240 8 ปีที่แล้ว

    Great video keep up the good work.

  • @terryfraser6591
    @terryfraser6591 4 ปีที่แล้ว

    great video. As i am a total beginner to network automation, could you please explain the network topology you used to enable you to configure your devices. Thanks

    • @Grelleum
      @Grelleum  4 ปีที่แล้ว

      I used GNS3 to simulate some routers and switches. For this project I just had some routers connected to a switch so that the switch would have some basic CDP information. The configuration on all devices was minimal, just enough so each device would have a unique hostname, username/password authentication, and connectivity to the switch.

  • @sreerammeka9238
    @sreerammeka9238 6 ปีที่แล้ว

    Is there an ideal method to verify whether the password entered by the user is correct the first time instead of having the user to type in their password twice? Maybe using RADIUS or TACACS+?
    Why can't we print saying "login succeeded" in the first attempt instead of retyping the password?

    • @Grelleum
      @Grelleum  6 ปีที่แล้ว

      Sure you can do that - you would need to make a loop so that you can try again and wrap the connection attempt within a try/except block:
      for attempt in range(3):
      username = get_input('Enter username: ')
      password = getpass('Enter password: ')
      try:
      connection = netmiko.ConnectHandler(
      ip='172.17.3.153',
      device_type='cisco_ios',
      username=username,
      password=password,
      )
      except netmiko.NetMikoAuthenticationException as e:
      print(e)
      else:
      print('Login successful!')
      break

  • @marcinsudak
    @marcinsudak 7 ปีที่แล้ว

    Hi Greg! Nice toturial, but I have a question - is there any chance to put var 'ip' into the connection.send_command() for example - to set hostname at the devices as their ip address, like connection.send_command('hostname', device['ip'])? Of course, this example is not working. Any other way to do this?

    • @Grelleum
      @Grelleum  6 ปีที่แล้ว

      Sorry for the later response on this - I think you want to set the hostname of the device to it's IP address. You would want to create that config and then send the config. 'hostname ' + device['ip']

  • @visethur
    @visethur 8 ปีที่แล้ว

    Greg, can I get these final scripts that you taught in every class

    • @Grelleum
      @Grelleum  7 ปีที่แล้ว

      I have just posted the source files on github at this link:
      github.com/grelleum/youtube-network-automation

    • @visethur
      @visethur 7 ปีที่แล้ว

      Greg, Thanks lot for keeping it in mind and sharing it. I see only from Folder 4. Do you have the scripts before that?

    • @Grelleum
      @Grelleum  7 ปีที่แล้ว +1

      There were no scripts prior to Video # 4.

    • @visethur
      @visethur 7 ปีที่แล้ว

      Thanks Greg for sharing useful scripts. I really appreciate you had it in mind and shared it with us irrespective of your busy schedule.

  • @srsnth
    @srsnth 7 ปีที่แล้ว

    Hi nice video. I ha e a double, What if we want to execute a command on 100 devices at the same time?

    • @Grelleum
      @Grelleum  6 ปีที่แล้ว

      Sorry for the very late response. You can send to each device one at a time, which will take a long time for 100 devices. To send the same command to multiple devices, you would want to search for python multithreading.

  • @ventu2000
    @ventu2000 8 ปีที่แล้ว

    Greg, first off thank you for these great videos. i however encountered an error during the first part of this video.
    File "cmdrunner_dictionary.py", line 11, in
    devices = json.load(dev_file)
    Truncated:
    obj, end = self.scan_once(s, idx)
    ValueError: Expecting property name: line 7 column 1 (char 116)
    I'm very sure I have all of the devices.json copied verbatim. Do you have any suggestions?

    • @Grelleum
      @Grelleum  8 ปีที่แล้ว +1

      Sorry for the delay getting back to you.
      The error appears to be something wrong with the format of the json file.
      I am able to reproduce the error in a general below. I guess you could open the file in an editor and count to where the error is ... line 7 column 1 (char 116) - this means the error is on line # 7, and the 116th character from start of file (or string) which would include all newline, as well as printable characters.
      >>> import json
      # correct json format:
      >>> json.loads('{"a":1 , "b":2}')
      {u'a': 1, u'b': 2}
      # here the key b does not have quotes around it
      >>> json.loads('{"a":1 , b:2}')
      Traceback (most recent call last):
      [truncated]
      obj, end = self.scan_once(s, idx)
      ValueError: Expecting property name: line 1 column 10 (char 9)
      # here there is an extra comma after the second key/value pair.
      >>> json.loads('{"a":1 , "b":2, }')
      Traceback (most recent call last):
      [truncated]
      obj, end = self.scan_once(s, idx)
      ValueError: Expecting property name: line 1 column 17 (char 16)
      >>>

    • @ventu2000
      @ventu2000 8 ปีที่แล้ว

      Greg,
      thank you very much for the reply. It seems like jSon is very, very particular on where commas are located. I've removed unneeded commas and all is working.

  • @AJAL9574
    @AJAL9574 7 ปีที่แล้ว

    whats the drv_file task here ?
    Iam getting this error
    devices = json.load(dev_file)
    File "/usr/lib/python3.4/json/__init__.py", line 268, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
    File "/usr/lib/python3.4/json/__init__.py", line 318, in loads
    return _default_decoder.decode(s)
    File "/usr/lib/python3.4/json/decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    File "/usr/lib/python3.4/json/decoder.py", line 359, in raw_decode
    obj, end = self.scan_once(s, idx)
    ValueError: Expecting property name enclosed in double quotes: line 2 column 3 (cha

    • @Grelleum
      @Grelleum  7 ปีที่แล้ว +1

      The json in the file that "dev_file" points to has some syntax error that causes it to not be parsed correctly. It is specifically stating that the second line in your file has an error - it expects double quotes around the value. Perhaps you forgot to use any quotes, or used single quotes in your json file?

    • @AJAL9574
      @AJAL9574 7 ปีที่แล้ว

      yes u were right I had single quotes only ...also its good to know about the dev_file..thanks a lot ..hope im not bothering u with my questions

    • @Grelleum
      @Grelleum  7 ปีที่แล้ว

      not a bother. please feel free to ask.

  • @dOL6891
    @dOL6891 5 ปีที่แล้ว

    could you please maek video on how to run automatic script with out prompting UID and PWD and shedule time here you are every time hitting enter on file name and prompt user id password i want know how do we run scripted file on schedule

  • @AJAL9574
    @AJAL9574 7 ปีที่แล้ว

    what if i have over a 100 switch or router that I wanna use the script for ....I tried to change the json file to like this :
    switch010={
    "ip" : "192.168.X.X",
    "device_type" : "cisco or hp "
    }
    switch020={
    "ip" : "192.168.X.X",
    "device_type" : "cisco or hp "
    }
    devices = [switch010, switch020]
    the reason why I names them cause I can ran the script for 100 switch but in the result I wont be able to find out which results belong to who!!!! any suggestions ?

    • @Grelleum
      @Grelleum  7 ปีที่แล้ว +1

      I believe in one of the later videos I specify how you can use the base_prompt method from the Netmiko ConnectHandler object. So using this the script can identify which device it is connected to.
      Also, if you wanted to store your device names like that in json, you could do something like this:
      {
      "switch010": {
      "device_type": "cisco_ios",
      "ip": "192.168.1.1"
      },
      "switch020": {
      "device_type": "cisco_ios",
      "ip": "172.16.2.1"
      }
      }

    • @Grelleum
      @Grelleum  7 ปีที่แล้ว +1

      another thought occurs to me: the "ip" parameter passed to netmiko does not need to be in the form of an IP address. It only have to be resolvable to an IP address. So you can use a name instead of an IP address, as long as your system can resolve the name to an IP address. For example, if your system can "ping switch010", then you can use "ip": "switch010", within your json file.

    • @AJAL9574
      @AJAL9574 7 ปีที่แล้ว

      I tried both ways ...no luck .

    • @Grelleum
      @Grelleum  7 ปีที่แล้ว +1

      Several people have had issues with the json. I'm going to do a video on reading in your devices from csv files and spreadsheets. I'll get it done this weekend.

    • @AJAL9574
      @AJAL9574 7 ปีที่แล้ว

      that will be great ,,Thanks a lot Greg!!!!!

  • @bounpasongphommasan6393
    @bounpasongphommasan6393 6 ปีที่แล้ว

    i have this problem how to solve it
    Traceback (most recent call last):
    File "cmdrunner-Dic.py", line 9, in
    pooldata = json.load(json_file)
    File "/usr/lib/python2.7/json/__init__.py", line 291, in load
    **kw)
    File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
    File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be dec

  • @dot11xpert
    @dot11xpert 6 ปีที่แล้ว

    Hi Greg
    Thank you for sharing this, I've tried and followed what you explained, but I ran into an issue that I'm not able to solve (I'm new to Python :) ). It's related to JSON
    $ ./cmdRunner_json.py
    Enter Username: Ashoor
    Enter Password:
    Verify password:
    Traceback (most recent call last):
    File "./cmdRunner_json.py", line 14, in
    devices = json.load(dev_file)
    File "/usr/lib/python2.7/json/__init__.py", line 291, in load
    **kw)
    File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
    File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    File "/usr/lib/python2.7/json/decoder.py", line 380, in raw_decode
    obj, end = self.scan_once(s, idx)
    ValueError: Expecting property name: line 3 column 3 (char 5)
    Maybe you've a hokash bokash command that could resolve it. Thanks alot
    Mahmoud

    • @Grelleum
      @Grelleum  6 ปีที่แล้ว

      You would need to look at your json file to see what you have at line 3 that does not look right.