FAQ
Some where i am missing simple logic.... :)


=====
child = pexpect.spawn('ssh hari at hostname')
child.logfile = sys.stdout
child.expect('hari\'s Password: ')
=====


getting error as follows:
============
child.expect('hari\'s Password: ')
TypeError: must be str, not bytes
===========


Thanks...

Search Discussions

  • Laura Creighton at Sep 8, 2015 at 12:12 pm
    In a message of Tue, 08 Sep 2015 04:37:09 -0700, harirammanohar159 at gmail.com wr
    ites:
    Some where i am missing simple logic.... :)

    =====
    child = pexpect.spawn('ssh hari at hostname')
    child.logfile = sys.stdout
    child.expect('hari\'s Password: ')
    =====

    getting error as follows:
    ============
    child.expect('hari\'s Password: ')
    TypeError: must be str, not bytes
    ===========

    Thanks...
    --
    https://mail.python.org/mailman/listinfo/python-list

    https://pexpect.readthedocs.org/en/latest/api/pexpect.html#run-function
    you want to use spawnu because you have a unicode string.


    Laura
  • Chris Angelico at Sep 8, 2015 at 1:57 pm

    On Tue, Sep 8, 2015 at 9:37 PM, wrote:
    Some where i am missing simple logic.... :)

    =====
    child = pexpect.spawn('ssh hari at hostname')
    child.logfile = sys.stdout
    child.expect('hari\'s Password: ')
    =====

    getting error as follows:
    ============
    child.expect('hari\'s Password: ')
    TypeError: must be str, not bytes
    ===========

    Laura's already answered your actual question. But I would recommend
    using public key login rather than keying in a password; it's a lot
    more secure, as it means you don't have to embed a password in your
    source code (or at very best, on your hard disk in some other readable
    and decryptable way). It also simplifies the transaction
    significantly.


    ChrisA
  • Jmp at Sep 8, 2015 at 2:56 pm

    On 09/08/2015 03:57 PM, Chris Angelico wrote:
    On Tue, Sep 8, 2015 at 9:37 PM, wrote:
    Some where i am missing simple logic.... :)

    =====
    child = pexpect.spawn('ssh hari at hostname')
    child.logfile = sys.stdout
    child.expect('hari\'s Password: ')
    =====

    getting error as follows:
    ============
    child.expect('hari\'s Password: ')
    TypeError: must be str, not bytes
    ===========
    Laura's already answered your actual question. But I would recommend
    using public key login [snip]

    ChrisA

    My 2 cents, beside the public key, use the python module paramiko,
    unless you really want to work at the low level yourself.


    http://docs.paramiko.org/en/1.15/api/client.html


    JM
  • Laura Creighton at Sep 8, 2015 at 4:43 pm

    In a message of Tue, 08 Sep 2015 23:57:41 +1000, Chris Angelico writes:
    On Tue, Sep 8, 2015 at 9:37 PM, wrote:
    Some where i am missing simple logic.... :)

    =====
    child = pexpect.spawn('ssh hari at hostname')
    child.logfile = sys.stdout
    child.expect('hari\'s Password: ')
    =====

    getting error as follows:
    ============
    child.expect('hari\'s Password: ')
    TypeError: must be str, not bytes
    ===========
    Laura's already answered your actual question. But I would recommend
    using public key login rather than keying in a password; it's a lot
    more secure, as it means you don't have to embed a password in your
    source code (or at very best, on your hard disk in some other readable
    and decryptable way). It also simplifies the transaction
    significantly.

    ChrisA

    This poor soul cannot login at all. He needs this hack to let him
    su some-other-user because of a draconian policy he cannot do
    anything about. If he could just log in, one heck of a lot of
    other things would be simpler for him ...


    Laura
  • Harirammanohar159 at Sep 9, 2015 at 8:58 am

    On Tuesday, 8 September 2015 17:07:24 UTC+5:30, hariramm... at gmail.com wrote:
    Some where i am missing simple logic.... :)

    =====
    child = pexpect.spawn('ssh hari at hostname')
    child.logfile = sys.stdout
    child.expect('hari\'s Password: ')
    =====

    getting error as follows:
    ============
    child.expect('hari\'s Password: ')
    TypeError: must be str, not bytes
    ===========

    Thanks...

    Hey Laura,


    spawnu worked for handling the unicode, thank you for that, yes i have already tried paramiko, it didnt worked for the same purpose (tried both sftp client and ssh client of paramiko module; finally while doing su getting the error as expecting tty terminal...)


    fabric will work but it is up to only python 2.7


    so just i am trying to work out with pexpect whether it will be of any use or not.


    (my goal is to login to remote server then do su then execute commands...)


    Thank you for spawnu, now i got stuck with freezing issue similar to it is not returing to the shell prompt...its on waiting...i had to press ctrl+c


    ===========
    child.sendline(password)
    child.expect('-bash-4.1$')
    child.sendline('ls')
    print(child.before)
    child.sendline('su - user1')
    child.expect('-bash-4.1$')
    child.sendline('pwd; hostname')
    print(child.before)
    child.close()
    =================
    output:
    ============


    hari's Password:
    ls


    WARNING. You have accessed a private computer system.
    Unauthorized access,use,connection,or entry is not permitted
    and constitutes a crime punishable by law. We reserve the
    right to fully pursue criminal and civil legal penalties.
    All individuals using this computer system with or without proper
    authority are subject to having all their activities monitored
    and recorded. Anyone using this system implicitly consents to
    this monitoring. Any evidence of suspected criminal activity
    revealed by such monitoring may be provided to law enforcement officials.




    su - user1


    lsTotal users logged in under your name (via BoKS): 3 (2 on this host)
    Total users logged in (via BoKS): 8324 (2 on this host)






    #########################################################################
    # For Authorized Use Only #
    # #
    # Activities on this system are subject to being monitored #
    # or recorded for all users, authorized or unauthorized. #
    # Use of the system is considered consent to such monitoring. #
    # Any evidence of possible criminal activity may be provided #
    # to law enforcement. #
    # #
    #########################################################################






    -bash-4.1$ ls
    -bash-4.1$ su - user1
    -bash-4.1$




    ^CTraceback (most recent call last):
       File "./rmcomments.py", line 16, in <module>
         child.expect('-bash-4.1$')
       File "/ceopt/utils/python/3.4.3/custommodules/lib/python3.4/site-packages/pexpect/__init__.py", line 1451, in expect
         timeout, searchwindowsize)
       File "/ceopt/utils/python/3.4.3/custommodules/lib/python3.4/site-packages/pexpect/__init__.py", line 1466, in expect_list
         timeout, searchwindowsize)
       File "/ceopt/utils/python/3.4.3/custommodules/lib/python3.4/site-packages/pexpect/__init__.py", line 1535, in expect_loop
         c = self.read_nonblocking(self.maxread, timeout)
       File "/ceopt/utils/python/3.4.3/custommodules/lib/python3.4/site-packages/pexpect/__init__.py", line 958, in read_nonblocking
         r, w, e = self.__select([self.child_fd], [], [], timeout)
       File "/ceopt/utils/python/3.4.3/custommodules/lib/python3.4/site-packages/pexpect/__init__.py", line 1717, in __select
         return select.select(iwtd, owtd, ewtd, timeout)
    KeyboardInterrupt
  • Laura Creighton at Sep 9, 2015 at 10:45 am
    In a message of Wed, 09 Sep 2015 01:58:30 -0700, harirammanohar159 at gmail.com wr
    ites:

    Thank you for spawnu, now i got stuck with freezing issue similar to it is not returing to the shell prompt...its on waiting...i had to press ctrl+c

    ===========
    child.sendline(password)
    child.expect('-bash-4.1$')
    child.sendline('ls')
    print(child.before)
    child.sendline('su - user1')
    child.expect('-bash-4.1$')
    child.sendline('pwd; hostname')
    print(child.before)
    child.close()
    WARNING. You have accessed a private computer system.
    <lots more threats and stuff>


    Any doubts we had about whether the computer policy authorities
    as this place are 'nuttier than 2 trees full of squirrels' are
    now put to rest. I just hope I am not helping you do things that
    is going to end up with them trying to put you in jail. Poor you.

    su - user1

    lsTotal users logged in under your name (via BoKS): 3 (2 on this host)
    Total users logged in (via BoKS): 8324 (2 on this host)
    -bash-4.1$ ls
    -bash-4.1$ su - user1
    -bash-4.1$

    Okay.
    Instead of waiting for that bash-4.1 prompt
    after you issue your su, issue whoami
    and wait to see if it returns user1.


    That way we can see if the problem is with expect waiting for the prompt
    which for some reason it is not seeing, or whether it is just lost, lost,
    lost.


    Also, for informational purposes try your su without the '-'
    just "su user1", and see if it is some part of the 'provide an environment
    similar to what the user would get if the user logged in directly'
    ability of su that is confusing things.


    Laura
  • Harirammanohar159 at Sep 9, 2015 at 9:19 am

    On Tuesday, 8 September 2015 17:07:24 UTC+5:30, hariramm... at gmail.com wrote:
    Some where i am missing simple logic.... :)

    =====
    child = pexpect.spawn('ssh hari at hostname')
    child.logfile = sys.stdout
    child.expect('hari\'s Password: ')
    =====

    getting error as follows:
    ============
    child.expect('hari\'s Password: ')
    TypeError: must be str, not bytes
    ===========

    Thanks...

    Hey All,


    I am able to achieve this using pxssh.. thank you for help...
  • Laura Creighton at Sep 9, 2015 at 10:47 am

    Hey All,

    I am able to achieve this using pxssh.. thank you for help...

    Great to hear it. Good luck with the crazies.
    Laura
  • Harirammanohar159 at Sep 9, 2015 at 11:22 am

    On Tuesday, 8 September 2015 17:07:24 UTC+5:30, hariramm... at gmail.com wrote:
    Some where i am missing simple logic.... :)

    =====
    child = pexpect.spawn('ssh hari at hostname')
    child.logfile = sys.stdout
    child.expect('hari\'s Password: ')
    =====

    getting error as follows:
    ============
    child.expect('hari\'s Password: ')
    TypeError: must be str, not bytes
    ===========

    Thanks...

    Hi Laura,


    Basically its the same, no difference if i keep su user1...
    i am getting the desired output with a single host, but if i keep for loop for multiple hosts, remaining are skipped saying as below:


    pexpect.TIMEOUT: Timeout exceeded.


    During handling of the above exception, another exception occurred:


    pexpect.TIMEOUT: Timeout exceeded.
    <pexpect.pxssh.pxssh object@0x7f22d2b838d0>


    only 1st host is getting executed. how to control child.logout() and make for loop continue...
  • Harirammanohar159 at Sep 10, 2015 at 5:18 am

    On Tuesday, 8 September 2015 17:07:24 UTC+5:30, hariramm... at gmail.com wrote:
    Some where i am missing simple logic.... :)

    =====
    child = pexpect.spawn('ssh hari at hostname')
    child.logfile = sys.stdout
    child.expect('hari\'s Password: ')
    =====

    getting error as follows:
    ============
    child.expect('hari\'s Password: ')
    TypeError: must be str, not bytes
    ===========

    Thanks...

    Hi,


    can you check it and let me know the way...


    Thanks...
  • Harirammanohar159 at Sep 10, 2015 at 6:08 am
    Hey,


    its resolved by small tweak :)
    do child.sendline('logout') instead of child.logout()


    Thanks...

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedSep 8, '15 at 11:37a
activeSep 10, '15 at 6:08a
posts12
users4
websitepython.org

People

Translate

site design / logo © 2019 Grokbase