如何通过SSH登录在另一个远程系统上执行这4个命令的程序?

huangapple go评论75阅读模式
英文:

HOW TO have my program do these 4 commands on another remote system via SSH login?

问题

我想要一些帮助,学习一种更好(更简单?)的方法来使这个PYTHON3程序执行这4个命令...而不是我现在的方式。现在的方式可以正常工作,但是对于这四个命令中的每一个,它都会在远程设备上引发6个“安全”事件...每次这个PYTHON3程序运行并完成其工作时,都会有16个安全事件。所以问题不是它不工作...它正在工作...只是在我的远程设备上引发了很多安全警报。

让我解释一下。
当PYTHON3程序执行时(受到另一个程序的控制)会执行4个操作:
1)PYTHON3发送SCP命令将一个视频.mp4文件发送到RPi170。这个SCP命令会导致RPi170上的SSHD认证成功和相应的新登录会话,来自PYTHON3机器。PYTHON3使用免密码SSH,并且它的公钥已经发送到RPi170,所以不需要使用密码。然后这还会导致一个新的PAM登录会话被打开(PYTHON3机器现在已经登录到RPi170)。SCP传输正常进行(文件被传输),一旦SCP命令完成,这会导致PYTHON3机器注销RPi170并关闭其PAM登录会话。所有这些都正常工作,如预期的那样。

  1. 接下来,PYTHON3程序发送SCP命令以使用SCP将JPG照片文件发送到RPi170。与上面相同的过程再次发生,另一个新的SSHD和另一个认证成功以及相应的新登录会话在RPi170上发生,最终导致PYTHON3机器注销RPi170并关闭此PAM登录会话。

  2. 接下来,PYTHON3程序发送另一个SSH登录和TOUCH命令以创建一个新的空文件。

  3. 最后,步骤4,PYTHON3程序发送另一个SSH登录和CHOWN命令。

这是工作中的PYTHON3程序:

#!/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()

PYTHON3程序是通过以下代码启动的,但我认为这不重要。

//  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());

我的问题是:在PYTHON3程序中是否有一种方法...只需SSH登录到RPi170...登录一次...并执行这4个任务...在相同的登录中?这将更清洁,RPi170上的安全事件也会少得多。

谢谢你的倾听,希望一些专家可以帮助我找到一种更简单的方法,只需一次登录即可完成这项任务。

非常感谢。
Dave

正如我所说,我现在拥有的程序是可以工作的,只是每次运行PYTHON3程序时,我会得到16个单独的安全事件。

英文:

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):

  1. 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.

  2. 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.

  3. next the PYTHON3 program sends another SSH login and a TOUCH command to create a new empty file.

  4. 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.

答案1

得分: 0

我已经找到了一个更好的解决方案,来解决我上面提出的问题。
而不是使用PYTHON,我正在使用一个bash脚本,其中包含一个更好的SCP命令,将3个文件发送到同一个目标文件夹,而不是使用3个单独的SCP命令。
我认为这个问题已经解决。
Dave

英文:

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

huangapple
  • 本文由 发表于 2023年8月4日 08:38:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76832315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定