Hi guys how are you 
please why When I run this code it does not work but when I change the (   tn.write(b"config t \n")
tn.write(b"router ospf 10 \n") to the show command it works fine ??
****************************************************************************************************************
import sys    
import telnetlib
import getpass
host=["192.168.1.164","192.168.1.169"]
devno=len(host)
user='ali'
Password='cisco'
for i in range(devno):
    print('*'*50 + ' Connected to device : '+str(host[i]) +'*'*50)
    tn=telnetlib.Telnet(host[i])
    tn.read_until(b'Username: ')
    tn.write(user.encode('ascii')+ b'\n')
    tn.read_until(b'Password')
    tn.write(Password.encode('ascii')+b'\n')
    tn.write(b"config t \n")         if I change to tn.write(b"show ip route \n") it works fine 
    tn.write(b"router ospf 10 \n")  if I change to tn.write(b"show ip inter bri \n") it works fine
    tn.write(b'  exit\n')
    print(tn.read_all().decode('ascii'))

Dani AI

Generated

Short answer: show commands often succeed from the current exec level, while configuration commands require elevation and a different prompt state. The script needs to ensure it is in the device's privileged mode, wait for each prompt change before sending the next line, and handle paging so long command output does not stop the session.

Practical checklist to follow (in order):

  • Make sure the session is elevated to privileged exec and that any enable password is supplied when requested.
  • Wait for the device prompt after each command before sending the next one. Match the prompt text the device uses (user vs. privileged vs. config) and only proceed after you see it.
  • Disable output paging so multi‑page output does not pause the session.
  • Read and inspect the command output after each command for error messages (incomplete/invalid/authorization errors).
  • Close the connection cleanly when done.

If implementing all of the above in raw telnet becomes fragile, use a device-aware library that handles prompts, paging and mode transitions for you. For low‑level reference see the Python telnetlib documentation and for a higher‑level, network device‑friendly option see Netmiko:

Python telnetlib docs
Netmiko (device libraries)

Note: already demonstrated that adding the privilege step changed behavior. Focus next on strict prompt matching and reading responses between commands; that will make configuration commands reliable.

this code is working fine but when I want to send configuration it does not accespt ??

import sys
import telnetlib
import getpass

host=["192.168.1.164","192.168.1.169"]
devno=len(host)
user='ali'
Password='cisco'

 for i in range(devno):
    print('*'*50 + ' Connected to device : '+str(host[i]) +'*'*50)
    tn=telnetlib.Telnet(host[i])
    tn.read_until(b'Username: ')
    tn.write(user.encode('ascii')+ b'\n')
    tn.read_until(b'Password')
    tn.write(Password.encode('ascii')+b'\n')
    tn.write(b"en" + b"\n")
    tn.write(b"terminal length 0\n")
    tn.write(b"show run\n")
    tn.write(b' show ip route\n')
    tn.write(b'  exit\n')
    print(tn.read_all().decode('ascii'))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.