2

My Problem is that my script dont send me the right variables to my Mattermost Channel. I will that my script send when a user login with ssh to my server , with name,ip,ect. And when the login failed send to another channel with information.

i have read many examples and tried. i can send a Text Message to the Channel when a user is login with ssh, but the Information is not included. I tried mattersend instead of curl.

#!/bin/bash

#Variable
USER="User:          $PAM_USER"
REMOTE="Remote host: $PAM_RHOST"
SERVICE="Service:    $PAM_SERVICE"
TTY="TTY:            $PAM_TTY"
DATE="Date:          `date`"
SERVER="Server:      `uname -a`"
LOGINMESSAGE="$PAM_SERVICE login on `hostname -s` for account    $PAM_USER"

if [ "$PAM_TYPE" = "open_session" ]
then
# dont function
curl -i -X POST -d 'payload={"text": "${PAM_RHOST}" ,    "username":"ssh", "channel":"monitoring"}' \  https://domain/hooks/xxx
# function
curl -i -X POST -d 'payload={"text": "Hello, world!", "username":"sshbot", "channel":"monitoring"}' \  https://domain/hooks/xxx
fi
exit 0

The script is started as optional in the pam.d config

I expected the variables to be transferred when logging in successfully.

Felix123
  • 21
  • 1

1 Answers1

0

'payload={"text": "${PAM_RHOST}" ...' is wrapped in single quotes. It doesn't allow expanding the variable ${PAM_RHOST}.

If you need it to be expanded, change

curl -i -X POST -d 'payload={"text": "${PAM_RHOST}" ,    "username":"ssh", "channel":"monitoring"}' \  https://domain/hooks/xxx

into,

curl -i -X POST -d 'payload={"text": '"${PAM_RHOST}"' ,    "username":"ssh", "channel":"monitoring"}' \  https://domain/hooks/xxx
Anubis
  • 6,995
  • 14
  • 56
  • 87
  • thanks anubis. I changed it to your example, but then he doesn't send anything to the channel. – Felix123 Apr 17 '19 at 11:26
  • Does it work as expected if you run the same command manually while putting the correct `$PAM_RHOST` value? If so, make sure `$PAM_RHOST` is properly set inside the script (simply try `echo $PAM_RHOST`) – Anubis Apr 17 '19 at 11:31
  • Hello Anubis, i echo it out to a file and yes in the file is the ip address but the curl dont send the info. – Felix123 Apr 17 '19 at 11:36
  • Did you check the first part? manually executing the same command? – Anubis Apr 17 '19 at 12:42