user account creation date in LDAP - bash

I would like to write a shell script to do the following , would advise how to make it ? very thanks
Connect LDAP server ( Id : user , password : pass )
check the field user_account_create_date in the LDAP server, the format of data in this field is ABC20130922 (September 22, 2013)
find the record in this field to check last 8 digits ,
Pseudocode:
if the date is within 7 days: # account is created within 7 days
then do
...
else do
...
I have a script as below which connect ldap, and check if the user account is created within 7 days ( the script is not complete yet)
timestamp = date --date="-7 days" +%Y%m%d%k%m%SZ
ldapsearch -h 192.168.1.100 -p 389 -D cn=admin,o=services -w pass -x "(&(objectclass=*)(createTimestamp>=$timestamp))"
then
What I hope to do now is to modify the above script so that createTimestamp get the creation date string which can be used to compare with timestamp.

Something close to this should work:
ndays=7
timestamp=`date --date="-$ndays days" +'%Y%m%d'"100000Z"`
ldapsearch -x -LLL -h yourhost.yourdoamin.com -p 389 -b "ou=people,dc=yourdomain,dc=com" -D cn=admin,....,dc=yourdomain,dc=com -w yourpassword "(&(objectclass=inetorgperson)(modifytimestamp>=$timestamp))" modifytimestamp
I used modifyTimeStamp for testing as no one had been created lately on my home system.
-jim

Ensure that the timestamp has the correct syntax (generalized time). When using a value in an assertion (filter), the value must be the syntax of the attribute. Also, the objectClass=* component of the filter is not needed: all entries have at least one objectClass.

Related

Calculating a date of expiration with shell commands

I am writing a code which would check my proxy expiration date. I manage to calculate the expiration date only by adding hours to present time, but would now also like to include minutes and seconds. I run arcproxy --info and the output that I am interested in (among other lines) says e.g. Time left for proxy: 45 hours 55 minutes 10 seconds. I am now wondering what would be the shortest way to extract all three (h, m, s) parameters and add them to date?
What I currently have is
PROXY_VALIDITY=$(arcproxy --info | grep -oE "proxy: [0-9]+" | grep -oE "[0-9]+")
which extracts the hours and then I simply add it to date by date -d "+$PROXY_VALIDITY hours".
Of course I could use multiple grep commands to define also minutes and seconds similarly but I am interesting in the easiest and cleanest code..
EDIT: I found that arcproxy has a flag -i validityEnd which gives the validity end timestamp so this is now much easier to implement, but I am still looking for answers on my question above to learn something new.
EDIT: Output of arcproxy --info command:
Subject: /C=<C>/O=<O>/O=<O>/OU=<OU>/CN=<name>/CN=<CN>
Issuer: /C=<C>/O=<O>/O=<O>/OU=<OU>/CN=<name>
Identity: /C=<C>/O=<O>/O=<O>/OU=<OU>/CN=<name>
Time left for proxy: 44 hours 24 minutes 46 seconds
Proxy path: /tmp/x509up_u3431
Proxy type: X.509 Proxy Certificate Profile RFC compliant impersonation proxy - RFC inheritAll proxy
Proxy key length: 2048
Proxy signature: sha512
====== AC extension information for VO ======
VO : vo
subject : /C=<C>/O=<O>/O=<O>/OU=<OU>/CN=<name>
issuer : /DC=<C>/DC=<DC>/OU=<OU>/CN=<name>
uri : <url>:15001
attribute : <attribute>
attribute : <attribute>
tag : <tag>:nickname=gasar8
Time left for AC: 44 hours 25 minutes 1 second
where things in < > are my personal data.
Try this:
date -d "now +$(arcproxy --info|cut -f2 -d:)"
Also as per your question edit:
date -d#$(arcproxy -i validityEnd) # Change flag with appropriate argument
Piping the output into awk is an option:
arcproxy --info | awk -F: '/Time left for proxy/ { system("date -d +\""$2"\"") }'
Find the right output line, set the field delimiter to : and then utilise the hours minutes and seconds output to build a date command to execute with awk's system function

ldapsearch - how to display "lastLogonTimestamp"

I want to search Active Directory for inactive users that have no login for x days/months. I've got such a ldapsearch query:
ldapsearch -h domain.test -p 389 -D "cn=login,ou=test,dc=domain,dc=test" -w "passwd" -s sub -b "ou=Test,dc=domain,dc=test" "(&(objectCategory=person)(objectClass=user)(userAccountControl:1.2.840.113556.1.4.803:=2))"
It gives me the list of all inactive users in domain.test with all attributes.
I would like to add a filter for searching users that have no login for x days/months, and it would be great if the result was the list of sAMAccountNames (inactive user and lastLogonTimestamp >e.g. 3months).
I'm aware that LastLogonTimestamp is not the real time of last user logon, but in this case it's not so important.
EDIT: now I only need to know if there is a way to show attribute like "lastLogonTimestamp" in the output of above ldapsearch query?
ANSWER: Attribute lastLogonTimestamp was not set for each object in the output of above ldapsearch query. I haven't noticed that. So grep displayed it:
ldapsearch -h domain.test -p 389 -D "cn=login,ou=test,dc=domain,dc=test" -w "passwd" -s sub -b "ou=Test,dc=domain,dc=test" "(&(objectCategory=person)(objectClass=user)(userAccountCont‌​rol:1.2.840.113556.1‌​.4.803:=2))" | grep -i lastlogontimestamp
EDIT:
I though that it will be ok to compare lastlogontimestamps - it isn't because lastlogontimestamp values are not comparable. The only way is to convert to date format first, and then compare to get users that lastlogon was eg. before 01/06/2017. And here's the question: how to convert windows lastlogontimestamp to date in bash?
Please let me know if it's the correct way to do it.
Any advice very appreciated.

Consuming function module with SAP Netweaver RFC SDK in Bash

I'm trying to make a request to a function in a SAP RFC server hosted at 10.123.231.123 with user myuser, password mypass, sysnr 00, client 076, language E. The name of the function is My_Function_Nm with params: string Alternative, string Date, string Name.
I use the command line:
/usr/sap/nwrfcsdk/bin/startrfc -h 10.123.231.123 -s 00 -u myuser -p mypass -c 076 -l en -F My_Function_Nm
But it always shows me the help instructions.
I guess I'm not specifying the -E pathname=edifile, and it's because i don't know how to create a EDI File to include the parameters values to the specified function. Maybe someone can help me on how to create this file and how to correctly invoke startrfc to consume from this function?
Thanks in advance.
If you actually check the help text the problem shows, you should find the following passages:
RFC connection options:
[...]
-2 SNA mode on.
You must set this if you want to connect to R/2.
[...]
-3 R/3 mode on.
You must set this if you want to connect to R/3.
Apparently you forgot to specify -3...
You should use sapnwrfc.ini which will store your connection parameters, and it should be places in the same directory as client program.
Sample file for your app should be following:
DEST=TST1
ASHOST=10.123.231.123
USER=myuser
PASSWD=mypass
SYSNR=076
RFC_TRACE=0
Documentation on using this file is here.
For calling the function you must create Bash-script, but better to use Python script.

cURL call works with number but not with variable containing number

I've ran into a strange issue. I'm trying to script my router to collect usage stats and other stuff. I'm making one cURL to the auth URL to get a valid session id, then another using that session id to the page I need.
Here is my script:
SESSION_ID=$(curl --silent -D - -X POST http://10.0.0.1/login.cgi -d'admin_username=admin&admin_password=admin' | grep 'SESSION' | sed 's/Set-Cookie: SESSION=//' | sed 's/; path=\///')
echo $SESSION_ID # 1234567890
curl -v -H "Cookie: SESSION=$SESSION_ID" http://10.0.0.1/modemstatus_dslstatus.html
If I manually take SESSION_ID and insert it in place of '"$SESSION_ID"' everything is dandy. cURL shows the headers (via -v) and they are correct. Running the command while manually inserting the session id produces identical headers.
I'm sure it's something small. Please teach me something :)
Check for carriage returns \r in your variables which wouldn't appear with a simple echo in some cases.

mongoexport recently hours datas from mongodb

I am a new beginner of mongodb. I want to export some data in recent hours from my databases. So, I think I need to write mongoexport command and include date range in --query options to do it.
I write a bash file like this and try to run it:
#!/bin/bash
mongoexport --host localhost:27017 --db copy --collection txt --csv --fields x1,x2,x3...,date --query '{ "date" : {$gt:new Date(new Date() - 1000*60*60*3)} }' --out home/data.csv
But I get the results says:
connected to: localhost:27017
assertion: 16619 code FailedToParse: FailedToParse: Expecting '}' or ',': offset:25 of:{ "date" : {$gt:new Date(new Date() - 1000*60*60*3)} }
It sees connect to localhost but cannot output the data. If I remove --query option, this can run successfully and get the whole data, but I must need the query to subset the data in recently 3 hours.
Any ideas and help will be highly appreciated. Thank you and Best.
with mongoexport you have to provide the Date object a timestamp.
An explaination is answered here: MongoDb timestamp
What you can write as script is something like this (I'm rather rusty with bash, surely can be improved to stay on a single line):
timestamp=$(date +%s)
let total=$timestamp*1000-3600*1000*3
mongoexport --host localhost:27017 --db copy --collection txt --csv --fields x1,x2,x3...,date --query '{ "date" : {$gt:new Date('$total')} }' --out home/data.csv

Resources