Guest User

Untitled

a guest
Jan 24th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #!/bin/sh
  2. IP=$(wget http://ipecho.net/plain -qO-)
  3.  
  4. Host $IP
  5.  
  6. User UserName
  7. Port 22
  8. IdentityFile ~/.ssh/id_rsa
  9.  
  10. Host home
  11.  
  12. HostName 192.168.0.1
  13.  
  14. Host away
  15.  
  16. HostName 97.113.55.62
  17.  
  18. OLDIP=`grep -w away -A 1 /etc/ssh/ssh_config | awk '/Hostname/ {print $2}'`
  19.  
  20. sed -i "s/$OLDIP/$IP/g" /etc/ssh/ssh_config
  21.  
  22. #!/bin/sh
  23. IP=$(wget http://ipecho.net/plain -qO-)
  24. OLDIP=`grep -w away -A 1 /etc/ssh/ssh_config | awk '/Hostname/ {print $2}'`
  25. sed -i "s/$OLDIP/$IP/g" /etc/ssh/ssh_config
  26.  
  27. #!/usr/bin/env python2
  28. # -*- coding: utf-8 -*-
  29.  
  30. """
  31. Script to automatically update the SSH host name when
  32. an AWS EC2 instance changes IP after reboot.
  33.  
  34. Call it like this:
  35.  
  36. $ ~/repos/scripts/update_aws_hostname.py i-06bd23cd0514c2c7d windows
  37.  
  38. """
  39.  
  40. # Built-in modules #
  41. import sys, argparse
  42. from os.path import expanduser
  43.  
  44. # Third party modules #
  45. import sshconf, boto3
  46.  
  47. # Constants #
  48. ec2 = boto3.client('ec2')
  49.  
  50. ###############################################################################
  51. class UpdateHostnameFromEC2(object):
  52. """A singleton. EC2 is provided by Amazon Web Services."""
  53.  
  54. def __init__(self, instance_id, ssh_shortcut):
  55. self.instance_id = instance_id
  56. self.ssh_shortcut = ssh_shortcut
  57.  
  58. def __call__(self):
  59. # Read SSH config #
  60. self.config = sshconf.read_ssh_config(expanduser("~/.ssh/config"))
  61. # Get new DNS #
  62. self.response = ec2.describe_instances(InstanceIds=[self.instance_id])
  63. self.new_dns = self.response['Reservations'][0]['Instances'][0]['PublicDnsName']
  64. self.tags = self.response['Reservations'][0]['Instances'][0]['Tags']
  65. self.instance_name = [i['Value'] for i in self.tags if i['Key']=='Name'][0]
  66. # Substitute #
  67. self.config.set(self.ssh_shortcut, Hostname=self.new_dns)
  68. # Write SSH config #
  69. self.config.write(expanduser("~/.ssh/config"))
  70. # Success #
  71. message = "Updated the ssh config host '%s' for '%s'."
  72. message = message % (self.ssh_shortcut, self.instance_name)
  73. print(message)
  74.  
  75. ###############################################################################
  76. if __name__ == "__main__":
  77. # Parse the shell arguments #
  78. parser = argparse.ArgumentParser(description=sys.modules[__name__].__doc__)
  79. parser.add_argument("instance_id", help="ID of the instance to update from EC2", type=str)
  80. parser.add_argument("ssh_shortcut", help="The ssh config host name (shortcut)", type=str)
  81. args = parser.parse_args()
  82.  
  83. # Create the singleton and run it #
  84. update_config = UpdateHostnameFromEC2(args.instance_id, args.ssh_shortcut)
  85. update_config()
Add Comment
Please, Sign In to add comment