简体   繁体   English

python:如何从EC2实例在本地计算机上运行命令

[英]python: How to run command on local machine from an EC2 instance

I am trying to automate the process of creating a postgres database from production dump using a python script. 我正在尝试使用python脚本从生产转储中自动创建postgres数据库的过程。 I am running this script on an EC2 instance that hosts an RoR app server. 我在托管RoR应用程序服务器的EC2实例上运行此脚本。

I am able to create the DB using psychopg. 我可以使用psychopg创建数据库。 But in the same script I want to do the following: 但是在同一脚本中,我想执行以下操作:

- do a deploy using capistrano from local machine

I came across paramiko - SSHClient in my research. 我在研究中遇到了paramiko-SSHClient。 I think it can be used. 我认为可以使用。 But I'm not sure how I can SSH from an EC2 instance to my local machine. 但是我不确定如何从EC2实例到本地计算机进行SSH。 I found out my IP(say abcd) and tried ssh abcd from the EC2 instance but it says: 我找到了我的IP(例如abcd),并从EC2实例尝试了ssh abcd ,但它说:

ssh: connect to host a.b.c.d port 22: Connection refused

So my question is: - If this method (using paramiko) is correct, how can I set up a ssh connection from EC2 instance to my local machine OR If paramiko cannot achieve this, how can I run the deploy from my script that is on the EC2 instance 所以我的问题是:-如果此方法(使用paramiko)是正确的,我如何建立从EC2实例到我的本地计算机的ssh连接,或者如果paramiko无法实现此目的,如何从正在运行的脚本中运行部署EC2实例

For one, paramiko is a bit low-level, you could try using Fabric , it will make a lot of things much easier. 例如,paramiko有点低级,您可以尝试使用Fabric ,它将使很多事情变得更加容易。

Secondly, check whether your local machine is 其次,检查您的本地计算机是否

  • accessible from the internet, ie has public IP address 可从互联网访问,即具有公共IP地址
  • running SSH (assuming you run *NIX-like OS, run ps -ef | grep sshd , if you see a process matching, then you should be able to connect) 运行SSH(假设您运行的是类似* NIX的操作系统,请运行ps -ef | grep sshd ,如果看到匹配的进程,则应该可以连接)

Then, if you wanna automate a postgres DB creation on a EC2 instance, why do you need a connection from it to your local machine. 然后,如果要 EC2实例自动化Postgres DB创建,为什么需要从它到本地计算机的连接。 Wouldn't it be more logical to reverse the process? 逆转流程是否更合乎逻辑? Ie SSH from local machine to EC2 instance. 本地计算机到EC2实例的SSH。

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

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