1

I have 2 raspberry, and I want to have python script on pi1 that will open file and write something into it. That works on pi1. But when i try to run script from pi2 via ssh, it wont write to file.

On pi1 I have a simple script that write something to *.txt file. I gave 777 to the file. The script works on pi1. Then I try to execute it from pi2. ssh pi@pi1 'python /path/to/file.py' ,but it doesn't write to pi1. On pi1 user is pi, on pi2 user is also pi.

What am I doing wrong ?

3
  • Could we have a bit more detail, please? If you're running the script on Pi1, you might be running as user "pi". The user ID used by SSH needs to have the correct permission to write to the file. So, what are the permissions on the file you want to write, or the directory if you are creating a file? What is the user ID when you SSH in to Pi2?
    – Bob Brown
    Dec 3 '19 at 23:50
  • Thanks for your answer. On pi1 i have a simple script that write something to txt file. I gave 777 to txt. Script works on pi1. Than i try to execute it from pi2. ssh pi@pi1 ‘python /path/to/file.py’ ,but it doesnt write to pi1. On pi1 user is pi, on pi2 user is also pi. Thanks Dec 4 '19 at 5:44
  • Are you able to make ssh from pi1 to pi2 ??? otherwise try ssh pi@ip_address_pi2 and also make sure enable ssh in pi2
    – DRV
    Dec 4 '19 at 13:43
1

What you try to do isn't possible this way with ssh. You have to understand how ssh works. It's abbreviation means secure shell and it opens a console (shell) on the connected pi2. It is a software terminal and keyboard doing just the same as a monitor and keyboard direct connected to pi2. With this you can't simply store content to a file on pi1 because there is no connection to it.

With ssh you are working on pi2 and not on pi1. The latter only give you the screen and the keyboard. You can run your script on pi2 within the ssh console and write to a file on rpi2. Then you have to copy it from pi2 to pi1 by network. There is a simple program scp (just made from ssh) that can do it. To copy the file from pi2 to pi1 from within the ssh console you can execute:

pi2 ~$ scp /path/to/file-on-pi2.txt pi@pi1:/path/to/file-on-pi1-now.txt

Or copy the same file staying on pi1:

pi1 ~$ scp pi@pi2:/path/to/file-on-pi2.txt /path/to/file-on-pi1-now.txt

There are other ways to have direct access to files on a remote device with network shares using samba or with exports using NFS.

3
  • So there is no way to write to pi2 from pi1? I have script on pi2 that writes arg to txt file. When i execute “script.py 222” it writes 222 to file.txt. That works on pi2. But when i try from pi 1: “ssh pi@pi2 ‘python path/to/script.py 222’” it wont write. For example i put print “something” in script.py and when i execute it from pi1, it shows me a print but wont weite to file. Dec 7 '19 at 10:58
  • I solved the problem. My script didnt have full path to txt. f.open(path/to/file.txt) instead f.open(file.txt) Dec 7 '19 at 11:37
  • @DejanPesic I do not understand what you mean. You can redirect the output of a remote executed program to stdout and get it via ssh on the local device on stdin, if you mean that. But because you found the solution, please make an answer about it and accept it after two days. This will finish your question and it will not pop up again after months.
    – Ingo
    Dec 7 '19 at 12:47
0

Enable the ssh of pi1 using raspi-config

Open the terminal type

raspi-config

enable the ssh configuration.

Open the terminal in pi2 and Type

ssh pi@ipaddressofpi1

Hope this will work. Cheers

2
  • Thanks but i already have alot of working scripts, my only problem is writing to txt file. Cheers Dec 4 '19 at 15:36
  • try sudo nano filename.txt and then edit it
    – DRV
    Dec 5 '19 at 4:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.