Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #96122 > unrolled thread

issue while doing pexpect ssh

Started byharirammanohar159@gmail.com
First post2015-09-08 04:37 -0700
Last post2015-09-09 23:08 -0700
Articles 12 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  issue while doing pexpect ssh harirammanohar159@gmail.com - 2015-09-08 04:37 -0700
    Re: issue while doing pexpect ssh Laura Creighton <lac@openend.se> - 2015-09-08 14:12 +0200
    Re: issue while doing pexpect ssh Chris Angelico <rosuav@gmail.com> - 2015-09-08 23:57 +1000
    Re: issue while doing pexpect ssh jmp <jeanmichel@sequans.com> - 2015-09-08 16:56 +0200
    Re: issue while doing pexpect ssh Laura Creighton <lac@openend.se> - 2015-09-08 18:43 +0200
    Re: issue while doing pexpect ssh harirammanohar159@gmail.com - 2015-09-09 01:58 -0700
      Re: issue while doing pexpect ssh Laura Creighton <lac@openend.se> - 2015-09-09 12:45 +0200
    Re: issue while doing pexpect ssh harirammanohar159@gmail.com - 2015-09-09 02:19 -0700
      Re: issue while doing pexpect ssh Laura Creighton <lac@openend.se> - 2015-09-09 12:47 +0200
    Re: issue while doing pexpect ssh harirammanohar159@gmail.com - 2015-09-09 04:22 -0700
    Re: issue while doing pexpect ssh harirammanohar159@gmail.com - 2015-09-09 22:18 -0700
    Re: issue while doing pexpect ssh harirammanohar159@gmail.com - 2015-09-09 23:08 -0700

#96122 — issue while doing pexpect ssh

Fromharirammanohar159@gmail.com
Date2015-09-08 04:37 -0700
Subjectissue while doing pexpect ssh
Message-ID<3f4642c2-d548-4089-87c2-f3abb4ccb224@googlegroups.com>
Some where i am missing simple logic.... :)

=====
child = pexpect.spawn('ssh hari@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...

[toc] | [next] | [standalone]


#96125

FromLaura Creighton <lac@openend.se>
Date2015-09-08 14:12 +0200
Message-ID<mailman.220.1441714357.8327.python-list@python.org>
In reply to#96122
In a message of Tue, 08 Sep 2015 04:37:09 -0700, harirammanohar159@gmail.com wr
ites:
>Some where i am missing simple logic.... :)
>
>=====
>child = pexpect.spawn('ssh hari@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

[toc] | [prev] | [next] | [standalone]


#96128

FromChris Angelico <rosuav@gmail.com>
Date2015-09-08 23:57 +1000
Message-ID<mailman.223.1441720664.8327.python-list@python.org>
In reply to#96122
On Tue, Sep 8, 2015 at 9:37 PM,  <harirammanohar159@gmail.com> wrote:
> Some where i am missing simple logic.... :)
>
> =====
> child = pexpect.spawn('ssh hari@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

[toc] | [prev] | [next] | [standalone]


#96133

Fromjmp <jeanmichel@sequans.com>
Date2015-09-08 16:56 +0200
Message-ID<mailman.226.1441724225.8327.python-list@python.org>
In reply to#96122
On 09/08/2015 03:57 PM, Chris Angelico wrote:
> On Tue, Sep 8, 2015 at 9:37 PM,  <harirammanohar159@gmail.com> wrote:
>> Some where i am missing simple logic.... :)
>>
>> =====
>> child = pexpect.spawn('ssh hari@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

[toc] | [prev] | [next] | [standalone]


#96137

FromLaura Creighton <lac@openend.se>
Date2015-09-08 18:43 +0200
Message-ID<mailman.228.1441730631.8327.python-list@python.org>
In reply to#96122
In a message of Tue, 08 Sep 2015 23:57:41 +1000, Chris Angelico writes:
>On Tue, Sep 8, 2015 at 9:37 PM,  <harirammanohar159@gmail.com> wrote:
>> Some where i am missing simple logic.... :)
>>
>> =====
>> child = pexpect.spawn('ssh hari@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

[toc] | [prev] | [next] | [standalone]


#96174

Fromharirammanohar159@gmail.com
Date2015-09-09 01:58 -0700
Message-ID<789c4865-9526-4592-bcaa-9b83955b41d8@googlegroups.com>
In reply to#96122
On Tuesday, 8 September 2015 17:07:24 UTC+5:30, hariramm...@gmail.com  wrote:
> Some where i am missing simple logic.... :)
> 
> =====
> child = pexpect.spawn('ssh hari@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


[toc] | [prev] | [next] | [standalone]


#96183

FromLaura Creighton <lac@openend.se>
Date2015-09-09 12:45 +0200
Message-ID<mailman.263.1441795576.8327.python-list@python.org>
In reply to#96174
In a message of Wed, 09 Sep 2015 01:58:30 -0700, harirammanohar159@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

[toc] | [prev] | [next] | [standalone]


#96178

Fromharirammanohar159@gmail.com
Date2015-09-09 02:19 -0700
Message-ID<21277811-df80-49d8-9249-c58b7fdf01c2@googlegroups.com>
In reply to#96122
On Tuesday, 8 September 2015 17:07:24 UTC+5:30, hariramm...@gmail.com  wrote:
> Some where i am missing simple logic.... :)
> 
> =====
> child = pexpect.spawn('ssh hari@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...

[toc] | [prev] | [next] | [standalone]


#96184

FromLaura Creighton <lac@openend.se>
Date2015-09-09 12:47 +0200
Message-ID<mailman.264.1441795685.8327.python-list@python.org>
In reply to#96178
>Hey All,
>
>I am able to achieve this using pxssh.. thank you for help...

Great to hear it.  Good luck with the crazies.
Laura

[toc] | [prev] | [next] | [standalone]


#96187

Fromharirammanohar159@gmail.com
Date2015-09-09 04:22 -0700
Message-ID<a6c6cdec-93d4-4839-a048-de5361d78236@googlegroups.com>
In reply to#96122
On Tuesday, 8 September 2015 17:07:24 UTC+5:30, hariramm...@gmail.com  wrote:
> Some where i am missing simple logic.... :)
> 
> =====
> child = pexpect.spawn('ssh hari@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 at 0x7f22d2b838d0>

only 1st host is getting executed. how to control child.logout() and make for loop continue...

[toc] | [prev] | [next] | [standalone]


#96239

Fromharirammanohar159@gmail.com
Date2015-09-09 22:18 -0700
Message-ID<f35aa5d0-65ba-4499-88e0-5ec467285cef@googlegroups.com>
In reply to#96122
On Tuesday, 8 September 2015 17:07:24 UTC+5:30, hariramm...@gmail.com  wrote:
> Some where i am missing simple logic.... :)
> 
> =====
> child = pexpect.spawn('ssh hari@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...

[toc] | [prev] | [next] | [standalone]


#96241

Fromharirammanohar159@gmail.com
Date2015-09-09 23:08 -0700
Message-ID<19322052-a9bf-441a-bc8e-bb7ae1932037@googlegroups.com>
In reply to#96122
Hey,

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

Thanks...

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web