简体   繁体   English

Python-使用pexpect / pxssh失败

[英]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. 我是python的新手,我正在尝试建立一个简单的ssh会话并运行一个简单的命令。 i know that i can use "paramiko" but i decide to use pexpect/pxssh and i installed the last version. 我知道我可以使用“ paramiko”,但我决定使用pexpect / pxssh并安装了最新版本。

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. 检查是否有多余的“(”和在线上的变量hostname

     s.login((hostnmae,username,password) 
  3. And you need password for the ssh before above line. 并且您需要在上一行之前输入ssh的密码。

      import getpass() password = getpass.getpass() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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