Python rsync program
I am trying to create a Python (Ubuntu) program "To get a file from a directly connected Linux computer without asking for a password"
I am currently using this command in python, but I want to put the password ahead of time so that it doesn't ask for a password.
import os
os.system("echo 'hello world'")
os.system("rsync -rav pi@192.168.2.34:python ~/")
IP address of another Linux machine: 192.168.2.34 Password: raspberry Hostname: pi
source to share
You can achieve this with exchanging private keys
. This way you can get the file from a directly connected Linux computer without asking for a password. Following are the steps for exchanging private keys:
- Run the command
ssh-keygen
on your Ubuntu terminal. -
Keep clicking until something like this appears:
The key randomart image is: +--[ RSA 2048]----+ | . .*| | . + +o| | + * + .| | o E * * | | S + + o +| | o o o | | . . . | | | | | +-----------------+
-
After that, execute
ssh-copy-id pi@192.168.2.34
and enter the password, i.e.raspberry
if it's a password for another computer.
Now execute the python script as usual and it won't ask for a password.
import os
os.system("echo 'hello world'")
os.system("rsync -rav pi@192.168.2.34:python ~/")
source to share
You can try the following using pexpect and subprocess, pexpect should definitely work, subprocess I'm not sure:
cmd = "rsync -rav pi@192.168.2.34:python ~/"
from pexpect import *
run(cmd,events={'(?i)password':'your_password\r'})
from subprocess import PIPE,Popen
cmd = "rsync -rav pi@192.168.2.34:python ~/"
proc = Popen(cmd.split(),stdin=PIPE)
proc.stdin.write('your_pass\r')
proc.stdin.flush()
If you are not using pexpect use pip install pexpect
source to share
If you are on a private network (it should be like 192.168 ..) and if you trust all the IP addresses on that network (this means that an unauthorized user can confuse the IP address), you can also use authentication.
Extract from the man page for ssh (assuming you are using it as the base protocol for rsync):
Host-based authentication works as follows: if the machine from which the user is logged on is listed in /etc/hosts.equiv or / etc / shosts.equiv on the remote machine and the usernames are the same on both sides, or if the ~ / .rhosts or ~ / .shosts exist in the user's home directory on the remote computer and contain a string containing the name of the client machine and the username on that computer that the user is considered to be logged in.
That is, you put in your home directory a pi
file .shosts
containing one line
name_or_ip_address_of_source_machine user_name_on_source_machine
if the file already exists, just add this line.
But ... you should understand that, as with the BHAT IRSHAD solution, this means that you are now allowed to send any command on the dest machine as the pi user without a password.
source to share