简体   繁体   中英

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 ?! or what can i do to make it work?!... here is a part of the script that deal with 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 : 在此处输入图片说明 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?

Ansible solution:

If you have a script which installs software locally, then run that script in remote servers with the Ansible script module

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

Python file example :

#!/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/intro_adhoc.html

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