how to check if already logged in perforce from command line - perforce

I want command to check if I am already logged in perforce, which returns 0 if I am logged in, otherwise some non-zero value to represent error. how to achieve this from command line?

p4 help login says:
p4 login [-a -p] [-h <host>] [user]
p4 login [-s]
[...]
The -s flag displays the status of the current ticket (if there is one).
Additionally, empirical testing shows that running p4 login -s while logged in returns an exit code of 0, and running it while not logged in returns an exit code of 1.

Related

Testing accessibility through SSH

I'm trying to write a PowerShell script to test user permissions through SSH, the idea is to connect to a distant machine through ssh, then do su to another user and test if we have the permission to do so. I managed to connect to ssh, and send a su command, but the problem is that when I su, I no longer in control with session, so I get stuck into the su user, my question is there a way to solve this issue ?
Regards
Well, you could use the su command with the -c option, which will run a single command and then return (pick a simple command, like hostname). Thereafter you could check the $? variable. If 0, it worked. If non-zero, it failed.
However, if su prompts for a password, then your script is going to get stuck.
sudo, on the other hand, has both a "command" parameter and a "non interactive" parameter. Would sudo work for you?
# -n = Non Interactive
# -u = Username
# -s = Command to run
sudo -n -u root -s hostname
if [ $? == 0 ]; then echo "It worked"; else echo "No good"; fi

'su' by using 'script' in Docker returns different results compared to the standard environment

I need to request certain commands via su including password in one line.
I found a solution and it is working in a standard environment (Ubuntu) (more about solution here):
{ sleep 1; echo password; } | script -qc 'su -l user -c id' /dev/null | tail -n +2
But I am faced with the problem that this solution is not suitable in a Docker container environment
Script terminates the command without waiting for echo and as a result i get:
su: Authentication failure
Any help is much appreciated.
Passing the password for su via stdin is problematic for various reasons: the biggest one is probably that your password will end up in the history.
You could instead:
Call the entire script as the specific user and thus enter the password manually
Use sudo with the appropriate NOPASSWD sudoers configuration
In your case you are using docker, so you could just set the USER in your Dockerfile

Check if logged in user via SSH did go through Active Direcory

I'm trying to check if a user who connects to a linux server via SSH is authentificated using Active Directory (Centrify).
In case he uses a local account (located in /etc/passwd), I need to display a warning asking him to use his Active Directory account and then prompt login again.
My first attempt was using PAM module pam_script inside /etc/pam.d/login to execute a script that checks if the current username exists in /etc/passwd whenever a user log in, display a warning if found and call the login command again.
I added the following line to /etc/pam.d/login
session required pam_script.so runas=root
This line execute a script file located in /etc/security/onsessionopen which contain:
#!/bin/sh
username=$1
if [ $(grep -c '^'$username':' /etc/passwd) = 1 ]
then
echo "Warning, please user your AD credentials"
login
fi
But the same scenario didn't work in /etc/pam.d/sshd.
When using SSH the script does run but doesn't display text or prompt for login.
Any thoughts ?
Thank you

EXISTS=`ssh xyz 'egrep "$username" /etc/passwd | cut -d':' -f1'` command is not working properly

I am writing a script to add users on multiple linux servers. But first i want to check that mentioned username already exists on that particular server or not.
EXISTS=`ssh xyz 'egrep "$username" /etc/passwd | cut -d':' -f1'`
This is the command i am trying. But its not giving me the output that i want..rather its giving me the all username in passwd file.
The variable EXISTS should have the only that username or it should be empty.
On any modern Linux systems, you have getent. So instead of checking like this, check the return code (via $?) of:
getent passwd $username &>/dev/null
(exits with 0 if the user exists, 2 if it doesn't exist; other error codes described in the manpage)
(note: this is not really a Java question, is it?)
EDIT OK, full code...
ssh thehost getent passwd $username;
RC=$?;
# check for $RC here; if 0, the user exists; if not 0 it doesn't.
# Man getent for more details
Note that another solution would be to try and add the user directly; the command will fail if the user already exists, or some other reason; here again, man useradd, and check for possible return codes.

How do I run a command on login and have it appear in the console?

For example have it run tmux attach -d right when I login. .bashrc, .profile and the like seem to run it in the background? I want to see the action run in the terminal right after I login. I tried adding command="echo 1" entry to authorized_keys in .ssh that runs the command and logs me out which was not what I wanted.
To run something run right away as part of .bashrc when starting a login shell, put these as the last lines:
if shopt -q login_shell; then
exec tmux attach -d
# run exit if you want to be logged out if the exec fails, otherwise omit
exit
fi
However, this will mean you won't be dumped into an actual bash shell, and when tmux exits, you'll be logged out.

Resources