65045

SSH to EC2 instance using boto on private IP through bastion server

<h3>Question</h3>

I am trying to execute some bash script on EC2 instance using boto. Boto provides a way SSH to EC2 instance on public IP but in my case the instances have only private IP. The way SSH is done on these instance is using a host which can SSH on all the instance using private IP (Bastion host). Following is the script to connect to instance on public IP:

s3_client = boto3.client('s3') s3_client.download_file('mybucket','key/mykey.pem', '/tmp/mykey.pem') k = paramiko.RSAKey.from_private_key_file("/tmp/mykey.pem") c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) host=event print "Connecting to " + host c.connect( hostname = host, username = "ec2-user", pkey = k )

How to connect to instances if host have private IP instead of public key if we want to connect through bastion host with public IP P.P.P.P


<h3>Answer1:</h3>

If your requirement is to trigger execution of some code on an Amazon EC2 instance, then it would be better to use the Amazon EC2 Run Command rather than try to automate an SSH connection.

<blockquote>

Amazon EC2 Run Command provides a simple way of automating common administrative tasks like executing Shell scripts and commands on Linux, running PowerShell commands on Windows, installing software or patches, and more. Amazon EC2 Run Command allows you to execute these commands across multiple instances and provides visibility into the results, making it easy to manage configuration change across fleets of instances.

</blockquote>

Your instances would need the Amazon EC2 Systems Manager (SSM) agent installed. See: Installing SSM Agent

You would then run commands on Amazon EC2 instances from the management console, AWS Command-Line Interface (CLI) or via an API call.

The send command does not accept tags as input. However, you could first perform a list-instances command to search for instances by tag, then pass the instance-ids to the send command. See: AWS CLI send-command

来源:https://stackoverflow.com/questions/44090141/ssh-to-ec2-instance-using-boto-on-private-ip-through-bastion-server

Recommend

  • How do I attach a listener to a date selection in the Django admin?
  • SSH to EC2 instance using boto on private IP through bastion server
  • SSH to EC2 instance using boto on private IP through bastion server
  • windows服务器 Nginx 、tomcat集群搭建及session共享解决
  • Eclipse gives dead code warning for reachable code
  • Overload built in function in Haskell
  • What does 1>&2 mean in shell? [duplicate]
  • Create .wav from multiple sine waves and play all at once [closed]
  • Fetch friend list from Google Plus
  • unable to create jar when adding the package declaration
  • Geofencing in background on Android 8 or 9 does not work
  • How to search a list of Objects that are displayed in a RecyclerView?
  • One-to-many relationship getting error in Entity Framework Core when trying to insert data
  • Application crashes with Assertion Failure message while updating SQLite database
  • PyQt: connecting slots with custom objects via Qt Designer
  • Get Table Cell Values Google Docs
  • HTML5 Video Play/Pause issue
  • Inspect common lisp macro source
  • MySQL - Filter records which date is biggest
  • Visual Studio MSB3721 error when compiling a __device__ function call from another file
  • Please update your Node runtime to version >=0.12.x
  • how to add dashed border on highcharts “area” graph for every point
  • C++ STL stack pop operation giving segmentation fault
  • DataTables move rows between tables
  • iphone image to video issue in video speed
  • Computing the discrete fourier transform of audio data with FFTW
  • Using Service Component Runtime
  • convert json to excel in java
  • Create/delete users from text file using Bash script