简体   繁体   中英

python - pxssh - failed to login

I'm trying to connect to a server using ssh via the pxssh python library. Here's my code:

import pxssh
import getpass
try:
    s = pxssh.pxssh()
    s.force_password = True
    hostname = 'myserverip'
    username = 'username'
    password = 'password'
    s.login (hostname, username, password)
    s.sendline ('get system number')  # run a command
    s.prompt()             # match the prompt
    print s.before         # print everything before the prompt.
    s.logout()
except pxssh.ExceptionPxssh, e:
    print "pxssh failed on login."
    print str(e)

Here is the response I am getting:

pxssh failed on login.
could not set shell prompt
 unset PROMPT_COMMAND

error,09
$ PS1='[PEXPECT]\$ '

error,09
$ set prompt='[PEXPECT]\$ '

error,09
$

Pretty old question but just for posterity. (For the one's who would bump in to this like me :) )

from pexpect import pxssh
import getpass
try:
    s = pxssh.pxssh()
    hostname = raw_input('hostname: ')
    username = raw_input('username: ')
    password = getpass.getpass('password: ')
    s.login(hostname, username, password)
    s.sendline('uptime')   # run a command
    s.prompt()             # match the prompt
    print(s.before)        # print everything before the prompt.
    s.sendline('ls -l')
    s.prompt()
    print(s.before)
    s.sendline('df')
    s.prompt()
    print(s.before)
    s.logout()
except pxssh.ExceptionPxssh as e:
    print("pxssh failed on login.")
    print(e)

which I just yanked from http://pexpect.readthedocs.io/en/latest/api/pxssh.html#pexpect.pxssh.pxssh

Did you verify the login credentials? The logs indicate that your script didnt clear the first step necessary to get the shell. s.login(hostname, username, password) BTW, you are hardcoding all these arguments. Your first problem here is that there is no hostname as 'hostname' which you can logon to on any system, unless alias of 'localhost' or some website/url.

Note: Use your head before blatantly copying examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM