简体   繁体   中英

Python - failed using pexpect/pxssh

i'm pretty new with python and i'm trying to make a simple ssh session and run a simple command. i know that i can use "paramiko" but i decide to use pexpect/pxssh and i installed the last version.

my code:

#!/usr/bin/env python

from pexpect import *
import pexpect
import pxssh
import getpass
import time
import os

try:
    s = pexpect.pxssh()
    hostname = raw_input('hostname:')
    username = raw_input('usernmae:')
    s.login((hostnmae,username,password)
    s.sendline ('uptime')
    s.prompt()
    print s.before
    s.sendline ('ls -l')
    s.prompt()
    print s.before
    s.logout()
except pxssh.ExceptionPxssh, e:
    print "pxssh failed"
    print str(e)

but it fails with the following:

$ python pssh.py
  File "pssh.py", line 15
    s.sendline ('uptime')
    ^
SyntaxError: invalid syntax

can someone please help?

Thanks a lot!

There are various problems in your code:

  1. It should be

     s = pexpect.pxssh.pxssh() 
  2. Check for extra "(" and variable hostname on line.

     s.login((hostnmae,username,password) 
  3. And you need password for the ssh before above line.

      import getpass() password = getpass.getpass() 

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