【问题标题】:installing software on remote machine via python script通过 python 脚本在远程机器上安装软件
【发布时间】:2017-05-22 21:46:02
【问题描述】:

这是为了工作......所以我会尽可能多地分享,我正在尝试通过 python 脚本在远程机器上安装“环境”,这个“环境”需要传递给它用户名和密码,我尝试了很多东西,似乎没有任何效果......最接近的是这个脚本,但是在它传递用户名后会弹出一个GUI并要求输入密码......我做错了什么?!或者我该怎么做才能让它工作?!...这是处理 pexpect 的脚本的一部分

import os
import pexpect

cmd = 'ssh -X groupName@machineName cd ~/theLocationOfTheInstallation/ && pwd && theFullPathOfTheFileToInstall'
child = pexpect.spawn(cmd)
cmd_show_data = ''
usr = 'userName'
pas = 'myPassword'
while not child.eof() :
    index = child.expect(['Please enter your.*','pass.*', pexpect.EOF, pexpect.TIMEOUT])
    cmd_show_data += child.before
    child.delaybeforesend = 1
    if index == 0 :
        print 'user name required, "'+usr+'" is passed'
        child.sendline(usr)
    elif index == 1 :
        print 'password required, "'+pas+'" is passed'
        child.sendline(pas)
    elif index == 2 :
        print 'EOF'
    elif index == 3 :
        print 'TIMEOUT'
cmd_show_data += child.before
cmd_show_data  = cmd_show_data.split('\r\n')
for s in cmd_show_data :
    print s

这是弹出的 GUI: 如果我手动输入密码(我试图避免),我会得到这样的输出:

user name required, "userName" is passed
TIMEOUT
TIMEOUT (a lot of times out)
user name required, "userName" is passed
TIMEOUT
TIMEOUT (a lot of times out)
password required, "myPassword" is passed
TIMEOUT
TIMEOUT (a lot of times out).... till the installation is complete.

所以.. 有什么想法吗?

【问题讨论】:

  • 说实话......我解决这个问题的所有方法都可能是错误的,我愿意听取任何种解决方案。

标签: python unix ssh git-log


【解决方案1】:

如果你真的想只使用 Python 来完成这项工作,为什么不能使用 Ansible 来代替?

Ansible 解决方案:

如果您有一个在本地安装软件的脚本,则使用 Ansible 脚本模块在远程服务器上运行该脚本

# Example from Ansible Playbooks
- script: /some/local/script.py 1234

Python 文件示例:

#!/usr/bin/python

import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'
print '1st Argument :', str(sys.argv[1])

以下是一些文档链接:

http://docs.ansible.com/ansible/script_module.html

http://docs.ansible.com/ansible/intro_adhoc.html

【讨论】:

  • 这似乎相当复杂,我通过调用 6 行期望脚本以某种方式通过 GUI 解决了这个问题,但直到知道我不明白为什么 python 不能做同样的事情,谢谢你的努力任何方式
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-03
  • 2013-03-04
  • 2012-08-23
  • 2014-06-17
  • 2015-01-26
相关资源
最近更新 更多