小编blu*_*ndr的帖子

无法使用 jq 将 JSON 输出转换为 CSV 格式

我正在构建 AWS EBS 卷属性列表,以便可以使用 jq 将其作为 CSV 存储在变量中。我要将变量输出到电子表格中。

第一个命令给出了我正在使用 jq 查找的值:

aws ec2 describe-volumes | jq -r '.Volumes[] | .VolumeId, .AvailabilityZone, .Attachments[].InstanceId, .Attachments[].State, (.Tags // [] | from_entries.Name)'
Run Code Online (Sandbox Code Playgroud)

给出我想要的输出,如下所示:

MIAPRBcdm0002_test_instance
vol-0105a1678373ae440
us-east-1c
i-0403bef9c0f6062e6
attached
MIAPRBcdwb00000_app1_vpc
vol-0d6048ec6b2b6f1a4
us-east-1c
MIAPRBcdwb00001 /carbon
vol-0cfcc6e164d91f42f
us-east-1c
i-0403bef9c0f6062e6
attached
Run Code Online (Sandbox Code Playgroud)

但是,如果我将其转换为 CSV 格式以便可以将变量输出到电子表格,则该命令会崩溃并且不起作用:

aws ec2 describe-volumes | jq -r '.Volumes[] | .VolumeId, .AvailabilityZone, .Attachments[].InstanceId, .Attachments[].State, (.Tags // [] | from_entries.Name)| @csv'
jq: error (at <stdin>:4418): string ("vol-743d1234") cannot be csv-formatted, only array
Run Code Online (Sandbox Code Playgroud)

对于 EBS 卷,即使将 JSON 的顶层转换为 …

csv json flatten jq

2
推荐指数
1
解决办法
2450
查看次数

Catch specific exceptions in Python

My script works fine if I catch all exceptions to an error I was experiencing.

However if I try to limit it to just one exception, this is the error I get:

except botocore.ProfileNotFound:
NameError: name 'botocore' is not defined
Run Code Online (Sandbox Code Playgroud)

This is my code:

import boto3
while True:
    try:
        aws_account = input("Enter the name of the AWS account you'll be working in: ")
        session = boto3.Session(profile_name=aws_account)
        resource = session.resource('iam')
        client = session.client('iam')
        kms_client = session.client('kms')
        secrets_client = session.client('secretsmanager')
        break
    except …
Run Code Online (Sandbox Code Playgroud)

python boto3

2
推荐指数
1
解决办法
185
查看次数

Mongodb [js] 未捕获的异常:SyntaxError:标识符在数字文字之后立即开始

我有一个 MongoDB 集合,名为:ec2_list-07-26-2020

如果我尝试对其进行典型操作,find()或者validate()出现此错误:

db.ec2_list-07-26-2020.find()
2020-07-26T20:17:16.845-0400 E  QUERY    [js] uncaught exception: SyntaxError: identifier starts immediately after numeric literal :
@(shell):1:18
Run Code Online (Sandbox Code Playgroud)

所以我创建了一个名为:的测试集合audit00。当我对该集合执行相同操作时,效果很好:

 db.audit00.find()
{ "_id" : ObjectId("5f1e1e02807681425a06fa07"), "writeConcern" : "yes", "ordered" : "True" }
Run Code Online (Sandbox Code Playgroud)

和:

db.audit00.validate()
{
        "ns" : "aws_inventories.audit00",
        "nInvalidDocuments" : NumberLong(0),
        "nrecords" : 1,
        "nIndexes" : 1,
        "keysPerIndex" : {
                "_id_" : 1
        },
        "valid" : true,
        "warnings" : [
                "Some checks omitted for speed. use {full:true} option to do more thorough …
Run Code Online (Sandbox Code Playgroud)

mongodb

2
推荐指数
1
解决办法
1815
查看次数

firewall-cmd删除富规则失败

我正在尝试从firewall-cmd中删除一些丰富的规则,它似乎工作:

 firewall-cmd --remove-rich-rule 'rule family="ipv4"   source address="10.4.220.143/32"   port protocol="tcp" port="13782" accept'
Run Code Online (Sandbox Code Playgroud)

成功

但在我重新加载规则并再次检查后,规则仍然存在:

firewall-cmd --reload
Run Code Online (Sandbox Code Playgroud)

成功

# firewall-cmd --list-all
    rule family="ipv4" source address="10.4.220.143/32" port port="13724" protocol="tcp" accept
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

firewalld

1
推荐指数
1
解决办法
9698
查看次数

bash脚本if/else不区分大小写的答案

我在bash脚本中有一个if/else语句,询问是否要从快照ID创建EBS卷.

我想创建一个类似的行:if [ "$from_snapshot" -eq "Y|y" ]但我认为这不会起作用.

这就是我所拥有的:

  if [ "$from_snapshot" -eq "Y" ]
    then
      echo "Enter Snapshot ID: "
      read -r snapshot_id
   elif "$from_snapshot" -eq "y"
      echo "Enter Snapshot ID: "
      read -r snapshot_id
   else 
      echo "No Snapshot Required"
   fi
Run Code Online (Sandbox Code Playgroud)

如果有什么办法我可以更简洁地陈述一下?

bash

1
推荐指数
1
解决办法
67
查看次数

在 while 循环 bash 中通过多个值重复输出循环

我试图在 bash 中使用 while 循环来遍历 3 个文件的内容。它们包含名称、aws 账户和 aws 账号的列表。

但循环不正确,它不断重复列表中的第一个名称和列表中的第一个 aws 环境。

这是我看到的输出:

AWS user name: aadatiya does not exist in AWS account: company-lab
AWS user name: aadatiya does not exist in AWS account: company-lab
AWS user name: aadatiya does not exist in AWS account: company-lab
AWS user name: aadatiya does not exist in AWS account: company-lab
AWS user name: aadatiya does not exist in AWS account: company-lab
AWS user name: aadatiya does not exist in AWS account: company-lab …
Run Code Online (Sandbox Code Playgroud)

bash

1
推荐指数
1
解决办法
824
查看次数

AWS Athena 中的小数点舍入

我在 Amazon Athena 中有一些列显示订单项成本。

这些数字在 select 语句中显示如下:

0.0137222782
0.005445911
8.237777E-4
Run Code Online (Sandbox Code Playgroud)

我只是对信息进行基本选择:

SELECT line_item_unblended_cost FROM company_cost_allocation WHERE line_item_unblended_cost !=0;
Run Code Online (Sandbox Code Playgroud)

如何将这些总数四舍五入到小数点后第二位?

sql amazon-web-services amazon-athena

1
推荐指数
1
解决办法
2万
查看次数

删除完整 S3 存储桶 CLI

我需要删除其中包含一些对象的 S3 存储桶:

aws s3 rb --force  s3://ansible.prod-us-east 
remove_bucket failed: s3://ansible.prod-us-east An error occurred (BucketNotEmpty) when calling the DeleteBucket operation: The bucket you tried to delete is not empty. You must delete all versions in the bucket.
Run Code Online (Sandbox Code Playgroud)

我也尝试过这个:

aws s3api delete-bucket --bucket "ansible.prod-us-east" --region "us-east-1" 

An error occurred (BucketNotEmpty) when calling the DeleteBucket operation: The bucket you tried to delete is not empty. You must delete all versions in the bucket.
Run Code Online (Sandbox Code Playgroud)

错误表明存储桶已满。但是当我在命令行上列出它或查看控制台中的存储桶时,存储桶已经是空的。

当我尝试从控制台删除存储桶时,出现同样的错误。存储桶是空的,但错误表明存储桶已满,我无法删除该存储桶。

我怎样才能完成这件事?

amazon-s3 amazon-web-services aws-cli

1
推荐指数
1
解决办法
7456
查看次数

Python pandas 将 mongodb 集合无序导出到 CSV 列

我有一个 Python 脚本,用于在我们所有 AWS 账户(大约 150 个)中创建 EC2 实例列表,并将结果存储在 MongoDB 中。

我正在使用 Python pandas 模块将 mongodb 集合导出到 CSV 文件。它可以工作,只是标题乱序,而且我不想打印 MongoDB 索引。

在脚本的原始版本中(添加数据库之前),我使用 CSV 模块来写入文件,并且标题是正确的: csv文件输出没有mongo

我添加数据库既是为了学习练习,也是因为它可以让我们更轻松地处理我们拥有的所有亚马逊账户。

如果我查看 mongo 数据库中的 json 集合,我将打印所有字段的顺序都是正确的:

{'_id': ObjectId('5f14f9ffa40de31278dade03'), 'AWS Account': 'jf-master-pd', 'Account Number': '123456789101', 'Name': 'usawsweb001', 'Instance ID': 'i-01e5e920b4d3d5dcb', 'AMI ID': 'ami-006219aba10688d0b', 'Volumes': 'vol-0ce8db4e071bc7229, vol-099f6d212a91121d0, vol-0bb36e343e9c01374, vol-05610645edfd02253, vol-05adc01d70d75d649', 'Private IP': '172.31.62.168', 'Public IP': 'xx.xx.xx.xx', 'Private DNS': 'ip-172-31-62-168.ec2.internal', 'Availability Zone': 'us-east-1e', 'VPC ID': 'vpc-68b1ff12', 'Type': 't2.micro', 'Key Pair Name': 'jf-timd', 'State': 'running', 'Launch Date': 'July 20 2020'} …
Run Code Online (Sandbox Code Playgroud)

python mongodb amazon-web-services

1
推荐指数
1
解决办法
1万
查看次数

Python创建目录错误当该文件已经存在时无法创建文件

仅当目录不存在时,我才尝试使用 Python 创建目录。

如果该目录不存在,则脚本运行良好。但如果它已经存在,我会收到一个错误消息:

An error has occurred: [WinError 183] Cannot create a file when that file already exists: '..\\..\\source_files\\aws_accounts_list'
Traceback (most recent call last):
  File ".\aws_ec2_list_instances.py", line 781, in <module>
    main()
  File ".\aws_ec2_list_instances.py", line 654, in main
    mongo_export_to_file(interactive, aws_account, aws_account_number)
  File "C:\Users\tdun0002\OneDrive - Synchronoss Technologies\Desktop\important_folders\Jokefire\git\jf_cloud_scripts\aws_scripts\python\aws_tools\ec2_mongo.py", line 292, in mongo_export_to_file
    create_directories()
  File "C:\Users\tdun0002\OneDrive - Synchronoss Technologies\Desktop\important_folders\Jokefire\git\jf_cloud_scripts\aws_scripts\python\aws_tools\ec2_mongo.py", line 117, in create_directories
    os.makedirs(source_files_path)
  File "C:\Users\tdun0002\AppData\Local\Programs\Python\Python38-32\lib\os.py", line 223, in makedirs
    mkdir(name, mode)
FileExistsError: [WinError 183] Cannot create a file when that file already …
Run Code Online (Sandbox Code Playgroud)

python

1
推荐指数
1
解决办法
885
查看次数