Python Forum
Raspi Commands via Python (VS Code) Script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Raspi Commands via Python (VS Code) Script
#1
The following Code is what I have written to access my raspi and input a series of commands to run sequentially. What i cannot figure out, is how to make the "snapshot.py" command pause, and allow me to do the start and capture commands for the photo to show up on the device, when i input the command individually into the raspi it works and stores the photo in the proper directory but not when i have this running. I also need to make sure the results of the commands/onboarding tests get stored onto a txt file. Any advice is greatly appreciated, I am new to the raspi world so please all critique is welcome. Thank you

import paramiko, subprocess


p = paramiko.SSHClient()
p.set_missing_host_key_policy(paramiko.AutoAddPolicy())
p.connect("10.0.0.65", port=22, username="dev", password="Production1!")
stdin, stdout, stderr = p.exec_command('ls')
opt = stdout.readlines()
opt = "".join(opt)
print(opt)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('sudo raspi-config')
opt0 = stdout.readlines()
opt0 = "".join(opt0)
print(opt0)
input("press enter to continue")


stdin, stdout, stderr = p.exec_command('sed -n 43p /proc/cpuinfo')
opt1 = stdout.readlines()
opt1 = "".join(opt1)
print(opt1)
input("press enter to continue")


stdin, stdout, stderr = p.exec_command('sudo nano /etc/hostname')
opt2 = stdout.readlines()
opt2 = "".join(opt2)
print(opt2)
input("press enter to continue")


stdin, stdout, stderr = p.exec_command('sudo nano /etc/hosts')
opt3 = stdout.readlines()
opt3 = "".join(opt3)
print(opt3)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('sudo touch ../../full_log.sh')
opt4 = stdout.readlines()
opt4 = "".join(opt4)
print(opt4)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('sudo touch ../../live_log.sh')
opt5 = stdout.readlines()
opt5 = "".join(opt5)
print(opt5)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('sudo touch ../../update.sh')
opt6 = stdout.readlines()
opt6 = "".join(opt6)
print(opt6)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('sudo touch ../../watch_and_update.sh')
opt7 = stdout.readlines()
opt7 = "".join(opt7)
print(opt7)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('echo sudo journalctl -u spiderentry.service | sudo tee ../../full_log.sh')
opt8 = stdout.readlines()
opt8 = "".join(opt8)
print(opt8)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('echo sudo journalctl -f -u spiderentry.service| sudo tee ../../live_log.sh')
opt9 = stdout.readlines()
opt9 = "".join(opt9)
print(opt9)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('echo python3 ~/spiderkeypadpi/main/firmware_update.py | sudo tee ../../update.sh')
opt10 = stdout.readlines()
opt10 = "".join(opt10)
print(opt10)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('echo python3 ~/spiderkeypadpi/main/firmware_update.py | sudo tee ../../watch_and_update.sh')
opt11 = stdout.readlines()
opt11 = "".join(opt11)
print(opt11)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('echo sudo journalctl -f -u spiderentry.service | sudo tee -a ../../watch_and_update.sh')
opt12 = stdout.readlines()
opt12 = "".join(opt12)
print(opt12)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('sudo chmod +x ../../full_log.sh')
opt13 = stdout.readlines()
opt13 = "".join(opt13)
print(opt13)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('sudo chmod +x ../../live_log.sh')
opt14 = stdout.readlines()
opt14 = "".join(opt14)
print(opt14)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('sudo chmod +x ../../update.sh')
opt15 = stdout.readlines()
opt15 = "".join(opt15)
print(opt15)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('sudo chmod +x ../../watch_and_update.sh')
opt16 = stdout.readlines()
opt16 = "".join(opt16)
print(opt16)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('ssh-keygen -f ~/.ssh/id_rsa -t rsa -N "')
opt17 = stdout.readlines()
opt17 = "".join(opt17)
print(opt17)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('cat ~/.ssh/id_rsa.pub')
opt18 = stdout.readlines()
opt18 = "".join(opt18)
print(opt18)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('git clone [email protected]:SpiderDoorLLC/spiderkeypadpi.git')
opt19 = stdout.readlines()
opt19 = "".join(opt19)
print(opt19)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('cd spiderkeypadpi/services')
opt20 = stdout.readlines()
opt20 = "".join(opt20)
print(opt20)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('sudo chmod +x setup.sh')
opt21 = stdout.readlines()
opt21 = "".join(opt21)
print(opt21)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('sudo ./setup.sh')
opt22 = stdout.readlines()
opt22 = "".join(opt22)
print(opt22)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('sudo chmod -x setup.sh')
opt23 = stdout.readlines()
opt23 = "".join(opt23)
print(opt23)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('cd ../main')
opt24 = stdout.readlines()
opt24 = "".join(opt24)
print(opt24)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('sudo python3 serial_number.py')
opt25 = stdout.readlines()
opt25 = "".join(opt25)
print(opt25)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('sudo python3 relay.py')
opt26 = stdout.readlines()
opt26 = "".join(opt26)
print(opt26)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('sudo python3 snapshot.py')
opt27 = stdout.readlines()
opt27 = "".join(opt27)
print(opt27)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('sudo rm 000*')
opt28 = stdout.readlines()
opt28 = "".join(opt28)
print(opt28)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('sudo python3 main.py')
opt29 = stdout.readlines()
opt29 = "".join(opt29)
print(opt29)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('cd spiderkeypadpi/services')
opt30 = stdout.readlines()
opt30 = "".join(opt30)
print(opt30)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('sudo python3 stop_services.py')
opt31 = stdout.readlines()
opt31 = "".join(opt31)
print(opt31)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('cd ../main ')
opt32 = stdout.readlines()
opt32 = "".join(opt32)
print(opt32)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('sudo python3 snapshot.py')
opt33 = stdout.readlines()
# # capture and end commands
# # report for photo or failure of camera
opt33 = "".join(opt33)
print(opt33)
input("press enter to continue")

stdin, stdout, stderr = p.exec_command('sudo python3 piezo.py')
opt34 = stdout.readlines()
opt34 = "".join(opt34)
print(opt34)
buran write Oct-21-2020, 07:41 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Not exactly sure what you ask or what your problem is, but don't you see the repetitive code? why not just loop over the commands
import paramiko, subprocess

p = paramiko.SSHClient()
p.set_missing_host_key_policy(paramiko.AutoAddPolicy())
p.connect("10.0.0.65", port=22, username="dev", password="Production1!")

commands = ('ls', 'sudo raspi-config', 'sed -n 43p /proc/cpuinfo', 'sudo nano /etc/hostname',
            'sudo nano /etc/hosts', 'sudo touch ../../full_log.sh', 'sudo touch ../../live_log.sh',
            'sudo touch ../../update.sh', 'sudo touch ../../watch_and_update.sh', 
            'echo sudo journalctl -u spiderentry.service | sudo tee ../../full_log.sh', 
            'echo sudo journalctl -f -u spiderentry.service| sudo tee ../../live_log.sh', 
            'echo python3 ~/spiderkeypadpi/main/firmware_update.py | sudo tee ../../update.sh', 
            'echo python3 ~/spiderkeypadpi/main/firmware_update.py | sudo tee ../../watch_and_update.sh', 
            'echo sudo journalctl -f -u spiderentry.service | sudo tee -a ../../watch_and_update.sh', 
            'sudo chmod +x ../../full_log.sh', 'sudo chmod +x ../../live_log.sh', 'sudo chmod +x ../../update.sh', 
            'sudo chmod +x ../../watch_and_update.sh', 'ssh-keygen -f ~/.ssh/id_rsa -t rsa -N "', 
            'cat ~/.ssh/id_rsa.pub', 'git clone [email protected]:SpiderDoorLLC/spiderkeypadpi.git', 
            'cd spiderkeypadpi/services', 'sudo chmod +x setup.sh', 'sudo ./setup.sh', 
            'sudo chmod -x setup.sh', 'cd ../main', 'sudo python3 serial_number.py', 
            'sudo python3 relay.py', 'sudo python3 snapshot.py', 'sudo rm 000*', 
            'sudo python3 main.py', 'cd spiderkeypadpi/services', 'sudo python3 stop_services.py', 
            'cd ../main ', 'sudo python3 snapshot.py', 'sudo python3 piezo.py')

for command in commands:
    stdin, stdout, stderr = p.exec_command(command)
    print("".join(stdout.readlines()))
    input("press enter to continue")
225 lines into just 27 lines
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
exec_command being non-blocking call, maybe look at https://stackoverflow.com/a/28486051/4046632
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(Oct-21-2020, 07:54 AM)buran Wrote: Not exactly sure what you ask or what your problem is, but don't you see the repetitive code? why not just loop over the commands
import paramiko, subprocess

p = paramiko.SSHClient()
p.set_missing_host_key_policy(paramiko.AutoAddPolicy())
p.connect("10.0.0.65", port=22, username="dev", password="Production1!")

commands = ('ls', 'sudo raspi-config', 'sed -n 43p /proc/cpuinfo', 'sudo nano /etc/hostname',
            'sudo nano /etc/hosts', 'sudo touch ../../full_log.sh', 'sudo touch ../../live_log.sh',
            'sudo touch ../../update.sh', 'sudo touch ../../watch_and_update.sh', 
            'echo sudo journalctl -u spiderentry.service | sudo tee ../../full_log.sh', 
            'echo sudo journalctl -f -u spiderentry.service| sudo tee ../../live_log.sh', 
            'echo python3 ~/spiderkeypadpi/main/firmware_update.py | sudo tee ../../update.sh', 
            'echo python3 ~/spiderkeypadpi/main/firmware_update.py | sudo tee ../../watch_and_update.sh', 
            'echo sudo journalctl -f -u spiderentry.service | sudo tee -a ../../watch_and_update.sh', 
            'sudo chmod +x ../../full_log.sh', 'sudo chmod +x ../../live_log.sh', 'sudo chmod +x ../../update.sh', 
            'sudo chmod +x ../../watch_and_update.sh', 'ssh-keygen -f ~/.ssh/id_rsa -t rsa -N "', 
            'cat ~/.ssh/id_rsa.pub', 'git clone [email protected]:SpiderDoorLLC/spiderkeypadpi.git', 
            'cd spiderkeypadpi/services', 'sudo chmod +x setup.sh', 'sudo ./setup.sh', 
            'sudo chmod -x setup.sh', 'cd ../main', 'sudo python3 serial_number.py', 
            'sudo python3 relay.py', 'sudo python3 snapshot.py', 'sudo rm 000*', 
            'sudo python3 main.py', 'cd spiderkeypadpi/services', 'sudo python3 stop_services.py', 
            'cd ../main ', 'sudo python3 snapshot.py', 'sudo python3 piezo.py')

for command in commands:
    stdin, stdout, stderr = p.exec_command(command)
    print("".join(stdout.readlines()))
    input("press enter to continue")
225 lines into just 27 lines

Thank you for this, this was helpful and my apologies for no tags. To answer your question of what I am trying to do, with the snapshot.py command there are two more steps in the raspi that happen when that command is entered individually in the raspi, it asks for "start", "capture" and "end" to complete the photo taking and it stores the photo on the pi in the proper drive. When i have the code written like this, it does not ask for the steps or store the photo it almost "glances over" that command. I also need to store the results from these commands in a .txt file as well and am stuck on that. I hope that clarifies, again thanks for your help. See the following for the snapshot explanation:

dev@Test_normal:~/spiderkeypadpi/main $ sudo python3 snapshot.py
Enter start, capture, stop, or end
SNAPSHOT: Starting preview
start
SNAPSHOT: start preview requested
Enter start, capture, stop, or end
SNAPSHOT: Starting preview
capture
SNAPSHOT: start preview requested
SNAPSHOT: Preview is already running
SNAPSHOT: Snapshot requested:0000+Wed, 21 Oct 2020 15:23:18 utc+Test+0+0+.jpg
Enter start, capture, stop, or end
SNAPSHOT: snapshot called:0000+Wed, 21 Oct 2020 15:23:18 utc+Test+0+0+.jpg
SNAPSHOT: photo size:110117
end
SNAPSHOT: Camera process finished
made it
Reply
#5
did you check my second post and how to wait till command execution ends? Also, of you need to provide input to remote script, shouldn't you write to stdin somewhere in your code? All you do is to read output and print it

as to writing to file - just open a file for writing and then instead of/along with print, write to the file
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(Oct-21-2020, 03:27 PM)buran Wrote: did you check my second post and how to wait till command execution ends? Also, of you need to provide input to remote script, shouldn't you write to stdin somewhere in your code? All you do is to read output and print it

as to writing to file - just open a file for writing and then instead of/along with print, write to the file

I did, yes. So the ssh.exec_command() would follow that snapshot command only? correct?

And i got the write to file, thank you. I appreciate your assistance as I am new to this.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,181 Jun-29-2023, 11:57 AM
Last Post: gologica
  Sending string commands from Python to a bluetooth device Rovelin 13 9,434 Aug-31-2021, 06:40 PM
Last Post: deanhystad
  Python library for win32 console commands eldiener 3 3,448 Aug-24-2021, 10:28 PM
Last Post: bowlofred
  Possible to execute a python script before log off/shutdown with input commands? Kaltex 1 2,258 May-18-2021, 06:31 AM
Last Post: Skaperen
  cant use ping, sudo or other commands in remote shell script. throwaway34 7 3,579 May-17-2021, 11:29 AM
Last Post: throwaway34
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,872 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,288 May-28-2020, 05:27 PM
Last Post: micseydel
  how to get PID's of linux commands executed through python modules? Manikandan_PS 4 3,043 Mar-12-2020, 07:16 AM
Last Post: Manikandan_PS
  Questions re: my first python app (GUI, cross-platform, admin/root-level commands)? DonnyBahama 0 1,725 Feb-27-2020, 08:14 PM
Last Post: DonnyBahama
  Python script dioesn`t run all commands exploit123 5 2,904 Sep-25-2019, 03:15 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020