Python pexpect:SSH连接并更新日期

6

我的Python pexpect脚本终于可以工作了,除了最重要的部分-更新日期!我能够SSH进入目标主机,但是我的第二个命令无法正确执行。我一直在想方设法解决问题,但是一直没有头绪。我已经检查了代码输出,根据编写的内容应该是有效的,但是我不是Python或pexpect专家,所以需要一些帮助来弄清楚为什么我的时间没有更新。

我的原始代码:

list = ["089"]
sn = 0

ssh_new_conn = 'Are you sure you want to continue connecting'

class ThreadClass(threading.Thread):
def __init__(self, index):
super(ThreadClass, self).__init__()
self.index = index
def run(self):

sn = storelist[self.index]


#easterndate = (currenttime + datetime.timedelta(0, 3600))
#easterndate = easterndate

est = timezone('US/Eastern')
cst = timezone('US/Central')
#currenttime = (datetime.now())
currenttime = cst.localize(datetime.now())
#easterndate = (currenttime + timedelta(0, 3600))
#easterndate = easterndate.strftime("%a %b %d %H:%M:%S %Z %Y")
easterndate = currenttime.astimezone(est).strftime("%a %b %d %H:%M:%S %Z %Y")
command1 = "/usr/bin/ssh %(username)s@%(hostname)s" % locals()
command2 = " sudo date -s\"%(easterndate)s\"" % locals()
command3 = " sudo date -s\"%(currenttime)s\"" % locals()
now = datetime.now()

#central
if sn == "073" or sn == "066" or sn == "016": #or sn == "022":
    p = pexpect.spawn((command1 + command3), timeout=360)


#eastern
else:
    print(command1 + command2)
    p = pexpect.spawn((command1 + command2), timeout=360)


# Handles the 3 possible connection outcomes:
# a) Ssh to the remote host for the first time, triggering 'Are you sure you want to continue connecting'
# b) ask you for password
# c) No password is needed at all, because you already have the key.
i = p.expect([ssh_new_conn,'[pP]assword:',pexpect.EOF])
print ' Initial pexpect command output: ', i
if i == 0:
    # send 'yes'
    p.sendline('yes')
    i = p.expect(['[pP]assword:',pexpect.EOF])
    print 'sent yes. pexpect command output', i
    if i == 0:
        # send the password
        print "logging into box %(sn)s" % locals()
        p.sendline(password)
        print "login successful"
        print "Setting the time..."

elif i == 1:
    # send the password
    print "logging into box %(sn)s" % locals()
    p.sendline(password)
    print "login successful"
    print "Setting the time..."
    p.close()

elif i == 2:
    print "pexpect faced key or connection timeout"
    pass

print p.before

for i in range(len(list)):
  t = ThreadClass(i)
  t.start()

新代码:

class ThreadClass(threading.Thread):
def __init__(self, index):
   super(ThreadClass, self).__init__()
   self.index = index
def run(self):

    try:
        sn = storelist[self.index]
        username = raw_input('username: ')
        password = raw_input('password: ')
        hostname = "[hostname]"
        est = timezone('US/Eastern')
        cst = timezone('US/Central')
        #currenttime = (datetime.now())
        currenttime = cst.localize(datetime.now())
        #easterndate = (currenttime + timedelta(0, 3600))
        #easterndate = easterndate.strftime("%a %b %d %H:%M:%S %Z %Y")
        easterndate = currenttime.astimezone(est).strftime("%a %b %d %H:%M:%S %Z %Y")
        ssh = pxssh.pxssh()

        print(hostname + " " + username + " " + password)
        ssh.login(hostname, username, password)

        if sn == "073" or sn == "066" or sn == "016": #or sn == "022":
            ssh.sendline ('date')       # run a command
            ssh.prompt()                # match the prompt
            print(s.before)           # print everything before the prompt.
            ssh.sendline ('sudo date -s\"%(currenttime)s\"' % locals())  # run a command
            ssh.expect('(?i)password.*:')  # match password prompt for sudo
            ssh.sendline(password)
            ssh.prompt()
            print(s.before)
            ssh.logout()
        else:
            ssh.sendline ('date')       # run a command
            ssh.prompt()                # match the prompt
            print(s.before)           # print everything before the prompt.
            ssh.sendline ('sudo date -s\"%(easterndate)s\"' % locals())  # run a command
            ssh.expect('(?i)password.*:')  # match password prompt for sudo
            ssh.sendline(password)
            ssh.prompt()
            print(s.before)
            ssh.logout()

    except pxssh.ExceptionPxssh as e:
        print(e)


for i in range(len(storelist)):
  t = ThreadClass(i)
  t.start()

我遇到的新错误:

Traceback (most recent call last):
  File "./sshtest.py", line 8, in <module>
    s.login (hostname, username, password)
  File "/usr/lib/python2.6/dist-packages/pxssh.py", line 243, in login
    if not self.synch_original_prompt():
  File "/usr/lib/python2.6/dist-packages/pxssh.py", line 134, in synch_original_prompt
    self.read_nonblocking(size=10000,timeout=1) # GAS: Clear out the cache before getting the prompt
  File "/usr/lib/python2.6/dist-packages/pexpect.py", line 824, in read_nonblocking
    raise TIMEOUT ('Timeout exceeded in read_nonblocking().')
pexpect.TIMEOUT: Timeout exceeded in read_nonblocking().

错误解决方案

我找到了解决我遇到的错误的方法。由于一个已知的bug,我需要在usr/lib/python.2.6/dist-packages/pxssh.py中添加以下几行代码:

self.sendline()       #line 134
time.sleep(0.5)       #line 135
self.read_nonblocking(size=10000,timeout=1) # GAS: Clear out the cache before getting the prompt

请更新您的pexpect版本,例如安装到~/.local,运行:pip install --user pexpect。超时应由pexpect本身处理。 - jfs
@Sebastian 你说的 pip 是指 python-pip 吗? - WorkerBee
1
是的,pip - jfs
3个回答

2

解决错误的方法

我找到了解决我遇到的错误的方法。由于一个已知的漏洞,我需要在usr/lib/python.2.6/dist-packages/pxssh.py中添加以下行:

self.sendline()       #line 134
time.sleep(0.5)       #line 135
self.read_nonblocking(size=10000,timeout=1) # GAS: Clear out the cache before getting the prompt

对我来说,该行仅用于清除ssh登录发出的所有缓冲区。之后,他们发送空命令以引发另一个提示输出。因此,我认为可以将该行简单地包装在“try-except-pass”中,以忽略输入中没有剩余内容的情况。 - ony

1
你可能需要处理sudo密码提示(使用-t ssh选项获取tty),并在p.close()之前使用p.expect(EOF)以避免过早终止子进程。以下是基于pexpect文档的示例:
import pxssh
try:
    s = pxssh.pxssh()
    s.login (hostname, username, password)
    s.sendline ('date')       # run a command
    s.prompt()                # match the prompt
    print(s.before)           # print everything before the prompt.
    s.sendline ('sudo date')  # run a command
    s.expect('(?i)password.*:')  # match password prompt for sudo
    s.sendline(password)
    s.prompt()
    print(s.before)
    s.logout()
except pxssh.ExceptionPxssh as e:
    print(e)

你也可以尝试 fabric

# fabfile.py
from fabric.api import run, sudo

def date():
    run('date')
    sudo('date')

使用方法:

$ fab -H localhost,user@host date

@Sebastian 我尝试了你提供的方法,但是在s.login(hostname, username, password)这一行总是出现问题。我已经仔细检查了传递给该函数的值,一切看起来都是正确的。我甚至尝试运行你提供链接中的示例代码,但代码在同一行上出错。 - WorkerBee
@Sebastian,请检查我的原始帖子,查看我的最新更新代码。 - WorkerBee
@Sebastian,请查看我原始的帖子,了解我遇到的错误。 - WorkerBee
@Sebastian 我找到了解决我遇到的错误的方法。我将运行一个快速测试以确保其他所有内容都正常工作。如果一切正常,我将在今天将您的答案标记为正确。 - WorkerBee

0
对于像我一样在2019年被困在Python 2.6中,又不想破坏他们的站点包的其他可怜灵魂,我能够通过在构造函数中将maxread设置为1来解决这个问题。

pxssh.pxssh(maxread=1)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接