Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#! /usr/bin/env Rscript
#start an instance
startup <- system("ec2-run-instances ami-b232d0db -k ec2ApiTools", intern=T)
Sys.sleep(45) #rather random but 45 seconds is typically long enough to boot
#query running instances
instances <- system("ec2-describe-instances", intern=T)
instancesParsed <- sapply(instances, strsplit, "\t")
runningInstances <- NULL
ri <- 1
latestStart <- strptime("1900-01-01T00:00:00", "%Y-%m-%dT%H:%M:%S")
for (i in 1:length(instancesParsed)){
if (instancesParsed[[i]][[1]]=="INSTANCE") {
if (instancesParsed[[i]][[6]]=="running") {
runningInstances[[ri]] <- instancesParsed[[i]]
startTime <- strptime(runningInstances[[ri]][[11]], "%Y-%m-%dT%H:%M:%S")
if (startTime > latestStart) {
latestStart <- startTime
latestIndex <- ri
ri <- ri + 1
#return the DNS name of the running instance that was started last
dnsName <- runningInstances[[latestIndex]][[4]]
#fire up the SSH connection
system(paste("xterm -hold -e ssh -i ~/Documents/AWS/ec2ApiTools_rsa_id -D 9999 -o StrictHostKeyChecking=no root@", dnsName, sep=""), wait=F )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment