4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SSM PortForwardでのWindows RDPもhost名でつなぎたい

Posted at

前回の続きです

SSM PortForwardによるRDP接続もInstanceIDが必要・コマンドが長ったらしいのでスクリプト化してみました
通常なら下のような感じで実行し、RDPの接続先として localhost:13389 を指定することになります

aws ssm start-session --target i-xxxxxxxxxxxxxxxx --document-name AWS-StartPortForwardingSession --parameters "portNumber=3389,localPortNumber=13389" --profile yuzuriha

コードはたいして変わってませんw
awscliのところぐらいです

rdp_proxy.py
#!/usr/bin/python

import sys
import os
import subprocess
import argparse

from boto3.session import Session


def start_port_forward(profile, hostname, port):
    session = Session(profile_name = profile)
    client = session.client('ec2')
    try:
        response = client.describe_instances(
            Filters = [
                {
                    'Name':   'tag:Name',
                    'Values': [hostname]
                }
            ]
        )
    except:
        return

    subprocess.call([
        'aws',
        '--profile',
        profile,
        'ssm',
        'start-session',
        '--target',
        response['Reservations'][0]['Instances'][0]['InstanceId'],
        '--parameters',
        'portNumber=3389,localPortNumber={0}'.format(port),
        '--document-name',
        'AWS-StartPortForwardingSession',
    ])


def main():
    parser = argparse.ArgumentParser(
        formatter_class = argparse.RawTextHelpFormatter,
    )
    parser.add_argument('--profile')
    parser.add_argument('--host')
    parser.add_argument('--port', default=13389)
    args = parser.parse_args()

    start_port_forward(args.profile, args.host, args.port)

    sys.exit(0)


if __name__ == '__main__':
    main()
4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?