issue while doing pexpect ssh

harirammanohar159 at gmail.com harirammanohar159 at gmail.com
Wed Sep 9 04:58:30 EDT 2015


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






More information about the Python-list mailing list