Ldapsearch filtering using variables not displaying data - linux

I am currently trying to query an LDAP server to find whether the email passed to the script exists on our system.
Below is the ldapsearch command I am trying to use:
ldapdata=`ldapsearch -h ### -b "ou=###,o=###" "email=$email" email firstname surname`
echo "ldapdata: $ldapdata"
This works perfectly when the filter includes a predetermined email, ie "mail=firstname-surname####" however when passed a variable, such as $email, the output is not able to be manipulated by further grep / awk statements and will not display any data in the echo statement.
From some Googling I have figured out It could be to do with the line wrapping which LDAP uses.
What I have already tried to solve the issue:
| perl -p00e 's/\r?\n //g
| sed '/^$/d
-o ldif-wrap=no
My question is, what is the best method to solve this issue. Many thanks in advance.

Just for anyone having the same issue, the issue was actually due to me writing and testing the program in a Windows environment.
I was pulling the $email variable from a file that is in the dos format.
To fix this all I did was :
dos2unix $FILELOCATION

Related

Is there a simple alternative to "who am i" and "logname"?

I noticed that RHEL 8 and Fedora 30 don't update the utmp file properly.
As a result, commands such as 'who am i', 'last', 'w' etc print incorrect results (who am i actually doesn't print anything)
After a bit of googling, I found 'logname' which worked in this case but I read that gnome is dropping support for utmp altogether so it's a matter of time until this stops working too.
I wrote the following script which finds the login name of the user (even if he is using sudo the moment he runs the command) but it's way too complicated so I'm looking for alternatives.
LOGIN_UID=$(cat /proc/self/loginuid)
LOGIN_NAME=$(awk -v val=LOGIN_UID -F ":" '$3==val{print $1}' /etc/passwd)
Is there a simple alternative which is not based on proper updating of /var/run/utmp ?
Edit 1: Solutions that don't work $HOME, $USER and id return incorrect values when used in a script that has been run with the sudo command. who am i and logname depend on utmp which isn't always updated by the terminal.
Working solution: After a bit of searching, a simpler way than the aforementioned was found in https://unix.stackexchange.com/users/5685/frederik-deweerdt 's comment to his own answer
Link to answer which contains the commment: https://unix.stackexchange.com/a/74312
Answer 1
stat -c "%U" $(tty)
Second answer found at https://stackoverflow.com/a/51765389/10630167
Answer 2
`pstree -lu -s $$ | grep --max-count=1 -o '([^)]*)' | head -n 1 | sed 's/[()]//g'`
Your question is not well-defined because if X and Y are not working, what are the chances that Z will work? This depends entirely on the precise failure mode you are attempting to handle, and there is nothing in your question to reveal the specific circumstances in which you need this.
With that out of the way, perhaps look at the POSIX id command, which has explicit options to print the real (login) or effective (after any setuid command) user id with -r or -u, respectively. Of course, the precise means by which it obtains this information are not specified, and will remain implementation-dependent, and thus might or might not work on your platform under your specific circumstances.
As an aside, here is a refactoring of your code to avoid polluting the variable name space with two separate variables.
LOGIN_NAME=$(awk 'NR==FNR { val=$0; next }
$3==val{print $1}' /proc/self/loginuid FS=":" /etc/passwd)

Change the From field in an email with the mail command under linux without the -a option

Problem: when I use the mail command under linux (Ubuntu Server 16.04) as root to send an email (several scripts on my server do so), the From: field in the mail header looks like From: root#mydomain.org. I want it to look like From: admin#mydomain.org.
Attempt: I already found the option -a "From: admin#mydomain.org" to add the field to the mail header.
My whole command looks like this:
echo "content" | mail -s "subject" "recipient#wherever.org" -a "From: admin#mydmain.org"
Second Problem: However, I do not want to write the -a option at every point I use the mail command in a script because this is some kind of hard coding.
Second Attempt: My best attempt yet is to write a wrapper, though I think there should be a cleaner method to always add that header field to mails sent with the mail command.
Question: Does anyone know a better way which does not include hard coding? Still I want to use such a simple command line as above to not make things unnecessary complicated.
Best
Fabian
the mail command reads ~/.mailrc or a different startup file given by the environment variable MAILRC. (see manpage, http://manpages.ubuntu.com/manpages/xenial/man1/bsd-mailx.1.html#contenttoc4)
this file can contain the line set from=you#example.org.
conf="$(mktemp)"
trap 'rm -f "$conf"' EXIT
echo set from=you#example.org > "$conf"
export MAILRC="$conf"
# now mail will use you#example.org as From:

check date_filename.txt exist

I've created a set of linux command that do set of ping and keep the log file into 2 file, which is date-time_successping and date-time_failping. I want to do an action where when date-time_failping exist, the linux system will send an email to the PIC. but the filename is too random since i've a date infront of it. E.g. 20170911-160455_failping. I tried using like -c/-f, but they cannot search if no specific name. there will be multiples of file with variant of dates created. So I need some advice on this. hope anyone could help.
thanks
P/S: I'm so sorry if the information that given is not enough, please reply if needed more info so that i could try to provide and help me solve this issue.
Regards
You can do it something like this optionally,
This command is to get yesterday date:
VAR1=`date +%Y%m%d -d "yesterday"`
Or you want to work with today's date
VAR1=`date +%Y%m%d `
$file = VAR1`__failping`
if [ -f "$file" ]
then
echo "My message" | mail -s subject user#gmail.com
else
//Do something else
end
You can use inotify to monitor your log file creation event, then check if the file pattern match xxxxx_failping. Assume your log files are put in ping_logs, you can use the following script to monitor
$ inotifywait -rme create ping_logs/ |awk '{if($NF ~ /[0-9]+-[0-9]+_failping/) print $NF}'
Here just print the file name, you can change to your mailing action.

Base64 encoding from a website and terminal give two different results

I used following command on terminal
`echo admin:admin | base64`
It gives me following output
YWRtaW46YWRtaW4K
But when I used https://www.base64encode.org/ for the same string admin:admin it gives me following
YWRtaW46YWRtaW4=
Any reason for this?
The reason this behaviour is the new line added by the echo command. Normally the echo command add a new line at the end which leads to a different encoding. Therefore if you use it with echo -n admin:admin | base64 the difference won't occur.

egrep command with piped variable in ssh throwing No Such File or Directory error

Ok, here I'm again, struggling with ssh. I'm trying to retrieve some data from remote log file based on tokens. I'm trying to pass multiple tokens in egrep command via ssh:
IFS=$'\n'
commentsArray=($(ssh $sourceUser#$sourceHost "$(egrep "$v" /$INSTALL_DIR/$PROP_BUNDLE.log)"))
echo ${commentsArray[0]}
echo ${commentsArray[1]}
commax=${#commentsArray[#]}
echo $commax
where $v is something like below but it's length is dynamic. Meaning it can have many file names seperated by pipe.
UserComments/propagateBundle-2013-10-22--07:05:37.jar|UserComments/propagateBundle-2013-10-22--07:03:57.jar
The output which I get is:
oracle#172.18.12.42's password:
bash: UserComments/propagateBundle-2013-10-22--07:03:57.jar/New: No such file or directory
bash: line 1: UserComments/propagateBundle-2013-10-22--07:05:37.jar/nouserinput: No such file or directory
0
Thing worth noting is that my log file data has spaces in it. So, in the code piece I've given, the actual comments which I want to extract start after the jar file name like : UserComments/propagateBundle-2013-10-22--07:03:57.jar/
The actual comments are 'New Life Starts here' but the logs show that we are actually getting it till 'New' and then it breaks at space. I tried giving IFS but of no use. Probably I need to give it on remote but I don't know how should I do that.
Any help?
Your command is trying to run the egrep "$v" /$INSTALL_DIR/$PROP_BUNDLE.log on the local machine, and pass the result of that as the command to run via SSH.
I suspect that you meant for that command to be run on the remote machine. Remove the inner $() to get that to happen (and fix the quoting):
commentsArray=($(ssh $sourceUser#$sourceHost "egrep '$v' '/$INSTALL_DIR/$PROP_BUNDLE.log'"))
You should use fgrep to avoid regex special interpretation from your input:
commentsArray=($(ssh $sourceUser#$sourceHost "$(fgrep "$v" /$INSTALL_DIR/$PROP_BUNDLE.log)"))

Resources