How to enumerate running ec2 instances and load them into a database using ruby? [closed] - ruby

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm using aws-sdk gem, I can't figure how to list all the running ec2 instances and load them to a database.
I need an approach on how to do it.

require 'aws-sdk-v1'
ec2 = AWS::EC2.new(
access_key_id: 'YOUR_ACCESS_KEY_ID',
secret_access_key: 'YOUR_SECRET_ACCESS_KEY',
region: 'YOUR_EC2_REGION'
)
ec2.instances
http://docs.amazonwebservices.com/AWSRubySDK/latest/frames.html

The answer above will return all instances, not just running instances. You can use a filter to get only running instances:
ec2 = AWS::EC2.new
ec2.instances.filter('instance-state-name', 'running')

Install the AWS ClI and run the following to get a list of running instance ids:
aws ec2 describe-instances --filter "Name=instance-state-name,Values=running" \
| grep InstanceId | awk '{print $2}' | sed 's/^\"//g' | sed 's/\",$//g'

Related

Obtain the hostname served by a Kubernetes Ingress [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 11 months ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
How to resolve the domain/hostname of a K8s Service, that a specific K8s Ingress is serving?
In the namespace foobar, I want to know where to connect for the service provided by the ingress.
kubectl --namespace foobar get ingress
returns the available ones, and
kubectl --namespace foobar describe ingress/bazbar
returns the details; I can match by name (e.g. barbaz) the one I'm targeting.
But how can I extrapolate the host (and, possibly, also the path) to then launch it in the browser with xdg-open?
The below should solve your query on getting domain per namespace.
The query below "get ingress" retrieves the domain details from all namespaces and using awk, it prints the 1st column which is the namespace and the 4th column which is the domain in the ingress, you can grep it further to filter down on particular namespace.
#To get all namespace and domain
kubectl get ingress --all-namespaces|awk '{print $1 " | " $4 }'
foobar | foobar.example.com
barfoo | barfoo.example.com
#To filter on namespace from all namespace
kubectl get ingress --all-namespaces|awk '{print $1 " | " $4 }'|grep -i foobar
foobar | foobar.example.com
#To get one namespace
kubectl get ingress -n <namespace-name>|awk '{print $1 " | " $4 }'
foobar | foobar.example.com

Get currency exchange rate using bash [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed last year.
Improve this question
I would like to display the current exchange rate from the currency euro to us dollar in the command line using bash shell script. I'm using the website market insider.
I saw on someone's blog to use wget command
wget -qO- https://markets.businessinsider.com/currencies/eur-usd
But how can I display only the rate? Desired output (example for current rate 1.1347) --> 1.1347 $
PS: I would prefer not to use API
Any help would be appreciated
Do this cleanly using their API:
#!/usr/bin/env bash
API='https://markets.businessinsider.com/ajax/'
ExchangeRate_GetConversionForCurrenciesNumbers() {
isoCodeForeign=$1
isoCodeLocal=$2
amount=$3
date=$4
cacheFile="/tmp/$date-$amount-$isoCodeForeign-$isoCodeLocal.json"
# Check if we have cached the result to avoid front-running the API
if ! [ -e "$cacheFile" ]; then
post_vars=(
isoCodeForeign="$isoCodeForeign"
isoCodeLocal="$isoCodeLocal"
amount="$amount"
date="$date"
)
method='ExchangeRate_GetConversionForCurrenciesNumbers'
IFS='&' url="$API$method?${post_vars[*]}"
curl -s -X POST "$url" > "$cacheFile"
fi
jq -r '.ConvertedAmountFourDigits' "$cacheFile"
}
getRateEURO_USToday() {
ExchangeRate_GetConversionForCurrenciesNumbers EUR USD 1 "$(date '+%Y-%m-%d')"
}
# Set LC_NUMERIC=C because the float format returned is using . as decimal
LC_NUMERIC=C printf 'The exchange rate for EUR to USD today is: %.4f\n' \
"$(getRateEURO_USToday)"

how to reset root password by ansible [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
we have the servers where root passwd needs to be reset by ansible.
I ran the command below to get the hash passwd and input our correct root passwd:
python3 -c 'import crypt,getpass;pw=getpass.getpass();print(crypt.crypt(pw) if (pw==getpass.getpass("Confirm: ")) else exit())'
Password:
Confirm:
$6$c/98MRPOs7JQ.pbw$XI7Qyz80d5ZV2DcgMk8limxB9DoTNsTCIZVYtLRTkM3a5T6NnHOgxEoRq/te4jIJhm114HuTXLv0dMf5H
then, I added that generated root password in my playbook:
tasks:
- name: Change user password
user: name=root update_password=always password=$6$c/98MRPOs7JQ.pbw$XI7Qyz80d5ZV2DcgMk8limxB9DoTNsTCIZVYtLRTkM3a5T6NnHOgxEoRq/te4jIJhm114HuTXLv0dMf5H
ran that playbook without errors, then tried to login with that actual (not hash encrypted) root password on the server, but it does not work, what I am doing wrong and how it can be fixed ?
Your hash is probably not correct. Maybe an incompatible hashing-algorithm was used.
There are multiple ways to generate that hash:
The ansible-way:
ansible all -i localhost, -m debug -a "msg={{ 'mypassword' | password_hash('sha512', 'mysecretsalt') }}"
The mkpasswd:
mkpasswd --method=sha-512
Or with python:
python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"
You need to install passlib first.

database login using shell scripting [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
This is the Database entry of my application in server.properties file.
umpdb.driverClassName=org.mariadb.jdbc.Driver
umpdb.url=jdbc:mysql://10.66.11.44:3306/MT_SMS_CHN?useUnicode=true&characterEncoding=UTF-8
umpdb.username=stackuser
umpdb.password=stackpass
I want to print mysql -uuser -ppasswrod -hhostname dbname using linux command.
It means, I need output as below
mysql -ustackuser -pstackpass -h10.66.11.44 MT_SMS_CHN
Please help me for this.
Using awk
awk -F'[:=/?]' '/url/{
h=$6" "$8 # get host and database
}
/username/{
u=$2 # get username
}
/password/{
# print username, password, host and database
printf("mysql -u%s -p%s -h%s\n",u,$2,h);
# we got what we want, exit
# if your file contains more than 1 db config
# just comment below exit keyword
exit
}
' server.conf
Test Results:
$ cat server.conf
umpdb.driverClassName=org.mariadb.jdbc.Driver
umpdb.url=jdbc:mysql://10.66.11.44:3306/MT_SMS_CHN?useUnicode=true&characterEncoding=UTF-8
umpdb.username=stackuser
umpdb.password=stackpass
$ awk -F'[:=/?]' '/url/{h=$6" "$8}/username/{u=$2}/password/{printf("mysql -u%s -p%s -h%s\n",u,$2,h); exit}' server.conf
mysql -ustackuser -pstackpass -h10.66.11.44 MT_SMS_CHN

Pulling information from a who command on all servers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm creating a program that pulls information from a who command, see also this question to see the format of the who command.
Now what I want to do is ssh to different servers and run the who command. Problem being I have no idea how to ssh in Ruby. I'm aware of require 'net/ssh/gateway' would somebody mind giving me an example of how I can ssh in Ruby and and perform a who command (like the linked question) on multiple servers?
For example:
def user
cmd = `who`.gsub(/[ \t].*/,"")
puts cmd
#<= Do some fancy stuff that will ssh to the servers and run cmd
end
Thank you ahead of time.
I've found out that doing something like this:
26 print "Enter password: "
27 system "stty -echo" #<= Removes echo from typing, you won't see your keystrokes
28 #password = gets.chomp
29 system "stty echo"
30
31 def logged_in
32 cmd = `who`.gsub(/[ \t].*/,"")
33 check = Net::SSH.start(#host, #username, :password => #password)
34 check.exec!(cmd)
35 end
36
37 #host = %w(servers).each do
38 logged_in
39 end
40
41 #username = Etc.getlogin
Will do what I want to accomplish.

Resources