Skip to content

Instantly share code, notes, and snippets.

@mangkoran
Last active July 23, 2023 15:43
Show Gist options
  • Save mangkoran/10d8bce8af7895e2c24ad3af7a36aded to your computer and use it in GitHub Desktop.
Save mangkoran/10d8bce8af7895e2c24ad3af7a36aded to your computer and use it in GitHub Desktop.
  1. Write a Bash script that prompts the user to enter her favourite animal and favourite cartoon character and displays the entered output in a message on the terminal.
read -p "What's your favourite animal? " favanimal
read -p "What's your cartoon character? " favchar

echo "Your favourite animal is" $favanimal
echo "Your cartoon character is is" $favchar
  1. Write a Bash script that creates the following directory structure:
   tempdir/
   ├── Exercise1/
   │   ├── templates/
   │   └── static
   └── Exerecise2
       ├── templates/
       └── static
mkdir -p ./tempdir/Exercise{1,2}/{templates,static}
  1. File permission.

    1. What does the command line chmod u+rwx,g+rx,o+rx myfile.sh do?

      Add read write execute to owner, read execute to group, read execute to other

    2. What is the equivalent number format for the command above?

      755

  2. Write command line commands to perform the following Docker operations:

    1. Pull the latest version of the “serverx” Docker image from Docker Hub.

      docker pull docker.io/library/serverx:latest

    2. Run a Docker container named “my-serverx” using the “server” image, mapping host port 1234 to container port 80, and running the container in the background.

      docker run -d --name my-serverx -p 1234:80 docker.io/library/serverx

    3. List all running Docker containers.

      docker ps

    4. Stop and remove the "my-serverx" container.

      docker stop my-serverx && docker rm my-serverx

  3. Answer the following questions based on the Ansible playbook execution output.

PLAY [Example Playbook] ***********************************************

TASK [Display Hostname] ***********************************************
changed: [target_host]

TASK [Display System Information] *************************************
changed: [target_host]

PLAY RECAP ************************************************************
target_host : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0
    1. What is the name of the playbook?

      Example Playbook

    2. Which task in the playbook displayed the hostname of the target host?

      Display Hostname

    3. How many tasks caused changes in the playbook execution?

      2

    4. What is the total number of tasks executed in the playbook?

      2

  1. Fill in the blanks with appropriate names that explains the tasks and correct commands for the following playbook .

---
- name: Configure IP address on one interface and shutdown on second interface
  hosts: CSR1kv
  gather_facts: false
  tasks:
  - name: Configure IP address and bring up interface1
    ios_config:
      parents: "interface GigabitEthernet1"
      lines:
      - ip address 192.168.56.101
      - no shutdown
  - name: Shutdown interface2
    ios_config:
      parents: "interface GigabitEthernet2"
      lines:
      - shutdown
  - name: Display summary of IP interfaces
    ios_command:
      commands:
      - show ip interface brief
    register: output
  1. Answer the following questions on YANG tree model below.
   ietf-interfaces
   ├── rw interfaces
   │   └── rw interface* [name]
   │       ├── rw name              string
   │       ├── rw description?      string
   │       ├── rw type              string
   │       └── rw enabled?          boolean
   └── ro interfaces-state
       └── ro interface* [name]
           ├── ro name              string
           ├── ro type              identityref
           ├── ro admin-status      enumeration
           ├── ro oper-status       enumeration
           └── ro last-change?      yang:date-and-time
    1. What is the name of the module specified in the YANG model?

      ietf-interfaces

    2. Which container in the YANG model defines the configuration data for managing the network interfaces?

      interfaces

    3. What is the data type of the "name" leaf under the "interface" list?

      string

    4. Which leaf under "interfaces-state/interface" provides the administrative status of an interface?

      admin-status

  1. Based on Part 6 in Lab 8.3.6, modify the given Python code to configure a new VLAN on the network device using NETCONF. Write a necessary code to create a VLAN with the following details:

   VLAN ID: 1001
   VLAN Name: Management
   description: Management VLAN for network devices
from ncclient import manager
import xml.dom.minidom

m = manager.connect(
        host="192.168.56.101",
        port=830,
        username="cisco",
        password="cisco123!",
        hostkey_verify=False
    )

netconf_filter = """
<filter>
  <native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native" />
</filter>
"""

netconf_hostname = """
<config>
  <native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
    <hostname>CSR1kv</hostname>
  </native>
</config>
"""

netconf_vlan = """
<config>
  <native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
    <vlan>
      <Vlan>
        <id>1001</id>
        <name>Management</name>
        <description>Management VLAN for network devices</description>
      </Vlan>
    </vlan>
  </native>
</config>
"""

# netconf_reply = m.get_config(source="running", filter=netconf_filter) # get config
netconf_reply = m.edit_config(target="running", config=netconf_vlan)

print(xml.dom.minidom.parseString(netconf_reply.xml).toprettyxml())
  1. Find all SIX (6) errors in the NETCONF Python code below.
from ncclient import leader # 1. leader -> manager

m = leader.connect( # 2. leader -> manager
      host="192.168.56.101",
      port=880, # 3. 880 -> 830
      username="cisco",
      password="cisco123!",
      hostkey_verify=False
    )
print("Supported Capabilities:")
for cap in m.server_capabilities:
  print(cap)

netconf_reply = m.get_config(source="run")
print(netconf_reply.xml)

netconf_filter = """
<filter>
  <native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native" />
""" # 4. </filter>
netconf_reply = m.get_config(source="run", filter=netconf_filter)
print(netconf_reply.xml)

netconf_hostname = """
<config>
  <native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
    <hostname>CSR1kv
  </native>
</config>
""" # 5. <native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native" />
    # 6. </hostname>
netconf_reply = m.edit_config(target="running", config=netconf_hostname)
print(netconf_reply.xml)
  1. The following RESTCONF Python code produces this output.
import requests

api_url = "https://example.com/restconf/data/ietf-interfaces:interfaces"
headers = {
  "Accept": "application/yang-data+json",
  "Content-Type": "application/yang-data+json"
}

response = requests.get(api_url, headers=headers)

print(response)
print(response.json())
<Response [404]>
{'error': {'code': 404, 'message': 'Resource Not Found'}}
    1. Which RESTCONF endpoint is being accessed in the code?

      interfaces

    2. What is the purpose of the 'Accept' header in the code?

      Which content types the client is able to understand

    3. What is the expected format of the request payload?

      application/yang-data+json

    4. What does <Response [404]> means?

      Server cannot find requested resource

    5. What would be the HTTP response code if the RESTCONF operation was successful?

      200

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment