Login to Perforce from Commandline

The insecure method is to put the plaintext password into your script:

echo my.formerly.secure.password|p4 login





http://stackoverflow.com/questions/28318673/login-to-perforce-from-commandline

i would like to login to Perforce(P4) from the command line. I tried using

p4 -u My_Username login

But how do I provide the password. I need this to checkin a file from the command line

shareimprove this question
 


			

				

2 Answers

    




  
up vote 0 down vote accepted

Before connecting to the server, set P4PORT (to tell the client where the server is) and P4USER (to tell the server who you are). P4PORT is specified as hostname:port, with the port usually (but not always) being 1666. If you're not sure what your Perforce server address and user name are, check with your sys admin.

p4 set P4PORT=your.server.hostname:1666
p4 set P4USER=your.username

You can also use "set" or "setenv" or "export" as appropriate to your shell, but with a 2014.2 or newer Perforce client (use "p4 -V" to check your version information) you can use "p4 set" as a persistent cross-platform alternative. Another option is to use the "-u" and "-p" flags on every command you run (e.g. "p4 -p your.server.hostname:1666 -u your.username sync ..."), but this gets tiresome quickly.

To verify that your connection is correct, run:

p4 info

If this gives you an error message, or says that your user is unknown, check with your Perforce administrator to make sure you have the correct P4PORT and P4USER values.

To login, run:

p4 login

If the client is able to connect to the server and your user name is correct, you will be prompted for the password. Enter it and you will be logged in.

If you are scripting Perforce commands that require login credentials, and you don't want your script to stop in the middle to prompt for a password, my recommendation would be to run "p4 login" as above at some point prior to running the script. The login ticket will persist on your machine and the script will pick it up (provided that it's connecting with the same P4PORT and P4USER that you used to generate the ticket). This is the most secure method because your cleartext password is not stored anywhere on your machine, and the login ticket is not usable from other machines by default (it is also probably time-limited, depending on how your admin has configured things).

The insecure method is to put the plaintext password into your script:

echo my.formerly.secure.password|p4 login

Piping the cleartext of the password into the "p4 login" command will cause it to behave as if you'd entered it at the prompt, and continue executing rather than waiting for additional input.

shareimprove this answer
 
    
Yea. As you said, I think as our server is ssl, the P4SSWD and -P are not working. p4 login is working but it asks me to enter a password, but as im running it from a batch file, is there a way in which when the prompt appears, I can enter the text? –  Ajay S Feb 4 at 18:26
    
In that case, yes, completely ignore everything about P4PASSWD and -P. :) I've removed it from my answer to eliminate any possible confusion. –  Sam Stafford Feb 4 at 18:27
    
Thank you for your reply. i spent 2 hours trying to get this work. I am now looking at other alternatives. –  Ajay S Feb 4 at 18:30
    
Updated my response to include some notes on scripting (you didn't mention this in your original question so I didn't think to include it). –  Sam Stafford Feb 4 at 18:33
    
Wow Thanks. Works brilliantly. Apologies for not being unable to Upvote as I am a new user –  Ajay S Feb 4 at 18:44
  

I believe you can do this:

p4 -P My_Password login
shareimprove this answer
 
    
Nope. this doesnt work –  Ajay S Feb 4 at 10:46
    
Can you give a clue about how it doesn't work please? –  Mark Smith Feb 4 at 10:53
    
Throws me the following error:Perforce client error: Connect to server failed; check $P4PORT. TCP connect to Welcome@123 failed. The specified class was not found. –  Ajay S Feb 4 at 11:06
    
Have you tried doing what it says in the error message - checking P4PORT? It looks like it can't connect to the server - nothing to do with passwords. –  Mark Smith Feb 4 at 11:10
    
To specify the port, use lower-case '-p'. So a full command would be: 'p4 -p my_port -u my_user login'. To specify the password, you can pass the password on stdin, or via the -P (upper-case '-P') flag. –  Bryan Pendleton Feb 4 at 14:37
posted @ 2015-05-12 15:11  alxe_yu  阅读(493)  评论(0)    收藏  举报