2016-12-28 65 views
0

我是一個Python和開發人員的完整新手,我被困在一個項目中。如何使用函數來填充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) 

這裏是我的函數來獲取變量

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循環迭代之後返回?如果只是一個迭代,爲什麼首先需要for循環呢? – MooingRawr

+0

'hosts()'函數只會返回一個'hostsIndividual'值,因爲你有一個'return'語句在其中的'for'循環。 – martineau

回答

1

的問題是在hosts()方法的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) 

這裏循環將只運行一次作爲功能將在循環的第一個迭代返回單個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) // returning only one host name 

你還需要使用服務器名稱(串)的listhosts()方法,而只返回hostIndividual(一個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

您的循環返回單個字符串,即您找到的第一個輸入行。 因此,在你的主程序,服務器是一家單一的字符串,你的循環

for host in server: 

遍歷該字符串的字符。試着做此更改你的函數的循環...而不是

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

使用

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

return(server_list) 

這將構建串,每一個都是一個服務器的列表; 循環完成後,它將整個列表返回給主程序。