Python: expected indented block pylance

Viewed 20

I'm newbie on python programming and right now using python 3.10.7 and i tried to create network automation as script below:



from telnetlib import Telnet




host = '172.16.2.1'
username = 'admin'
password = 'admin'


def show_users_telnet():


tn = Telnet(host)

tn.read_until(b'Username: ')
tn.write(username.encode('ascii') + b'\n')


tn.read_until(b'Password: ')
tn.write(password.encode('ascii') + b'\n')

tn.read_until(b'#')
tn.write(b'show users\n')

stdout = tn.read_until(b'#').decode('utf-8')
print(stdout)

show_users_telnet()

and error shown below:

PS C:\Users\User\Documents\Tutorial Python> & C:/Users/User/AppData/Local/Programs/Python/Python310/python.exe "c:/Users/User/Documents/Tutorial Python/telnet1830/main.py"
  File "c:\Users\User\Documents\Tutorial Python\telnet1830\main.py", line 14        
    tn = telnetlib.Telnet(host)
    ^
IndentationError: expected an indented block after function definition on line 11   
PS C:\Users\User\Documents\Tutorial Python>

please help me to solve the problem.

Thanks

1 Answers

Indents and spaces are important in python so, the program should be like this:

from telnetlib import Telnet
host = '172.16.2.1'
username = 'admin'
password = 'admin'
def show_users_telnet():
    tn = Telnet(host)
    tn.read_until(b'Username: ')
    tn.write(username.encode('ascii') + b'\n')
    tn.read_until(b'Password: ')
    tn.write(password.encode('ascii') + b'\n')

    tn.read_until(b'#')
    tn.write(b'show users\n')

    stdout = tn.read_until(b'#').decode('utf-8')
    print(stdout)

show_users_telnet()
Related