5 - Basic Scripts
5 - Basic Scripts
Basic Scripts
Generally speaking, script is a regular file. This file stores the sequence of commands that
you want to execute.
Let’s start with basic script and print several strings on standard output. To do this, you
need to create an access_template.py file with this content:
$ python access_template.py
switchport mode access
switchport access vlan 5
switchport nonegotiate
spanning-tree portfast
spanning-tree bpduguard enable
Example of access_template_argv.py:
from sys import argv
interface = argv[1]
vlan = argv[2]
print('interface {}'.format(interface))
print('\n'.join(access_template).format(vlan))
27
Script output:
$ python access_template_argv.py Gi0/7 4
interface Gi0/7
switchport mode access
switchport access vlan 4
switchport nonegotiate
spanning-tree portfast
spanning-tree bpduguard enable
Arguments that have been passed to script are substituted as values in template. Several
points need to be clarified:
• argv is a list
• all arguments are in list and represented as strings
• argv contains not only arguments that passed to script but also name of script
itself.
User input
Sometimes it is necessary to get information from user. For example, request a password.
The input function is used to get information from the user:
interface = input('Enter interface type and number: ')
vlan = input('Enter VLAN number: ')
First two lines request information from user. Line print('\n' + '-' * 30) is used to visually
separate information request from the output.
28
Script output:
$ python access_template_input.py
Enter interface type and number: Gi0/3
Enter VLAN number: 55
------------------------------
interface Gi0/3
switchport mode access
switchport access vlan 55
switchport nonegotiate
spanning-tree portfast
spanning-tree bpduguard enable
29
Tasks
Task 5.1 :
The task contains a dictionary with information about different devices.
The task contains a dictionary with information about different devices. In the task you
need: ask the user to enter the device name (r1, r2 or sw1). Print information about the
corresponding device to standard output (information will be in the form of a dictionary).
london_co = {
"r1": {
"location": "21 New Globe Walk",
"vendor": "Cisco",
"model": "4451",
"ios": "15.4",
"ip": "10.255.0.1"
},
"r2": {
"location": "21 New Globe Walk",
"vendor": "Cisco",
"model": "4451",
"ios": "15.4",
"ip": "10.255.0.2"
},
"sw1": {
"location": "21 New Globe Walk",
"vendor": "Cisco",
"model": "3850",
"ios": "3.6.XE",
"ip": "10.255.0.101",
"vlans": "10,20,30",
"routing": True
}
}
30
Task 5.1a :
Modify the script from task 5.1 so that, in addition to the device name, the script requested
and then printed the device parameter as well.
Display information about the corresponding parameter of the specified device.
Task 5.1b :
Modify the script from task 5.1a so that, when requesting a parameter, a list of possible
parameters was displayed. The list of parameters must be obtained from the dictionary,
rather than written manually.
Display information about the corresponding parameter of the specified device.
Task 5.1c :
Copy and modify the script from task 5.1b so that when you request a parameter that is
not in the device dictionary, the message ‘There is no such parameter’ is displayed. The
assignment applies only to the parameters of the devices, not to the devices themselves.
31
Task 5.2 :
Ask the user to enter the IP network in the format: 10.1.1.0/24.
Then print information about the network and mask in this format:
Network:
10 1 1 0
00001010 00000001 00000001 00000000
Mask:
/24
255 255 255 0
11111111 11111111 11111111 00000000
Hint: You can get the mask in binary format like this:
"1" * 28 + "0" * 4
Task 5.2a :
Copy and modify the script from task 5.2 so that, if the user entered a host address rather
than a network address, convert the host address to a network address and print the
network address and mask, as in task 5.2.
An example of a network address (all host bits are equal to zero):
• 10.0.1.0/24
• 190.1.0.0/16
Host address example:
• 10.0.1.1/24 - host from network 10.0.1.0/24
• 10.0.5.195/28 - host from network 10.0.5.192/28
If the user entered the address 10.0.1.1/24, the output should look like this:
Network:
10 0 1 0
00001010 00000000 00000001 00000000
Mask:
/24
255 255 255 0
11111111 11111111 11111111 00000000
The network address can be calculated from the binary host address and the netmask. If
the mask is 28, then the network address is the first 28 bits host addresses + 4 zeros. For
example, the host address 10.1.1.195/28 in binary will be:
bin_ip = "00001010000000010000000111000011"
Then the network address will be the first 28 characters from bin_ip + 0000 (4 because in
total there can be 32 bits in the address, and 32 - 28 = 4)
00001010000000010000000111000000
32