SyntaxError: invalid syntax (windows)

Discussion in 'Python' started by Python Newsgroup, Mar 25, 2009.

  1. I'm a total newbe to scripting not to mention python. However I was able to
    successfully create a telnet script to initiate login, initiate tftp, exit,
    exit, confirm and close session. Frustrated, possibly causing my own misery.
    I replace the sript the script with the standard example.

    import getpass
    import sys
    import telnetlib

    HOST = "remote linux"
    user = raw_input("Enter your remote account: ")
    password = getpass.getpass()

    tn = telnetlib.Telnet(HOST)

    tn.read_until("login: ")
    tn.write(user + "\n")
    if password:
    tn.read_until("Password: ")
    tn.write(password + "\n")

    tn.write("ls\n")
    tn.write("exit\n")

    print tn.read_all()

    Regardless of the script content, running in windows I constently get this
    SyntaxError:

    C:\Python30>python c:\Python30\scripts\telnet.py
    File "c:\Python30\scripts\telnet.py", line 20
    print tn.read_all()
    ^
    SyntaxError: invalid syntax

    C:\Python30>

    The same script works fine from linux.

    I have also notices some other slight differences: this is my original
    script that runs and completes but only if I comment out print. Also tried
    to run debug without success in windows again this worked fine in linux. To
    run this script in linux I also had to remove the b syntax in the "b" in the
    perentesis

    import telnetlib
    # import pdb

    HOST = "HP switch"

    tn = telnetlib.Telnet(HOST)

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

    pdb.set_trace()

    tn.read_until(b'HP switch# ')
    tn.write(b' sh time\n')

    tn.read_until(b'HP switch# ')
    tn.write(b'exit\n')

    tn.read_until(b'HP switch> ')
    tn.write(b'exit\n')

    tn.read_until(b'Do you want to log out [y/n]? ')
    tn.write(b'y')

    print tn.read_all()

    Any guidance would be appreciated.

    Delrey
     
    Python Newsgroup, Mar 25, 2009
    #1
    1. Advertising

  2. Python Newsgroup

    Gary Herron Guest

    Python Newsgroup wrote:
    > I'm a total newbe to scripting not to mention python. However I was
    > able to successfully create a telnet script to initiate login,
    > initiate tftp, exit, exit, confirm and close session. Frustrated,
    > possibly causing my own misery. I replace the sript the script with
    > the standard example.
    >
    > import getpass
    > import sys
    > import telnetlib
    >
    > HOST = "remote linux"
    > user = raw_input("Enter your remote account: ")
    > password = getpass.getpass()
    >
    > tn = telnetlib.Telnet(HOST)
    >
    > tn.read_until("login: ")
    > tn.write(user + "\n")
    > if password:
    > tn.read_until("Password: ")
    > tn.write(password + "\n")
    >
    > tn.write("ls\n")
    > tn.write("exit\n")
    >
    > print tn.read_all()
    >
    > Regardless of the script content, running in windows I constently get
    > this SyntaxError:
    >
    > C:\Python30>python c:\Python30\scripts\telnet.py
    > File "c:\Python30\scripts\telnet.py", line 20
    > print tn.read_all()
    > ^
    > SyntaxError: invalid syntax
    >
    > C:\Python30>



    There's the clue:

    In python 3.X, print is a function call
    print(tn.read_all() )
    with lots of formatting and line-ending features

    In python 2.X, print is a statement:
    print tn.read_all()

    If you want one script to work for both Windows and Linux, then you
    should probably
    be running the same version of Python on each. At least both versions
    should be on
    the same side for the Python 2.x/3.x version change.


    Gary Herron



    >
    > The same script works fine from linux.
    >
    > I have also notices some other slight differences: this is my original
    > script that runs and completes but only if I comment out print. Also
    > tried to run debug without success in windows again this worked fine
    > in linux. To run this script in linux I also had to remove the b
    > syntax in the "b" in the perentesis
    >
    > import telnetlib
    > # import pdb
    >
    > HOST = "HP switch"
    >
    > tn = telnetlib.Telnet(HOST)
    >
    > tn.read_until(b'Password: ')
    > tn.write(b'password\n')
    >
    > pdb.set_trace()
    >
    > tn.read_until(b'HP switch# ')
    > tn.write(b' sh time\n')
    >
    > tn.read_until(b'HP switch# ')
    > tn.write(b'exit\n')
    >
    > tn.read_until(b'HP switch> ')
    > tn.write(b'exit\n')
    >
    > tn.read_until(b'Do you want to log out [y/n]? ')
    > tn.write(b'y')
    >
    > print tn.read_all()
    >
    > Any guidance would be appreciated.
    >
    > Delrey
    > --
    > http://mail.python.org/mailman/listinfo/python-list
     
    Gary Herron, Mar 25, 2009
    #2
    1. Advertising

  3. Python Newsgroup

    MRAB Guest

    Python Newsgroup wrote:
    > I'm a total newbe to scripting not to mention python. However I was able
    > to successfully create a telnet script to initiate login, initiate tftp,
    > exit, exit, confirm and close session. Frustrated, possibly causing my
    > own misery. I replace the sript the script with the standard example.
    >
    > import getpass
    > import sys
    > import telnetlib
    >
    > HOST = "remote linux"
    > user = raw_input("Enter your remote account: ")
    > password = getpass.getpass()
    >
    > tn = telnetlib.Telnet(HOST)
    >
    > tn.read_until("login: ")
    > tn.write(user + "\n")
    > if password:
    > tn.read_until("Password: ")
    > tn.write(password + "\n")
    >
    > tn.write("ls\n")
    > tn.write("exit\n")
    >
    > print tn.read_all()
    >
    > Regardless of the script content, running in windows I constently get
    > this SyntaxError:
    >
    > C:\Python30>python c:\Python30\scripts\telnet.py
    > File "c:\Python30\scripts\telnet.py", line 20
    > print tn.read_all()
    > ^
    > SyntaxError: invalid syntax
    >
    > C:\Python30>
    >
    > The same script works fine from linux.
    >
    > I have also notices some other slight differences: this is my original
    > script that runs and completes but only if I comment out print. Also
    > tried to run debug without success in windows again this worked fine in
    > linux. To run this script in linux I also had to remove the b syntax in
    > the "b" in the perentesis
    >
    > import telnetlib
    > # import pdb
    >
    > HOST = "HP switch"
    >
    > tn = telnetlib.Telnet(HOST)
    >
    > tn.read_until(b'Password: ')
    > tn.write(b'password\n')
    >
    > pdb.set_trace()
    >
    > tn.read_until(b'HP switch# ')
    > tn.write(b' sh time\n')
    >
    > tn.read_until(b'HP switch# ')
    > tn.write(b'exit\n')
    >
    > tn.read_until(b'HP switch> ')
    > tn.write(b'exit\n')
    >
    > tn.read_until(b'Do you want to log out [y/n]? ')
    > tn.write(b'y')
    >
    > print tn.read_all()
    >
    > Any guidance would be appreciated.
    >

    It looks like you're using Python 3.0 on Windows and Python 2.x on
    Linux.

    In Python 2.x, 'print' is a statement:

    print tn.read_all()


    In Python 3.x, 'print' is a function, so you need to write:

    print(tn.read_all())
     
    MRAB, Mar 25, 2009
    #3
  4. Thats newbe experience for ya ;-) thanks. Its seems to work and leads to
    another question. whether running the script or stepping thru the process at
    the command line I get what looks like hex

    C:\Python30>python \Python30\scripts\telnet-tftp1.py
    b'\x1b[24;1H\x1b[24;31H\x1b[24;1H\x1b[?25h\x1b[24;31H\x1b[24;31Hy\x1b[24;31H\x1b
    [?25h\x1b[24;32H\x1b[24;0H\x1bE\x1b[24;1H\x1b[24;32H\x1b[24;1H\x1b[2K\x1b[24;1H\
    x1b[?25h\x1b[24;1H\x1b[1;24r\x1b[24;1H\x1b[2J\x1b[?7l\x1b[1;24r\x1b[?6l\x1b[24;1
    H\x1b[?25h\x1b[24;1H\x1b[?6l\x1b[1;0r\x1b[?7l\x1b[2J\x1b[24;1H\x1b[1;1H\x1b[2K\x
    1b[24;1H\n\r'

    C:\Python30>

    I can verify the script ran thru and executed the telnet commands. is there
    a switch to convert to binary/ASCI or reduce the logging of the telnet
    session ???

    Thanks again
    Chris



    "Gary Herron" <> wrote in message
    news:...
    > Python Newsgroup wrote:
    >> I'm a total newbe to scripting not to mention python. However I was able
    >> to successfully create a telnet script to initiate login, initiate tftp,
    >> exit, exit, confirm and close session. Frustrated, possibly causing my
    >> own misery. I replace the sript the script with the standard example.
    >>
    >> import getpass
    >> import sys
    >> import telnetlib
    >>
    >> HOST = "remote linux"
    >> user = raw_input("Enter your remote account: ")
    >> password = getpass.getpass()
    >>
    >> tn = telnetlib.Telnet(HOST)
    >>
    >> tn.read_until("login: ")
    >> tn.write(user + "\n")
    >> if password:
    >> tn.read_until("Password: ")
    >> tn.write(password + "\n")
    >>
    >> tn.write("ls\n")
    >> tn.write("exit\n")
    >>
    >> print tn.read_all()
    >>
    >> Regardless of the script content, running in windows I constently get
    >> this SyntaxError:
    >>
    >> C:\Python30>python c:\Python30\scripts\telnet.py
    >> File "c:\Python30\scripts\telnet.py", line 20
    >> print tn.read_all()
    >> ^
    >> SyntaxError: invalid syntax
    >>
    >> C:\Python30>

    >
    >
    > There's the clue:
    >
    > In python 3.X, print is a function call
    > print(tn.read_all() )
    > with lots of formatting and line-ending features
    >
    > In python 2.X, print is a statement:
    > print tn.read_all()
    >
    > If you want one script to work for both Windows and Linux, then you should
    > probably
    > be running the same version of Python on each. At least both versions
    > should be on
    > the same side for the Python 2.x/3.x version change.
    >
    >
    > Gary Herron
    >
    >
    >
    >>
    >> The same script works fine from linux.
    >>
    >> I have also notices some other slight differences: this is my original
    >> script that runs and completes but only if I comment out print. Also
    >> tried to run debug without success in windows again this worked fine in
    >> linux. To run this script in linux I also had to remove the b syntax in
    >> the "b" in the perentesis
    >>
    >> import telnetlib
    >> # import pdb
    >>
    >> HOST = "HP switch"
    >>
    >> tn = telnetlib.Telnet(HOST)
    >>
    >> tn.read_until(b'Password: ')
    >> tn.write(b'password\n')
    >>
    >> pdb.set_trace()
    >>
    >> tn.read_until(b'HP switch# ')
    >> tn.write(b' sh time\n')
    >>
    >> tn.read_until(b'HP switch# ')
    >> tn.write(b'exit\n')
    >>
    >> tn.read_until(b'HP switch> ')
    >> tn.write(b'exit\n')
    >>
    >> tn.read_until(b'Do you want to log out [y/n]? ')
    >> tn.write(b'y')
    >>
    >> print tn.read_all()
    >>
    >> Any guidance would be appreciated.
    >>
    >> Delrey
    >> --
    >> http://mail.python.org/mailman/listinfo/python-list

    >
     
    Python Newsgroup, Mar 25, 2009
    #4
  5. Gotcha, I got started from the telnet example listed in the docs. The linux
    install was via yum and installed 2.x instead. That explains it. Althought
    print (tn.read_all () ) runs in 2.x on linux.

    I have another problem maybe you cna help me with. My telnet output
    jibberish in windows: I cna parse some of the output for results but is
    there anyway to get rid of the jibberish. (looks like hex but I'm not sure)

    I can only use this tn.read_until(b'whatever string') Is this whats causing
    the output? the b switch. seems like I don't need it in linux. is that
    binary?

    Thanks

    C:\Python30>python \Python30\scripts\telnet-tftp1.py
    b'\x1b[24;1H\x1b[24;31H\x1b[24;1H\x1b[?25h\x1b[24;31H\x1b[24;31Hy\x1b[24;31H\x1b
    [?25h\x1b[24;32H\x1b[24;0H\x1bE\x1b[24;1H\x1b[24;32H\x1b[24;1H\x1b[2K\x1b[24;1H\
    x1b[?25h\x1b[24;1H\x1b[1;24r\x1b[24;1H\x1b[2J\x1b[?7l\x1b[1;24r\x1b[?6l\x1b[24;1
    H\x1b[?25h\x1b[24;1H\x1b[?6l\x1b[1;0r\x1b[?7l\x1b[2J\x1b[24;1H\x1b[1;1H\x1b[2K\x
    1b[24;1H\n\r'

    C:\Python30>




    "MRAB" <> wrote in message
    news:...
    > Python Newsgroup wrote:
    >> I'm a total newbe to scripting not to mention python. However I was able
    >> to successfully create a telnet script to initiate login, initiate tftp,
    >> exit, exit, confirm and close session. Frustrated, possibly causing my
    >> own misery. I replace the sript the script with the standard example.
    >>
    >> import getpass
    >> import sys
    >> import telnetlib
    >>
    >> HOST = "remote linux"
    >> user = raw_input("Enter your remote account: ")
    >> password = getpass.getpass()
    >>
    >> tn = telnetlib.Telnet(HOST)
    >>
    >> tn.read_until("login: ")
    >> tn.write(user + "\n")
    >> if password:
    >> tn.read_until("Password: ")
    >> tn.write(password + "\n")
    >>
    >> tn.write("ls\n")
    >> tn.write("exit\n")
    >>
    >> print tn.read_all()
    >>
    >> Regardless of the script content, running in windows I constently get
    >> this SyntaxError:
    >>
    >> C:\Python30>python c:\Python30\scripts\telnet.py
    >> File "c:\Python30\scripts\telnet.py", line 20
    >> print tn.read_all()
    >> ^
    >> SyntaxError: invalid syntax
    >>
    >> C:\Python30>
    >>
    >> The same script works fine from linux.
    >>
    >> I have also notices some other slight differences: this is my original
    >> script that runs and completes but only if I comment out print. Also
    >> tried to run debug without success in windows again this worked fine in
    >> linux. To run this script in linux I also had to remove the b syntax in
    >> the "b" in the perentesis
    >>
    >> import telnetlib
    >> # import pdb
    >>
    >> HOST = "HP switch"
    >>
    >> tn = telnetlib.Telnet(HOST)
    >>
    >> tn.read_until(b'Password: ')
    >> tn.write(b'password\n')
    >>
    >> pdb.set_trace()
    >>
    >> tn.read_until(b'HP switch# ')
    >> tn.write(b' sh time\n')
    >>
    >> tn.read_until(b'HP switch# ')
    >> tn.write(b'exit\n')
    >>
    >> tn.read_until(b'HP switch> ')
    >> tn.write(b'exit\n')
    >>
    >> tn.read_until(b'Do you want to log out [y/n]? ')
    >> tn.write(b'y')
    >>
    >> print tn.read_all()
    >>
    >> Any guidance would be appreciated.
    >>

    > It looks like you're using Python 3.0 on Windows and Python 2.x on
    > Linux.
    >
    > In Python 2.x, 'print' is a statement:
    >
    > print tn.read_all()
    >
    >
    > In Python 3.x, 'print' is a function, so you need to write:
    >
    > print(tn.read_all())
     
    Python Newsgroup, Mar 25, 2009
    #5
  6. Python Newsgroup

    MRAB Guest

    Python Newsgroup wrote:
    > Gotcha, I got started from the telnet example listed in the docs. The
    > linux install was via yum and installed 2.x instead. That explains it.
    > Althought print (tn.read_all () ) runs in 2.x on linux.
    >
    > I have another problem maybe you cna help me with. My telnet output
    > jibberish in windows: I cna parse some of the output for results but is
    > there anyway to get rid of the jibberish. (looks like hex but I'm not sure)
    >
    > I can only use this tn.read_until(b'whatever string') Is this whats
    > causing the output? the b switch. seems like I don't need it in linux.
    > is that binary?
    >
    > Thanks
    >
    > C:\Python30>python \Python30\scripts\telnet-tftp1.py
    > b'\x1b[24;1H\x1b[24;31H\x1b[24;1H\x1b[?25h\x1b[24;31H\x1b[24;31Hy\x1b[24;31H\x1b
    >
    > [?25h\x1b[24;32H\x1b[24;0H\x1bE\x1b[24;1H\x1b[24;32H\x1b[24;1H\x1b[2K\x1b[24;1H\
    >
    > x1b[?25h\x1b[24;1H\x1b[1;24r\x1b[24;1H\x1b[2J\x1b[?7l\x1b[1;24r\x1b[?6l\x1b[24;1
    >
    > H\x1b[?25h\x1b[24;1H\x1b[?6l\x1b[1;0r\x1b[?7l\x1b[2J\x1b[24;1H\x1b[1;1H\x1b[2K\x
    >
    > 1b[24;1H\n\r'
    >
    > C:\Python30>
    >

    They look like control sequences for a terminal.
     
    MRAB, Mar 25, 2009
    #6
  7. Python Newsgroup

    Terry Reedy Guest

    MRAB wrote:
    > Python Newsgroup wrote:
    >> Gotcha, I got started from the telnet example listed in the docs. The
    >> linux install was via yum and installed 2.x instead. That explains it.
    >> Althought print (tn.read_all () ) runs in 2.x on linux.
    >>
    >> I have another problem maybe you cna help me with. My telnet output
    >> jibberish in windows: I cna parse some of the output for results but
    >> is there anyway to get rid of the jibberish. (looks like hex but I'm
    >> not sure)
    >>
    >> I can only use this tn.read_until(b'whatever string') Is this whats
    >> causing the output? the b switch. seems like I don't need it in linux.
    >> is that binary?
    >>
    >> Thanks
    >>
    >> C:\Python30>python \Python30\scripts\telnet-tftp1.py
    >> b'\x1b[24;1H\x1b[24;31H\x1b[24;1H\x1b[?25h\x1b[24;31H\x1b[24;31Hy\x1b[24;31H\x1b
    >>
    >> [?25h\x1b[24;32H\x1b[24;0H\x1bE\x1b[24;1H\x1b[24;32H\x1b[24;1H\x1b[2K\x1b[24;1H\
    >>
    >> x1b[?25h\x1b[24;1H\x1b[1;24r\x1b[24;1H\x1b[2J\x1b[?7l\x1b[1;24r\x1b[?6l\x1b[24;1
    >>
    >> H\x1b[?25h\x1b[24;1H\x1b[?6l\x1b[1;0r\x1b[?7l\x1b[2J\x1b[24;1H\x1b[1;1H\x1b[2K\x
    >>
    >> 1b[24;1H\n\r'
    >>
    >> C:\Python30>
    >>

    > They look like control sequences for a terminal.


    Indeed. \x1b == \033 == 27 == ESCAPE. 'ESC [' is the standard prefix
    for ANSI terminal control sequences.

    http://en.wikipedia.org/wiki/ANSI_escape_code

    "32-bit character-mode (subsystem:console) Windows applications don't
    write ANSI escape sequences to the console. They must interpret the
    escape code actions and call the native Console API instead to
    accomplish the proper result. If you do write the sequences directly,
    all you'll get is the text of the sequence on the screen, not the action
    intended."

    I mostly see a crazy mixture of ESC [ row , col H cursor position
    commands. For you app, parse out and ignore the codes not skipped by
    ..read_until calls.


    Terry Jan Reedy
     
    Terry Reedy, Mar 25, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DIBS

    SyntaxError: invalid syntax

    DIBS, Dec 27, 2005, in forum: Python
    Replies:
    5
    Views:
    39,534
    Terry Hancock
    Dec 27, 2005
  2. ronrsr

    SyntaxError: Invalid Syntax.

    ronrsr, Nov 10, 2006, in forum: Python
    Replies:
    10
    Views:
    1,435
    John Machin
    Nov 10, 2006
  3. Replies:
    3
    Views:
    1,683
  4. nicklancer

    SyntaxError: invalid syntax

    nicklancer, Apr 26, 2011, in forum: Python
    Replies:
    0
    Views:
    619
    nicklancer
    Apr 26, 2011
  5. Replies:
    9
    Views:
    400
    Karim
    Jan 10, 2013
Loading...

Share This Page