Pulling information from a who command on all servers [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 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.

Related

Get the days between two dates which are in Unix Timestamp [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 2 years ago.
Improve this question
Have a file with pull-requests details and has CreatedDate (ex: 1613698170) is in Unix timestamp format.
I want to notify to stake holders, when pull-request is open more than x days.
How to get the no of days between current date and pull-request created date in bash / groovy. So that I will execute this script in jenkins and send out notifications.
The following code:
def epochMillis = 1598098239000
def now = new Date()
def then = new Date(epochMillis)
def days = now - then
println "then: $then"
println "now: $now"
println "days between now and then: ${days}"
calculates the number of days between now and epoch millis. When executed, the above prints:
─➤ groovy solution.groovy
then: Sat Aug 22 14:10:39 CEST 2020
now: Fri Feb 19 19:01:54 CET 2021
days between now and file creation: 181

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

print params each in curl [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 8 years ago.
Improve this question
i need to increment the userb and usere but when i write my variable in curl , and when i try it i get nothing
(1..180000).step(20000)do |userb|
(20000..180000).step(20000)do |usere|
curl = %x[ curl -i -s -H "Host: xxxx" "http://XXXXX/scripts/exportStatsCsv/testA1?start='+ userb +'&end='+ usere +'&startDate='#{#array_timestampdate[0]}'&endDate='#{#array_timestampdate[1]}'" ] sleep(10)
end
end
Try running your code with warnings and debugging turned on:
ruby -cW2 path/to/your/code
You should see something like:
syntax error, unexpected tIDENTIFIER, expecting keyword_end
... sleep(10)
... ^
You need to do this as a first step when you run into a problem. Ruby will give you more detailed information about problems with the script when warnings are enabled and set to their highest value. Here's what the flags mean:
-c check syntax only
-W[level=2] set warning level; 0=silence, 1=medium, 2=verbose
You're getting this error, because sleep(10) needs to be executed as a separate statement. You can either insert ; between it, and the call to cURL, or put it on its one line. I'd recommend the second option in order to make the commands easier to read.
Also, I'd highly recommend using the Curb gem instead of launching cURL in a sub-shell like you are. You're losing flexibility and wasting CPU time having Ruby, then the OS, create a new shell to launch cURL.
Finally, you need to learn to write your code more clearly or you'll paint yourself into corners of confusion in no time. Here's a starting point for how I'd write the code:
require 'uri'
#array_timestampdate = ['start_date', 'end_date']
(1..180000).step(20000) do |userb|
(20000..180000).step(20000) do |usere|
uri = URI.parse('http://XXXXX/scripts/exportStatsCsv/testA1')
uri.query = URI.encode_www_form(
{
'start' => userb,
'end' => usere,
'startDate' => #array_timestampdate[0],
'endDate' => #array_timestampdate[1]
}
)
curl = %Q[ curl -i -s -H "Host: xxxx" "#{uri.to_s}" ]
puts curl
end
end
With a little example of the output:
>> curl -i -s -H "Host: xxxx" "http://XXXXX/scripts/exportStatsCsv/testA1?start=1&end=20000&startDate=start_date&endDate=end_date"
...
>> curl -i -s -H "Host: xxxx" "http://XXXXX/scripts/exportStatsCsv/testA1?start=160001&end=180000&startDate=start_date&endDate=end_date"

Name of the users which executed a command [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I want to get the name of the users which executed a command (for example cat).
fc -l will provide a list with the most recent commands executed by the current user but is there an way to find out the history for all users?
I read the manual but i could not find something that would help
Do you know any other commands which would do this job?
I also tried w and who
I found this solution: the super user will search in each dir from "home" in the .bash_history and make a grep on that file for that command. It will work but is this optimal?
Using awk =)
awk -v monitoredcmd=cat '
$1~"^#[0-9]{10,}\s*$"{
sub(/#/,"")
tmpdate=$1
}
$1==monitoredcmd{
"date -d #"tmpdate | getline date
close("date -d #"tmpdate)
print "command [" $0 "] by",
gensub(/\/home\/([^\/]+).*/, "\\1", "", FILENAME),
at,
date
}
' /home/*/.bash_history
Sample Output
command [cat file.txt] by sputnick mer. févr. 13 15:34:44 CET 2013
command [cat l.py] by sputnick mer. févr. 13 15:45:38 CET 2013
command [cat foobar.pl] by marc mer. févr. 13 15:47:54 CET 2013

How to say ${foo}123 in shell script? [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 8 years ago.
Improve this question
I'm building a shell script that is checks the existence of log files in a loop. The log files I'm trying to open are named like this: access_log-%02d00-%02d59 with %02d being an hour. In perl I could just say "access_log-${hour}00-${hour}59". But how do I do that in shell script? Here's my code. It doesn't work because it thinks the var name would be $HOUR00 and $HOUR59.
for HOUR in 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23; do
if [ -e tmp/access_log-$HOUR00-$HOUR59 ]; then
# do stuff here
fi
done
I figured it out myself. I works just the same as in perl. ${HOUR}00 does the trick.

Resources