Issue
I would like some help to learn a better (simpler?) way to have this PYTHON3 program do these 4 commands... instead of the way I am doing it now. The way have works fine, but for EACH one of these commands, it is causing 6 "security" events on the remote device... for a whopping total of 16 security events for every time this PYTHON3 program runs and does its job. So it's not a problem that this is not working... it is working... this just is causing A LOT of security alerts in my remote device.
Let me explain. The PYTHON3 program does 4 things when it executes (under program control from another program):
PYTHON3 sends a SCP command to send a video .mp4 file to RPi170. This SCP command causes an SSHD authentication success and a corresponding new login session on RPi170 from PYTHON3 machine. The PYTHON3 uses passwordless SSH, and its public key has been prior send to RPi170, so there is no need to use PASSWORD. This then also causes a new PAM login session to be opened (PYTHON3 machine is now logged into RPi170). The SCP transfer happens just fine (the file is transferred), and once the SCP command is complete, this results in the PYTHON3 machine being logged out of RPi170 and its PAM login session is closed. All this is fine and works as expected.
next the PYTHON3 program sends a SCP command to send a JPG photo file to RPi170 using SCP. The same process as above happens, again with another new SSHD and another authentication success and a corresponding new login session on RPi170, and eventually the PYTHON3 machine being logged out of RPi170 and this PAM login session closed.
next the PYTHON3 program sends another SSH login and a TOUCH command to create a new empty file.
finally, step 4, the PYTHON3 program sends another SSH login and a CHOWN command.
Here is the working PYTHON3 program:
#!/usr/bin/env python3
# program by DAK
import time
import os
import sys
from sys import argv
from datetime import date
python_program_filename, theMotionFileTimestamp, VideoDuration = argv
formatted_year = date.today().strftime("%Y")
formatted_month = date.today().strftime("%m")
formatted_day = date.today().strftime("%d")
# SEND the MOTION VIDEO file to RPi170
VideoFilename = ("/home/dk/GIG460/CAPTURED-VIDEOS/MOTION-VIDEO_" + theMotionFileTimestamp + "_2.mp4")
scp_command = 'scp ' + VideoFilename + ' rpi170:/home/pi/G440/MOTION-DETECTIONS/' + formatted_year + '/' + formatted_month + '/' + formatted_day
os.system(scp_command)
time.sleep(2)
# SEND the MOTION PIX file to RPi170
PixFilename = ("/home/dk/GIG460/CAPTURED-PIXS/MOTION-PIX_" + theMotionFileTimestamp + "_2.jpg")
scp_command = 'scp ' + PixFilename + ' rpi170:/home/pi/G440/MOTION-DETECTIONS/' + formatted_year + '/' + formatted_month + '/' + formatted_day
os.system(scp_command)
time.sleep(2)
# CREATE the empty TXT file in which its FILENAME contains the MotionTimeStamp and MotionVideo duration information
touch_command = 'ssh rpi170 sudo touch /home/pi/G440/MOTION-DETECTIONS/' + formatted_year + '/' + formatted_month + '/' + formatted_day + '/' + theMotionFileTimestamp + '__' + VideoDuration + "_2.txt"
os.system(touch_command)
time.sleep(2)
# finally, change the OWNERSHIP of the files in the DAY directory for this date, so that NGINX web-server can access these video and photo files.
change_ownership_command = 'ssh rpi170 sudo chown -R www-data:pi /home/pi/G440/MOTION-DETECTIONS/' + formatted_year + '/' + formatted_month + '/' + formatted_day
os.system(change_ownership_command)
time.sleep(2)
sys.exit()
The PYTHON3 program is started by this code, fyi... but I don't think that is important.
// send the MotionVideoFile and MotionPIXFile and the empty TEXT file with it's FILENAME containing the MotionTimeStamp and Video Duration to RPi170
std::string pre_cmd2 = "python3 ~/PYTHON/Send_MotionFILES_to_RPi170.py " + savedMotionEventTimestamp + " " \
+ std::to_string(duration_value); // non-blocking way to send file
std::string returned_value2 = execute_cmd(pre_cmd2.c_str());
MY QUESTION: is there a way in the PYTHON3 program... to simply SSH into RPi170 ... login ONE TIME... and do all 4 of these tasks... within the SAME single LOGIN? That would be cleaner and much less security events on RPi170.
Thank you for listening and I hope some of the experts can help guide me to accomplish this easier with only one login.
THANK YOU so much. Dave
As I said, the program I have now IS WORKING, it is just that I get 16 separate security events for each time the PYTHON3 program runs.
Solution
I have found a better solution to the problem I asked above.
Instead of using PYTHON, I am using a bash script with a better SCP command to send 3 files to the same destination folder, instead of 3 separate SCP commands.
I consider this problem closed.
Dave
Answered By - DaveK Answer Checked By - Willingham (WPSolving Volunteer)