2017-01-08 66 views
0

這是爲了工作......所以我會盡我所能分享,我試圖通過python腳本在遠程機器上安裝「環境」,這個「環境」要求傳遞給它的用戶名和密碼,我嘗試了很多東西,似乎沒有任何工作......最接近的是這個腳本,但它通過用戶名後彈出一個GUI並要求輸入密碼......我是什麼做錯了嗎?! ?!或者我能做些什麼,使工作...這裏是與Pexpect的處理腳本的一部分通過python腳本在遠程機器上安裝軟件

import os 
import pexpect 

cmd = 'ssh -X [email protected] 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: enter image description here 如果我手動輸入密碼(這我試圖避免),我得到這樣的輸出:

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?

+0

說實話......我所有的方法來解決這個問題,可能是錯的,我願意聽到** **任何一種解決方案。 –

回答

0

如果你真的只想用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

+0

它看起來相當複雜,我通過調用6行來解決這個問題,期望通過某種方式傳遞給GUI的腳本,但直到知道我不明白爲什麼python不能這樣做,感謝任何方式的努力 –

相關問題