0

以下のスクリプトは、root としてログインしてコマンド ラインから実行するとうまく機能しますが、Ubuntu 10.04 で /etc/rc.local を使用して最初の起動時に実行すると、約 25% の確率で失敗します (システム ルート、mysql ルート、および一部の mysql ユーザー パスワードは正しく設定されていますが、そのうちの 1 つは、標準の mysql ログイン エラーを報告するコンソール ログで失敗します。

環境変数など、考慮すべき init ジョブからの Python スクリプトの実行について何かありますか?

#!/usr/bin/env python 
# Randomizes and outputs to files the system root and mysql user passwords 
files = ['/home/ubuntu/passwords','/opt/data1/alfresco/extensions/ 
extension/alfresco-global.properties','/opt/data/etc/mysql/ 
debian.cnf','/home/ubuntu/duncil'] 
userpasswords = {'root':'ROOTPASSWORD'} 
mysqlpasswords = 
{'root':'MYSQLPASSWORD','alfresco':'alfrescoPASSWORD','debian-sys- 
maint':'debian-sys-maintPASSWORD'} 
otherpasswords = ['OTHERPASSWORD'] 
log = '/var/log/firstrun' 
import random, string 
import crypt 
import re 
from subprocess import PIPE, Popen 
def getsalt(chars = string.letters + string.digits): 
    # generate a random 2-character 'salt' 
    return random.choice(chars) + random.choice(chars) 
def getpwd(chars = string.letters + string.digits, len = 12): 
    retval = ""; 
    for i in range(0, len): 
    # generate 12 character alphanumeric password 
        retval += random.choice(chars) 
    return retval 
def replace_pass(filename): 
    handle = open(filename, 'r') 
    hbuf = handle.read() 
    handle.close() 
    for placeholder, password in pdict.iteritems(): 
        hbuf = re.sub(placeholder, password, hbuf) 
    try: 
        # Output file 
        handle = open(filename, 'w') 
        handle.write(hbuf) 
        handle.close() 
    except: 
        pass 
        #logh.write('failed to update ' + filename  + "\n") 
        #logh.write('maybe you don\'t have permision to write to it?\n') 
logh = open(log, "a") 
logh.write("Starting...\n") 
# Generate passwords 
pdict = {} 
for user, placeholder in userpasswords.iteritems(): 
    syspass = getpwd() 
    Popen(['usermod', '--password', crypt.crypt(syspass, getsalt()), user]) 
    logh.write(placeholder + ": User " + user + " --> " + syspass + "\n") 
    pdict[placeholder] = syspass 
# Whats the MySQL Root password placeholder? 
mplace = mysqlpasswords['root'] 
for user, placeholder in mysqlpasswords.iteritems(): 
    mpass = getpwd() 
    if (("root" in mysqlpasswords) and (mysqlpasswords['root'] in pdict)): 
        mrootpass = pdict[mysqlpasswords['root']] 
    else: 
        mrootpass = "" 
    Popen(['mysql', '-uroot', "--password=" + mrootpass, "-e", "UPDATE user SET Password = PASSWORD('" + mpass + "') WHERE User = '" + user + "';FLUSH PRIVILEGES;","mysql"]) 
    logh.write(placeholder + ": MySQL " + user + " --> " + mpass + "\n") 
    pdict[placeholder] = mpass 
for placeholder in otherpasswords: 
    opass = getpwd() 
    logh.write(placeholder + ": " + opass + "\n") 
    pdict[placeholder] = opass 
# Update passwords 
for file in files: 
    logh.write("Replacing placeholders in " + file + "\n") 
    replace_pass(file) 
logh.write("Finished\n") 
logh.close 
4

1 に答える 1

2

Popen非同期で実行されませんか?

起動中の負荷が高く、root パスワードの設定とそれを使用した次のパスワード (next コマンド) の設定との間で競合状態になっているようです。

試す

p = Popen(['mysql', '-uroot', "--password=" + mrootpass, "-e", "UPDATE user SET Password = PASSWORD('" + mpass + "') WHERE User = '" + user + "';FLUSH PRIVILEGES;","mysql"])
p.wait()

そして、それがうまくいくかどうかを確認してください。

于 2010-09-13T06:21:17.710 に答える