大家好,我正在Raspberry Pi-3上製作一款汽車。我正在發送一些shh命令。我的問題ID,我不想直接發送shh命令,雖然我想發送一個python控制檯輸出的輸出我該怎麼做?我想發送一個python腳本輸出到SSH shell
0
A
回答
0
您可以在python中使用OS庫,這將允許python程序與unix shell集成。
#Import Library
import os
#Set the action to 0 as it's not needed right now
action = 0
#Loop infinitely
while True:
#First of all, list the available actions and let the user choose one.
print('Actions that can be performed')
print('ID. Action')
print('')
print('1. Go Forward')
print('2. Go Backwards')
print('3. Stop')
print('')
print('0. Stop and exit program')
print('')
#Ask the 'driver' what they want the car to do. Program will hang continuing
#to perform any current action that was previously selected until a different
#action is provided.
action = int(input('Enter the numerical ID of the action and press return: '))
#Do the action based on the numerical ID
if(action == 1):
os.system('shell-command-to-go-forward')
if(action == 2):
os.system('shell-command-to-go-backward')
if(action == 3):
os.system('shell-command-to-stop-everything')
if(action == 0):
os.system('shell-command-to-stop-everything')
exit(0)
如果這不是你想要做的,請你能更具體。 python腳本應該採取任何形式的用戶輸入?
0
我無法幫助您使用您尋求的ssh功能,因爲我從來沒有嘗試過,也沒有我現在可以使用的工具來嘗試。但是我不相信這是達到目的的正確工具。 而是我附加一個套接字服務器示例與tkinter GUI,按箭頭鍵將事件發送到套接字服務器,然後可以採取行動。
server.py:
import socket
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = ('localhost', 10000)
print('starting up on %s port %s' % server_address)
sock.bind(server_address)
while True:
try:
# Listen for incoming connections
sock.listen(1)
while True:
# Wait for a connection
print('waiting for a connection')
connection, client_address = sock.accept()
try:
print('connection from', client_address)
# Receive the data in small chunks and retransmit it
while True:
data = connection.recv(16)
if data:
print('received "%s"' % data)
# here you can test the character received and act accordingly
# you could also opt to send data back the other way
# print('sending data back to the client')
# connection.sendall(data)
else:
print('no more data from', client_address)
break
finally:
# Clean up the connection
connection.close()
except:
pass
client.py
import socket
import tkinter as tk
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.title("Car control")
self.sock = None
self.addr = tk.Entry(self)
self.addr.insert(0, "127.0.0.1")
self.addr.grid(column=1, row=1, sticky="nesw")
self.button = tk.Button(self, text="Connect", command=self.connect)
self.button.grid(column=2, row=1, sticky="nesw")
self.bind("<KeyPress>", self.handle_binding)
self.bind("<KeyRelease>", self.handle_binding)
def connect(self):
try:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.addr.get(),10000))
self.addr.configure(state="disabled")
self.button.configure(text="Disconnect", command=self.disconnect)
self.focus()
except (ConnectionRefusedError, socket.gaierror):
pass
def disconnect(self):
self.sock.close()
self.sock = None
self.addr.configure(state="normal")
self.button.configure(text="Connect", command=self.connect)
def send(self, data):
if self.sock:
self.sock.sendall(data.encode('utf8'))
def handle_binding(self, event):
if event.widget != self.addr: # ignore key events aimed at texkbox
char = ""
if event.keysym == "Up":
char = "u"
elif event.keysym == "Down":
char = "d"
elif event.keysym == "Left":
char = "l"
elif event.keysym == "Right":
char = "r"
if event.type == '2': # pressed
self.send(char)
elif event.type == '3': # released
self.send(char.upper())
if __name__ == "__main__":
app = App()
app.mainloop()
相關問題
- 1. shell腳本:發送sed輸出到mysql?
- 2. bash腳本ssh到一個盒子,讓我到一個python shell
- 3. SSH Shell腳本多個命令輸出
- 4. 管fswatch輸出到一個shell腳本
- 5. 發送用戶輸入到shell腳本
- 6. Python Paramiko發送CTRL + C到ssh shell
- 7. SSH Shell腳本
- 8. Shell腳本 - SSH
- 9. 從shell腳本發送輸出到文件
- 10. 如何發送shell腳本(json)輸出到aws sns主題?
- 11. shell腳本的輸出是另一個shell腳本
- 12. 從shell輸入一個python腳本?
- 13. 來自shell腳本的SSH另一個shell腳本
- 14. 如何將輸出從一個python腳本輸入到另一個python腳本?
- 15. 多個python腳本發送消息到一箇中央腳本
- 16. 在unix shell腳本中發送輸出時發生mailx問題
- 17. 捕獲一個shell腳本的輸出是從另一個shell腳本
- 18. 我想格式化輸出將在shell腳本
- 19. 用SSH在shell中啓動Python腳本
- 20. 如何將VBA輸出發送到Python腳本?
- 21. 如何將SIGINT發送到shell腳本?
- 22. shell腳本ssh命令退出狀態
- 23. 詹金斯發佈通過SSH運行一個shell腳本
- 24. 用於讀取另一個腳本的輸出的Shell腳本
- 25. 如何分發我的Python/shell腳本?
- 26. 我試圖找到一個shell腳本
- 27. 使用python ssh運行本地shell腳本到遠程
- 28. 將一個python腳本的值發送到另一個
- 29. Shell腳本-directory compare-輸出
- 30. Shell腳本輸出奇怪
你的問題是不明確的,你要輸出(打印報表等)發送到SSH shell或做你該做希望你的python腳本的輸出被視爲shell命令?如果第一個從ssh會話中調用腳本有什麼問題? –
我想把python控制檯的輸出作爲我輸入到SSH shell的輸入 – salmanarshad1999
真的有兩個選擇,你可以使用'subprocess.Popen'跨越你的ssh連接,然後發送消息到那個,或者你可以使用一個python ssh庫如http://www.paramiko.org/無論哪種方式,你將不得不改變你的腳本,以便「輸出」不打印,而是發送到子進程或ssh模塊 –