每当我改变我的公网IP时,我必须在AWS console中将新的IP添加到Security group ,以允许端口22上的通信。
有什么办法来自动这个? 我的意思是说,在公共IP的每一个改变运行一个脚本(在OSX)添加新的IP到我的Security group ,允许在port 22stream量。
谢谢!
我相信有这样做的多种方式。 但我可以分享我一直在使用Python这一段时间的方式。 我没有OSX的经验,但我会假设它预装了Python,所以你应该能够做到这一点。 一个警告,但我必须安装boto ,这是一个Python API接口调用的Python接口。 当然你也可以用EC2 CLI Tools完成同样的事情。
博托安装说明可以在这里find –
http://boto.readthedocs.org/en/latest/getting_started.html
导入boto.ec2 康恩= boto.ec2.connect_to_region(“我们 - 东 - 1”) conn.authorize_security_group(group_name ='my_sec_group',ip_protocol ='tcp',from_port = '22',to_port = '22',cidr_ip ='1.2.3.4 / 32')
脚步 –
导入必要的模块
连接到任何地区
使用authorize_security_group并指定安全组名称,协议,端口和您的IP。
我只是把自动更新的脚本放在一起。 可能对别人有用,虽然它只是为个人使用而编写的:
import boto.ec2 import requests LAST_IP_FILENAME = 'last_ip.txt' AWS_REGION = '{your aws region}' GROUP_NAME = '{the security group you wanna update}' FROM_PORT = {from port} TO_PORT = {to port} AMAZON_IP_ENDPOINT = 'http://checkip.amazonaws.com/' def get_last_ip(): try: with open(LAST_IP_FILENAME, 'r') as fp: ip = fp.readline().strip() except: ip = None return ip def get_connection(): return boto.ec2.connect_to_region(AWS_REGION) def get_security_group(conn, group_name): return [s for s in conn.get_all_security_groups() if s.name == group_name].pop() def delete_ip(sg, ip): if not sg.revoke('tcp', FROM_PORT, TO_PORT, cidr_ip=ip): raise Exception('Removing ip from security group failed') def get_current_ip(): resp = requests.get(AMAZON_IP_ENDPOINT) resp.raise_for_status() return resp.content.strip() + '/32' def add_new_ip(ip): if not sg.authorize('tcp', FROM_PORT, TO_PORT, cidr_ip=ip): raise Exception('Adding ip to security group failed') def save_new_ip(ip): with open(LAST_IP_FILENAME, 'w') as fp: fp.write(ip + '\n') if __name__ == '__main__': last_ip = get_last_ip() current_ip = get_current_ip() if last_ip == current_ip: print 'Last ip and current ip are the same.. abort.' exit(0) conn = get_connection() sg = get_security_group(conn, GROUP_NAME) if last_ip is not None: print 'Found old ip {}'.format(last_ip) delete_ip(sg, last_ip) print ' ..deleted successfully..' else: print 'No old ip was found..' print 'Current ip is {}'.format(current_ip) add_new_ip(current_ip) print ' ..updated successfully' save_new_ip(current_ip)
您应该使用可以通过VPN打开整个互联网和SSH到您的服务器的VPN。 它会运行良好,安全性好(如果你能很好地处理你的证书),你不需要检测你的IP已经改变。 还有另一个好处 – 你不想让你旅行时使用的每个酒店,火车站或移动设备。
或者你可以打开端口22到互联网,这是安全的,如果你正在使用密钥,并已禁用密码的唯一身份validation。