GitPython Via HTTPS - python

I am currently trying to push to Git via GitPython using HTTPS. I am able to do this successfully, but I am prompted to enter my username and password for every fetch, push, and pull operation.
I would like to have my username and password be entered as command line arguments as my script makes a couple of git interactions. This repo will be on probably 40+ servers as a method of config management, and I don't want to set up all the SSH keys especially since it will often be used from a user on the host that is not myself.
Does anyone know how to take context of the shell from the python script to programmatically enter my username and password (they are already in my code as variables from the command line) or a way to pass my username and password to GitPython so that the shell prompt never even happens?
git_repo = Repo.init(repo_dir)
origin = git_repo.remote('origin')
origin.fetch() # Shell prompted here for username and password
git_repo.heads.dev.checkout()
origin.pull() # Shell prompted here for username and password
git_repo.git.add([added_filename])
git_repo.git.commit(commit_message)
origin.push() # Shell prompted here for username and password

Related

prompt user to login to protected directory in python

I have a script that searches through a protected directory and opens file explorer to a selected location. The problem is that it only works after the user has already logged into the directory.
Example of what sort of thing the code is doing below:
subfolder = input("give a subfolder")
if os.path.isdir("\\\\directory\\path\\"):
#run some stuff
os.startfile("\\\\directory\\path\\" + subfolder)
else:
print('error message- unable to connect to drive. Please log in')
If the user has opened "\\directory\path" on their own and entered their username/password into the windows security prompt, then my code works. If they haven't, I can't find the directory path.
Is there a way to open to the windows security prompt from Python? Ideally user puts in username and password and then can continue to the directory.
If you install win32, you can connect to network resources with a prompted username and password. It's not a simple solution (like an os call), but you can use the wnet_connect method from this example to pass a username/password in your except call.
It boils down to:
win32wnet.WNetAddConnection2(0, None, full_path, None, username, password)

create and read a .txt File on heroku with python app

i created Line-bot app using heroku as host, which can create, read, and append into a txt_files on heroku by sending a command from Line account,
so i tried to sent message to my Line-bot with command like this:
username = "user_name"
password = "pass_word"
i sent that text to my Line-bot and it saves my username and password into a txt_file(of course it check if there was no txt_file for save those data it will create txt_file automatically) on heroku, and by another command which can read the text which my Line-bot saved last time, my Line-bot sent me message and it shows:
username is user_name
password is pass_word
however after a few hours when i need my username and password/read back my data, by sending the same command, the data is gone and becomes like this
username is //empty
password is //empty
what should i do for this problem?
thank you...
Are you using a free-level heroku account? If so, heroku does not allow permanent (long-term) file storage. As long as the dyno is shut down, any changes will be reverted.
An alternative is to use an external server as a database, but those may suffer from slower connection.
This article tells you more about heroku.

while connecting to a host using fabric in python, getting a prompt(Login password for 'root':) asking for root password

In python file code is something like this:
with settings(host_string=elasticIP, key_filename = '/path/to/keyfile.pem', user = 'root', use_ssh_config = False):
run("any command")
After executing this, a prompt appears in console stating "Login password for 'root':" As we dont have a password for this user 'root' so we are not sure what password to provide, but, randomly giving any text as a password for 'root' in the console it accepts and moves further.
When we run same python program in background using nohup then same prompt sometimes appears sometimes it dose not appears.
We are working on eucalyptus instances and hope environment is not an issue here.
Please suggest...
When you are using settings, don’t use run, use execute.
from fabric.decorators import task
from fabric.operations import run, execute
def my_command():
run('echo "hello fabric!"')
#task
def my_task():
with settings(host_string=elasticIP, key_filename = '/path/to/keyfile.pem', user = 'root', use_ssh_config = False):
execute(my_command)
I diged more into this issue and finally got an answer:
file :/etc/ssh/ssh_config
holds a property 'UserKnownHostsFile' and 'StrictHostKeyChecking'
these properties should be addressed as :
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null
UserKnownHostsFile defines the storing of ssh and some metadata regarding to a particular machine. If there is a mismatch in these details it asks you to confirm whether the machine u SSHed is the correct machine or not.
So making these changes, all went well.
references:
http://linuxcommando.blogspot.in/2008/10/how-to-disable-ssh-host-key-checking.html
and
https://superuser.com/questions/19563/how-do-i-skip-the-known-host-question-the-first-time-i-connect-to-a-machine-vi

Implement password caching

I wrote a python script that lets users log in into a website by entering username and password and submit content using terminal. I am using mechanize to accomplish this. The problem is that users have to enter username and password every time they run the script. I want some way to cache the password for some time so that user doesn't have to enter password everytime and they get logged in for some time after they enter they enter username and password once. How can I do this?

Python: How do I set the password on an ubuntu user account using Fabric

I'm trying to create a user account on a remote Ubuntu system using Fabric. I want the account to have a strong password. I can use the following to create the account:
sudo('useradd -m test -s /bin/bash')
The problem is I'm not sure how to set the password. The useradd -p options requires an encrypted password. How do I set the password? How does the salt get passed to the remote system?
Example code would be appreciated.
Thanks
Try chpasswd. Unlike passwd, you can pass the username and password in the command's standard input. So you can for example upload a file containing the password and put this line in your fabfile:
sudo('chpasswd < my_password_file')
From man chpasswd:
The chpasswd command reads a list of user name and password pairs from standard input and uses this information to update a group of existing
users. Each line is of the format:
user_name:password
The supplied passwords must be in clear-text.
I do it like this:
sudo('useradd %s' % username)
sudo('echo %s | passwd %s --stdin' % (username, username))
sudo('chage -d 0 %s' % username)
The password will match the username and the user will be prompted to change their password at first login.

Categories

Resources