2016-12-28 19 views
0

私はPythonと開発の完全なnewbです。私はプロジェクトに取り残されています。functionを使用してforループ内の変数を設定する方法はありますか?

私はSalt Masterからサーバのリストを取得する機能を持っています。私はその後、forループ内でそのリストを使用して、そのサーバーに接続し、私の公開鍵をそれらにコピーしようとしています。サーバーをハードコードすると問題なく実行されます。私のforループの変数は何も返さないようです。

ここは私のForループです。ここで

def deploy_key(key, server, username, password): 
    ssh = paramiko.SSHClient() 
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
    ssh.connect(server, username=username, password=password) 
    transport = ssh.get_transport() 
    session = transport.open_session() 
    session.set_combine_stderr(True) 
    session.get_pty() 
    session = transport.open_session() 
    session.set_combine_stderr(True) 
    session.get_pty() 
    for commands in l_commands: 
     session = transport.open_session() 
     session.set_combine_stderr(True) 
     session.get_pty() 
     session.exec_command(commands) 
     stdin = session.makefile('wb', -1) 
     stdout = session.makefile('rb', -1) 
     stdin.write(password + '\n') 
     stdin.flush() 

username = "myUserName" 
password = "myPassword" 
server = hosts() 
user = getUser() 
key = getKey() 
l_commands = ['sudo mkdir -p /home/%s/.ssh/' % user,'sudo chmod -R 777 /home/%s' %user, 
      'sudo echo "%s" >> /home/%s/.ssh/authorized_keys' %(key, user), 
      'sudo chmod 644 /home/%s/.ssh/authorized_keys' % user, 
      'sudo chmod 700 /home/%s/.ssh/' % user, 'sudo chmod 755 /home/%s' %user] 

for host in server: 
    deploy_key(key, host, username, password) 

は、問題がhosts()メソッドのreturn文にある変数

import paramiko 

l_password = "myPassword" 
l_host = "saltMaster.salt.com" 
l_commands = "sudo salt-key -L" 
l_user = "myUser" 

def hosts(): 
    ssh = paramiko.SSHClient() 
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
    ssh.connect(l_host, username=l_user, password=l_password) 
    transport = ssh.get_transport() 
    session = transport.open_session() 
    session.set_combine_stderr(True) 
    session.get_pty() 
    session = transport.open_session() 
    session.set_combine_stderr(True) 
    session.get_pty() 
    session.exec_command(l_commands) 
    stdin = session.makefile('wb', -1) 
    stdout = session.makefile('rb', -1) 
    stdin.write(l_password + '\n') 
    stdin.flush() 

    for line in stdout.read().splitlines(): 
     input_line = line 
     input_line = input_line.replace(b"\x1b[0;32m", b'') # remove \x1b[1;32m 
     input_line = input_line.replace(b"\x1b[0;0m", b'') # remove \x1b[1;35m 
     input_line = input_line.replace(b"\x1b[0;1;34mRejected Keys:", b'') # remove \x1b[1;36m 
     input_line = input_line.replace(b"\x1b[0;1;31mUnaccepted Keys:", b'') # remove \x1b[1m 
     input_line = input_line.replace(b"\x1b[0;1;35mDenied Keys:", b'') # remove \x07 (BEL) 
     input_line = input_line.replace(b"\x1b[0;1;32mAccepted Keys:", b'') # remove \x07 (BEL) 
     input_line = input_line.replace(b"Freedom1", b'') # remove \x07 (BEL) 

     hostsIndividual = str(input_line,"utf-8") 
     return(hostsIndividual) 
+0

あなたのforループの前に 'l_commands'は何を保持していますか?あなたが望むものであることを確かめるためにそれを印刷できますか?あなたの 'host'関数のために、なぜあなたは最初のforループ反復の後に戻りますか?それがちょうど1回の反復であるなら、最初にforループが必要なのはなぜですか? – MooingRawr

+0

'hosts()'関数は、 'return'ステートメントを内部に' for'ループがあるので、ただ一つの 'hostsIndividual'値を返します。 – martineau

答えて

1

を得るために、私の関数です。関数は、ループの最初の反復で単一Stringを返しますよう

def hosts(): 
    // your code goes here 

    for line in stdout.read().splitlines(): 
     // your code goes here 
     hostsIndividual = str(input_line,"utf-8") 
     return(hostsIndividual) 

ここでループが一度だけ実行されます。最初にループの外側にreturn文を置くべきです。

def hosts(): 
    // your code goes here 

    for line in stdout.read().splitlines(): 
     // your code goes here 
     hostsIndividual = str(input_line,"utf-8") 

    return(hostsIndividual) // returning only one host name 

また、サーバー名(文字列)のlistを使用すると、以下のように、むしろのみhostIndividualを返すhosts()方法(単一のString)からそれを返す必要があります。

def hosts(): 
    // your code goes here 

    hosts = [] 
    for line in stdout.read().splitlines(): 
     // your code goes here 
     hostsIndividual = str(input_line,"utf-8") 
     hosts.append(hostsIndividual) 

    return(hosts) // now returning a list of host name 

今、あなたはhosts()によって返されたサーバのリスト(文字列のリスト)を反復処理することができます。

for host in server: 
    deploy_key(key, host, username, password) 
1

ループは、最初の入力行を1つの文字列で返します。 このように、あなたのメインプログラムでは、サーバーは、その文字列の文字によってその単一の文字列、およびお使いのループ

for host in server: 

反復しています。これは、一つのサーバでそれぞれの文字列のリストを作成し

server_list = [] 
for line in ... 
    ... 
    hostsIndividual = str(input_line,"utf-8") 
    server_list.append(hostsIndividual) 

return(server_list) 

使用

代わり

for line in ... 
    ... 
    hostsIndividual = str(input_line,"utf-8") 
    return(hostsIndividual) 

の...あなたの関数のループにこの変更を作ってみましょう。 の後にループが実行されると、リスト全体がメインプログラムに返されます。

関連する問題