Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
First time user
#1
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
Larz60+ write Jun-07-2021, 02:52 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
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']
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020