简体   繁体   English

通过python脚本在远程计算机上安装软件

[英]installing software on remote machine via python script

this is for work... so i will share as much as i can, I'm trying to install "environment" on a remote machine via python script, this "environment" require to pass to it user name and password, I tried a lot of things, nothing seems to work... the closest thing is this script, yet after it passes the user name a GUI popup and ask for the password... what i'm doing wrong ?! 这是为了工作...所以我将尽我所能,我正在尝试通过python脚本在远程计算机上安装“环境”,此“环境”要求将用户名和密码传递给它,很多事情,似乎什么都没用……最接近的事情是这个脚本,但是在它通过用户名后,会弹出一个GUI弹出窗口并要求输入密码……我在做什么错了? or what can i do to make it work?!... here is a part of the script that deal with pexpect 或我该怎么做才能使其正常工作?!...这是处理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

this is the GUI that popup : 这是弹出的GUI: 在此处输入图片说明 if i enter the password manually (which i'm trying to avoid), i get output like this : 如果我手动输入密码(我想避免这样做),我将得到如下输出:

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.

so.. any ideas? 所以..有什么想法吗?

If you really want to do this job with only Python, why can't you use Ansible instead? 如果您真的只想用Python来完成这项工作,为什么不使用Ansible呢?

Ansible solution: Ansible解决方案:

If you have a script which installs software locally, then run that script in remote servers with the Ansible script module 如果您有在本地安装软件的脚本,请使用Ansible脚本模块在远程服务器中运行该脚本

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

Python file example : Python文件示例:

#!/usr/bin/python

import sys

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

Here are some documentation links: 以下是一些文档链接:

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

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

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

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