linux 如何将samba-tool中的txt文件转换为bash脚本中的json文件

i5desfxk  于 2022-11-02  发布在  Linux
关注(0)|答案(2)|浏览(470)

在开始我想写我只是学习写脚本.我有一个测试域“universum.local”在VBox设置在Ubuntu 22. 04 ADDC桑巴舞.我想查询一个域控制器为域用户(10)的列表与bash脚本和保存有关他们的数据到一个json文件.目前我能够得到必要的信息,并保存到一个txt文件.
下面是我的脚本代码:


# !/bin/bash

clear

ldapuserslistfilename="ldapuserslist.txt"
ldapuserslistfile="$tmp/$ldapuserslistfilename"

ldapusersinfofilename="ldapusersinfo.txt"
ldapusersinfofile="$tmp/$ldapusersinfofilename"

# main code

touch $ldapuserslistfile
touch $ldapusersinfofile

samba-tool user list > $ldapuserslistfile

while read -r line ; do
 for user in $line ; do
  samba-tool user show $user >> $ldapusersinfofile
 done
done < $ldapuserslistfile

# copying txt files for tests

cp $ldapuserslistfile /mnt
cp $ldapusersinfofile /mnt

# deleting files

if [ -f $ldapuserslistfile ] ; then rm -f $ldapuserslisfile ; fi
if [ -f $ldapusersinfofile ] ; then rm -f $ldapusersinfofile ; fi

有输出,所有用户都保存在txt文件中,格式如下:

dn: CN=Bruce Banner,OU=Users,OU=MARVEL,OU=UNIVERSUM,DC=universum,DC=local
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: user
cn: Bruce Banner
sn: Banner
givenName: Bruce
instanceType: 4
whenCreated: 20220926075536.0Z
whenChanged: 20220926075536.0Z
displayName: Bruce Banner
uSNCreated: 4128
name: Bruce Banner
objectGUID: d1fb86d4-17bc-43f2-af83-ca06fa733e9e
badPwdCount: 0
codePage: 0
countryCode: 0
badPasswordTime: 0
lastLogoff: 0
lastLogon: 0
primaryGroupID: 513
objectSid: S-1-5-21-2846706046-4262971904-2743650290-1109
accountExpires: 9223372036854775807
logonCount: 0
sAMAccountName: hulk
sAMAccountType: 805306368
userPrincipalName: hulk@universum.local
objectCategory: CN=Person,CN=Schema,CN=Configuration,DC=universum,DC=local
pwdLastSet: 0
userAccountControl: 512
uSNChanged: 4132
memberOf: CN=Avengers,OU=Groups,OU=MARVEL,OU=UNIVERSUM,DC=universum,DC=local
distinguishedName: CN=Bruce Banner,OU=Users,OU=MARVEL,OU=UNIVERSUM,DC=universum,DC=local

我希望有json格式的数据,如

{
  "users": [
            {
             "cn" : "Bruce Banner",
             "sn" : "Banner",
             "givenName" : "Bruce",
             "whenCreated" : "20220926075536.0Z",
             "<objectname>" : "<value>",
             "<objectname>" : "<value>",
            },
            {
             <next user info>
            },
            {
             <next user info>
            }
           ]
}

objectname是下一个用户项,如lastLogon、lastLogoff等。我希望将所有用户保存在json文件中,以便可以使用计算机上的另一个powershell脚本读取它们
最新消息:
我添加了下面的行


# conversion fron txt to json

jsonfilename="jsontestfile.json"
json="./$jsonfilename"
touch $json

ed -s $ldapusersinfofile  << 'EOF' > $json
v/^cn:\|^sn:\|^givenName:\|^displayName:\|^name:\|^whenCreated:/d
,s/^\(.*[^:]*\): \(.*\)/"\1": "\2"/
g/cn\|sn\|givenName\|displayName\|name\|whenCreated/s/$/,/
,s/^/   /
g/lastLogon/t. \
s/.*/},/g
1,$-1g/}/t. \
s/.*/{/g
0a
{
.
$s/,//
,p
Q
EOF

在#main code section和# copying txt files for tests section之间,我将输出到json文件,如

{
   "cn": "James Rhodes",
   "sn": "Rhodes",
   "givenName": "James",
   "whenCreated": "20220926075852.0Z",
   "displayName": "James Rhodes",
   "name": "James Rhodes",
   "lastLogon": "0"
},
{
   "cn": "T'Chala",
   "givenName": "T'Chala",
   "whenCreated": "20220926081521.0Z",
   "displayName": "T'Chala",
   "name": "T'Chala",
   "lastLogon": "0"
},
{
   "cn": "Stephen Strange",
   "sn": "Strange",
   "givenName": "Stephen",
   "whenCreated": "20220926080942.0Z",
   "displayName": "Stephen Strange",
   "name": "Stephen Strange",
   "lastLogon": "0"
}

为了能够读取powershells脚本中的jsonfile,缺少

{
        "users": [

在开始数据和

]
    }

在数据的末尾有这样的文件

{
    "users": [
                   {
                    "cn": "James Rhodes",
                    "sn": "Rhodes",
                    "givenName": "James",
                    "whenCreated": "20220926075852.0Z",
                    "displayName": "James Rhodes",
                    "name": "James Rhodes",
                    "lastLogon": "0"
                   },
                   {
                    "cn": "T'Chala",
                    "givenName": "T'Chala",
                    "whenCreated": "20220926081521.0Z",
                    "displayName": "T'Chala",
                    "name": "T'Chala",
                    "lastLogon": "0"
                   },
                   {
                    "cn": "Stephen Strange",
                    "sn": "Strange",
                    "givenName": "Stephen",
                    "whenCreated": "20220926080942.0Z",
                    "displayName": "Stephen Strange",
                    "name": "Stephen Strange",
                    "lastLogon": "0"
                   }
               ]
}

通过PS脚本读取

Clear
$json = Get-Content <pathToFile>\jsontestfile.json -Raw | ConvertFrom-Json

foreach ($user in $json.users){
 echo $user.cn
 echo $user.sn
 echo $user.givenName
 echo "----------"
}

如何添加缺少的字符?

iszxjhcz

iszxjhcz1#

您需要'jq',然后解析samba-tool的输出,如下所示:

sudo samba-tool user show $USERNAME -H ldap://<DC_SHORT_HOSTNAME> -d0 | jq -R -s -c 'split("\n")'

您可以轻松地在用户名列表周围使用“for循环”。

fdx2calv

fdx2calv2#

我将代码更改为:


# add prefix

printf '{\n"users" :\n[\n' > $json

# add converted users data

ed -s $data << 'EOF' >> $json
v/^cn:\|^sn:\|^givenName:\|^whenCreated:\|^displayName:\|^name:\|^badPwdCount:\|^badPasswordTime:\|^lastLogoff:\|^primaryGroupID:\|^accountExpires:\|^sAMAccountName:\|^userPrincipalName:\|^pwdLastSet:\|^userAccountControl:\|^lastLogonTimestamp:\|^whenChanged:\|^lastLogon:\|^logonCount:\|^distinguishedName:/d
,s/^\(.*[^:]*\): \(.*\)/"\1" : "\2"/
g/cn\|sn\|givenName\|whenCreated\|displayName\|name\|badPwdCount\|badPasswordTime\|lastLogoff\|primaryGroupID\|accountExpires\|sAMAccountName\|userPrincipalName\|pwdLastSet\|userAccountControl\|lastLogonTimestamp\|whenChanged\|lastLogon\|logonCount/s/$/,/
,s/^/  /
g/distinguishedName/t. \
s/.*/},/g
1,$-1g/}/t. \
s/.*/{/g
0a
{
.
$s/,//
,p
Q
EOF

# add sufix

printf ']\n}' >> $json

它现在可以工作了,Powershell可以读取我的文件。

相关问题