How to use p4tickets with p4python - perforce

There's a large number of examples of using your user/pass as credentials to connect to the p4 server using p4python, but very little describing how to use a p4ticket. It's unclear how to use a p4tickets file with p4python.

It turns out the perforce documentation is really, really precise. The documentation basically says that if the user isn't logged in, then you have to provide a password to login. The flip side of that coin is that if the user is logged in then no password is needed. So, for instance, assume the user is logged in:
>>> p4 -u 'username' login
Then, in your p4python script, the following will connect:
p4con = P4.P4()
p4con.user = 'username'
p4.con.connect()
The p4python connection will naturally use the ~/.p4tickets file.

To use a ticket in a P4 Python script (or the P4 command line) you essentially just put the ticket anywhere that asks for a password. With that, there's an issue of precedence of where P4 Python takes the ticket from that is not consistent with how the P4 command line works.
In P4 Python:
If the environment variable P4PASSWD is set it is used.
If there is an entry for the user in whatever file p4.ticket_file looks at (from P4TICKETS, the default of ~/.p4tickets, or a value that was placed there by the script) that will be used if P4PASSWD is not set
If the p4.password field is set it will be used if neither of the above conditions have been met.
Running P4 from the command line:
If run with -P the ticket from the command line is used
If P4PASSWD is set that is used if the above condition has not been met
If there is an entry for the user in whatever file p4.ticket_file looks at (from P4TICKETS, the default of ~/.p4tickets, or a value that was placed there by the script) that will be used if neither of the above conditions have been met.
Example 1 - This will override the value set in p4.password on line 4 with the value for the user set on line 3 if there is an entry in the ticket file. If there's no entry in the ticket file it will use the supplied credentials AND update the ticket file so that the second time the script is run it will use that and not the supplied credentials.
from P4 import P4
p4 = P4()
p4.user = user
p4.password = ticket
Example 2 - This will always use the supplied credentials because we're not actually using a real file.
from P4 import P4
p4 = P4()
p4.user = user
p4.password = ticket
p4.ticket_file = '/dev/null'
tl;dr - Use the ticket anywhere Perforce asks for a password. If you want to use a ticket in your script, set ticket_file='/dev/null' first to ensure the expected behaviour.

run_login(): Runs p4 login using a password or ticket set by the user.
from P4 import P4
p4 = P4()
p4.port = "xxx.xxx.xxx.xxx:1666"
p4.user = "xxxxx"
p4.connect()
p4.run_login("-s", p4.user)

Related

Execute shell commands with excel VBA and collect output [duplicate]

This question already has answers here:
Capture output value from a shell command in VBA?
(8 answers)
Closed last year.
I am trying to make a ticket tool we have where I work a little better and make it so the tech team doesn't have to open Active Directory to make sure the ticket sender is eligible to make certain requests. What I need is the ability to run net user /domain <username> then collect the comment line and store it in a variable in VBA. The variable will then be sent in an email to the ticket software along with anything the user entered in the text boxes.
My thoughts were to run the command and send the output string to another variable then pull the line information from the larger output since I cant seem to find a way to get only the comment.
I've added a sample of the output below with an arrow pointing at the data that needs extracted into the variable.
I'm still pretty new to VBA so bear with me.
C:\Users\jdoe\Desktop>net user /domain jdoe
The request will be processed at a domain controller for domain StackOverflow.wh.
User name jdoe
Full Name John Doe
Comment Anlst Tech Supp <----- this is what needs extracted
User's comment
Country/region code 000 (System Default)
Account active Yes
Account expires Never
Password last set 11/1/2021 8:58:44 AM
Password expires 1/30/2022 8:58:44 AM
Password changeable 11/1/2021 8:58:44 AM
Password required Yes
User may change password Yes
Workstations allowed All
Logon script
User profile
Home directory
Last logon 1/15/2022 11:43:12 AM
Logon hours allowed All
Local Group Memberships
Global Group memberships *Domain Users
The command completed successfully.
On Error Resume Next
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set config = objWMIService.ExecQuery("Select * From Win32_UserAccount")
For Each thing in Config
Msgbox thing.caption & " " & thing.Description
Next
Programmers don't call user's commands. They are for humans not programs.
See https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-useraccount for more info.

What is the correct method to determine if a system user exists locally on windows?

I am working on an authentication system for a local server jupyterhub that relies on OAuth protocol. Additionally, it creates a local system user on windows, in case it does not exist.
What is the correct way to check whether a user exists on windows platforms using python?
This would include cases in which the system uses LDAP authentication and the user logged in the machine at least once.
I am looking for the correct windows alternative to the unix-like:
import pwd
try:
pwd.getpwnam(user.name)
except Exception as e:
print(repr(e))
My current solution is to check for the existence of the f"os.environ["SystemDrive"]\Users\{username}" folder. Side question, is there any drawback with the current method?
Here's a solution to checking if a local Windows user exists using python:
import subprocess
def local_user_exists_windows(username):
r = subprocess.run("net user",stdout=subprocess.PIPE)
#look for username in the output. Return carriage followed by line break followed by name, then space
return f"\\r\\n{username.lower()} " in str(r.stdout).lower()
Alternative is to use a regular expression to find username match (^ is regex for beginning of line if used in conjunction with multiline, \b for word boundary):
import re
re.findall(rf"^{username}\b", out,flags=re.MULTILINE | re.IGNORECASE)
Note that the \b could be replaced with \s+ meaning a space character one or more times and yield similar results. The function above will return True if given user name is an exact match with local username on Windows.
Again, my reason for this solution is there might be drawback to checking whether the path f"os.environ["SystemDrive"]\Users\{username}" exists. For example, I have a case where a Local User (e.g,local_username) exists via the net user command or via looking at "Local Users and Groups" control panel, but there is no C:\Users\local_user_name folder. One reason for this I can think of off the top of my head is perhaps the user switched from logging in as a Local User to using a Domain Account, and their User folder was deleted to save space, so the User exists, but the folder does not, etc.)
The call to net user gets local users - and the output looks something like this:
User accounts for \\SOME-WINDOWS-COMPUTER
-------------------------------------------------------------------------------
SomeUser Administrator DefaultAccount
Guest local_admin WDAGUtilityAccount
Notice how the SomeUser in this example is preceded by a \r\n followed by multiple spaces, hence looking for a username string inside this string could yield a false positive if the string you are searching is contained inside another string.
The solution above works for me, but has been tested all of ten minutes, and there might be some other simpler or more pythonic way of doing this.

Check the root password

What is the best way to check if a root linux password is correct,from a c program.One solution is tu run a command like : echo \"myPass\n"\ | sudo -S mySudoPassword and somehow check stderr to see if contains data.I am looking forward to get an elegant solution
You can validate that a given password is correct for a given username using the shadow file.
See Given a linux username and a password how can I test if it is a valid account? for the mechanics of how this is done. It should be possible to perform the same operations in a C program.

Need to capture the commands fired on Linux

I would like to capture all the commands fired by a user in a session. This is needed for the purpose of auditing.
I used some thing like below,
LoggedIn=`date +"%B-%d-%Y-%M:%H"`
HostName=`hostname`
UNIX_USER=`who am i | cut -d " " -f 1`
echo " Please enter a Change Request Number for which you are looging in : "
read CR_NUMBER
FileName=$HostName-$LoggedIn-$CR_NUMBER-$UNIX_USER
script $FileName
I have put this snippet in .profile file, so that as soon as the user logs in to a SU account this creates the file. The plan is to push this file to a central repository where an auditor can look into those files.
But there are couple of problems in this.
The "script" command spools all the data from the session, for example say, a user cats a property file, It appends all the data of the property file to the auditing file.
Unless user fires the 'exit' command, the data will not be spooled to auditing file, by any chance if user logs out with out firing exit command, the auditing file will be empty.
Is there any better solution for auditing ? History file is not an option since it does not tell me for which Change Request number ( internal to my organisation) the commands are fired. Is there any other way just capture only the commands fired but not the output ?
Some of the previous discussion are here and here
I think this software exactly matches your need:
https://github.com/a2o/snoopy

How to get command value from file or variable in Linux

I need to take the command value from file and execute the command,
in my scenario I am rinning this commands ON Terminal
uci set wireless.#wifi-iface[0].encryption=psk
uci set wireless.#wifi-iface[0].key="your_password"
uci commit wireless
wifi
but i need to pass the value of key i.e "your_password" dynamically i.e from file or from variable where I can store the value taken from python code.
so please tell me how can I pass this value dynamically and execute this commands successfully.
Thanks in Advance!!
Just use shell variable expansion, like this:
password='MYPASSWORD'
uci set wireless.#wifi-iface[0].key="$password"
The important thing here is the dollar sign in $password: which signals the shell that what you want is not the string password itself, but the value the variable password (defined before) points to.
If you want to read password's value from a file instead of defining it in inline, two approaches are available.
First approach
Create a configuration file (e.g. myscript.conf) and source it. E.g., myscript.conf will contain
password='MYPASSWORD`
and myscript will contain
source myscript.conf
uci set wireless.#wifi-iface[0].encryption=psk
uci set wireless.#wifi-iface[0].key="$password"
uci commit wireless
wifi
Be aware that this approach might have security flaws (everything you write into myscript.conf gets actually executed in the shell).
Second approach
Create a password file and just read its content. E.g., the password file will look like this
MYPASSWORD
I.e., it will contain just the password. On the other hand, myscript will be
password=$(cat password_file)
uci set wireless.#wifi-iface[0].encryption=psk
uci set wireless.#wifi-iface[0].key="$password"
uci commit wireless
wifi
Here we read the content of password_file by using cat and storing it into the variable password.

Resources