简体   繁体   English

python替换列表中的元素

[英]python replace an element in a list

I want to build a ssh_config and an .alias file out of password manager database. 我想从密码管理器数据库中构建一个ssh_config和.alias文件。 From the database I get all my information via JSON. 从数据库中,我通过JSON获取所有信息。 The ssh_config should then look like the following: ssh_config应该如下所示:

Servername
    User foo
    Port 22
    Hostname Servername

Additional values can occur. 可能会出现其他值。 The alias file gets the same values, but in one line: 别名文件获得相同的值,但只占一行:

alias Servername="ssh -p 22 foo@Servername"

So far so good this works fine. 到目前为止,一切正常。 But now the password database has several entries and some are the same server, but different users. 但是现在密码数据库有几个条目,有些条目是同一服务器,但用户不同。 I want a file for usernames only start with "foo" or "bar" and for any other I assume there are admin users and if there is an entry with such an admin user I'd like to have this user instead of any "foo" or "bar" This is currently my function to create ssh_config and alias file 我想要一个用于用户名的文件仅以“ foo”或“ bar”开头,对于其他任何文件,我都假定有管理员用户,如果有这样的管理员用户条目,我希望拥有该用户而不是任何“ foo” “或” bar“这是我目前创建ssh_config和别名文件的功能

# define function to add values to files
def fillSSH ( item, ssh_config, alias ):
    # split item name, first is customer server
    shortname = item.get('name').split()            
    # append Host entry to ssh_config
    ssh_config.append('Host ' + shortname[0])
    # append User entry to ssh_config
    ssh_config.append('   User ' + access_url[3])
    # If there is a :[port] at the end, we have 6 objects
    if len(access_url) >= 6:
        # append Port entry to ssh_config if it exists
        ssh_config.append('   Port ' + access_url[5])
        # create the alias entry just once
        aliasurl = 'alias ' + shortname[0] + '="ssh -p' + access_url[5] + ' ' + access_url[3] + '@' + access_url[4] + '"'
    else: 
        # create the alias entry just once
        aliasurl = 'alias ' + shortname[0] + '="ssh ' + access_url[3] + '@' + access_url[4] + '"'
    # append HostName entry to ssh_config
    ssh_config.append('   HostName ' + access_url[4])
    # append empty line 
    ssh_config.append('')

    # append entry for alias to list
    alias.append(aliasurl)

Now I am not sure how to proceed with my different users. 现在,我不确定如何继续使用其他用户。 I have something like 我有类似的东西

    # split access url by : / @
    access_url = re.split(':|/|@',item.get('access_info'))   
    # if foo or bar, for everyone
    if access_url[3] == 'foo' or access_url[3] == 'bar':
        fillSSH (item, ssh_config, alias)
    # else just for admins if not seen before
    else:
        fillSSH (item, ssh_config_admins, alias_admins)

So currently I have two ideas how I could proceed here. 因此,目前我有两个想法如何在这里进行。 1) I have to add a var "seen" where I add all server names. 1)我必须在所有服务器名称上添加一个变量“ seen”。 If I have seen a Servername and the new user is either "foo" or "bar" I can ignore the entry for my admin list and add it to the normal list instead. 如果我看到服务器名,并且新用户是“ foo”或“ bar”,则可以忽略管理员列表的条目,并将其添加到普通列表中。 But if I have seen the Servername and the new user isn't "foo" or "bar", then I need to find the old entry in ssh_config_admins to replace it. 但是,如果我看到服务器名并且新用户不是“ foo”或“ bar”,那么我需要在ssh_config_admins中找到旧条目来替换它。

2) Another idea would be to just create a list with all "foo" and "bar" users and a list with all other users. 2)另一个想法是只创建一个包含所有“ foo”和“ bar”用户的列表,以及一个包含所有其他用户的列表。 But I'd like to have "foo" and "bar" users for my admins as well if there is no other login for that server, so how to merge them in this case? 但是,如果该服务器没有其他登录名,我也希望管理员拥有“ foo”和“ bar”用户,那么在这种情况下如何合并它们? I think then I have to walk twice through the list, once to get all non-"foo" and "bar" users and the second time to get just foo and bar and if the servername is already in my list I just add them to the all list and not to the admins list. 我认为,然后我必须遍历该列表两次,一次获取所有非“ foo”和“ bar”用户,第二次获取仅foo和bar,如果服务器名已经在我的列表中,我只需将其添加到全部列表,而不是管理员列表。

So for both cases I have no idea how to really solve them, maybe someone knows how or even another way I could go here. 因此,对于这两种情况,我都不知道如何真正解决它们,也许有人知道我什至可以怎么做。

If I understand correctly, you're trying to build two config files: 如果我理解正确,则您正在尝试构建两个配置文件:

  • one with all the known hosts, with one entry per server, with the credentials of users "foo" or "bar" only if there is no other user specified 一个具有所有已知主机的主机,每个服务器一个条目,仅在没有指定其他用户的情况下,使用用户“ foo”或“ bar”的凭据
  • one will all the credentials of users "foo" and "bar", so not necessarily all the known hosts 一个将所有用户“ foo”和“ bar”的凭据,因此不一定是所有已知主机

Is this correct? 这个对吗?

If so, one solution would be to do two passes: 如果是这样,一种解决方案是进行两次通过:

  1. Build a dict containing the parsed info for all the known hosts, separating between admin and foo/bar user credential. 构建一个dict,其中包含所有已知主机的解析信息,并在admin和foo / bar用户凭据之间进行分隔。 For example, it could take that form 例如,它可以采用这种形式

     { 'server1': { 'admin': { 'username': 'blah', 'port': 22, etc. }, 'foobar': { 'username': 'foo', 'port': 22', etc. } }, 'server2': { 'foobar': { 'username': 'bar', 'port': 332, etc. } } } 
  2. Loop through this created dict, and build your admin file by either taking the "admin" credentials, or the "foobar" credentials if there is no "admin" credential. 遍历此创建的字典,并通过获取“ admin”凭据或“ foobar”凭据(如果没有“ admin”凭据)来构建您的管理文件。 For example, something like this: 例如,如下所示:

     for server, creds in enumerate(hosts): credentials = creds.get('admin') or creds.get('foobar') fill_admin_file(credentials) 

Is that what you're trying to do? 那是你想做的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM