Python Forum
First time user - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: First time user (/thread-33887.html)



First time user - cris - Jun-07-2021

Help Please. This is my first time attempting Python. My testing lab has 4 Ethernet interfaces E0/0-3 on my Router. I am writing a script to shutdown all interfaces at 1900 hrs.
My script only shuts down E0/0 no others. Please would some tell me what I am doing wrong, and possibly giving me some pointers on how to input the time variables? I have uploaded my script for review.

netshud.py
from netmiko import ConnectHandler
import subprocess

Network_Device ={"host": "192.168.2.119",
                 "username": "admin",
                 "password": "cisco",
                 "device_type": "cisco_ios",
                 "secret": "cisco",

     }

Connent_to_Device = ConnectHandler(**Network_Device)
Connent_to_Device.enable()

First_Port = 0
Last_Port = 3

To_Execute = Connent_to_Device.send_command("sh ip interface brief")
print(To_Execute)

for Port in range(First_Port, (Last_Port +1)):
    List_of_Commands = ["interface eth 0/0" + str(Port), "shut"]
    To_Execute = Connent_to_Device.send_config_set(List_of_Commands)
print(To_Execute)

To_Execute = Connent_to_Device.send_command("sh ip interface brief")
print(To_Execute)
Thank
Cris


RE: First time user - Yoriz - Jun-07-2021

list_of_commands on each loop you currently have as
FIRST_PORT = 0
LAST_PORT = 3

for number in range(FIRST_PORT, (LAST_PORT +1)):
    list_of_commands = ["interface eth 0/0" + str(number), "shut"]
    print(list_of_commands)
which give the following commands
Output:
['interface eth 0/00', 'shut'] ['interface eth 0/01', 'shut'] ['interface eth 0/02', 'shut'] ['interface eth 0/03', 'shut']
In your description you stated E0/0-3, so maybe you want the following
FIRST_PORT = 0
LAST_PORT = 3

for number in range(FIRST_PORT, (LAST_PORT +1)):
    list_of_commands = [f"interface eth 0/{number}", "shut"]
    print(list_of_commands)
which will give
Output:
['interface eth 0/0', 'shut'] ['interface eth 0/1', 'shut'] ['interface eth 0/2', 'shut'] ['interface eth 0/3', 'shut']