こんにちは皆私はRaspberry Pi-3で車を作ります。私はいくつかのshhコマンドを送信しています。私の問題IDは、私は直接、私はpythonコンソール出力の出力を送信したいのですが、私はどのようにそうするのですか?Pythonのスクリプト出力をSSHシェルに送信したい
0
A
答えて
0
OSライブラリをPythonで使用すると、PythonプログラムをUNIXシェルと統合することができます。
#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. シェルのコマンド出力を送信する
- 2. Perlスクリプト - パイプされたsshシェルからのSTDOUTをファイルに出力します。
- 3. SSHのシェルでPythonスクリプトを開始
- 4. pythonスクリプトの出力を電子メールアドレスに送信する方法
- 5. Pythonスクリプト出力しないSSHコマンドが正しく
- 6. ストアPHPスクリプトの出力シェル
- 7. Pythonスクリプトで電子メールを送信するcrontabを使用して出力して週に送信する
- 8. SSHシェルに送信された文字列にエンコードされたリターン/エンターキーの入力方法は?
- 9. 別のpythonスクリプトからpythonスクリプトを実行してその出力をテキストファイルで送信する方法
- 10. JavaからPythonスクリプトを実行して入出力を送信する
- 11. Emacsは、出力のpythonシェルに結果
- 12. は、まず、私はPythonスクリプトに変数を送信ノードアプリとPythonスクリプト 間で通信するために、パイソン、シェルを使用して、Node.jsのアプリ
- 13. Python - CSVにSSHコマンド出力を保存
- 14. Python Tkinterウィンドウ出力とシェル入力
- 15. PythonスクリプトPexpect SSH
- 16. 作成したSubprocess Pythonに通信で入力(コマンド)を送信
- 17. に私は、標準入力/出力を使用して、簡単なPythonスクリプトを持っているSSH
- 18. PythonスクリプトからJQueryにJSONを送信
- 19. bashからpythonスクリプトにパラメータを送信
- 20. pythonスクリプトでstdinをサブプロセスに送信
- 21. PythonのサブプロセスPopenがシェルにすべての引数を送信しない
- 22. SSHを介してスクリプト出力を表示しますか?
- 23. スクリプト出力をsyslogに送る方法
- 24. bashスクリプトを使ってボックスにsshを入れて、私をPythonシェルにしてください
- 25. pythonスクリプトから別のpythonスクリプトへの値の送信
- 26. クエリー出力のメール送信
- 27. SSHでpythonスクリプトを実行
- 28. Synologyのssh送信コマンド
- 29. 出力 "Linuxシェル" -
- 30. Python:logging.streamhandlerが標準出力にログを送信していない
は、あなたの質問はあなたがSSHシェルに出力(print文など)を送信する場合、または行うのですか、明らかではないが、あなたのpythonスクリプトの出力をシェルコマンドとして扱いたいですか?もし最初にsshセッションからスクリプトを呼び出すのに間違っていたら? –
私は、SSHシェルの入力 – salmanarshad1999
への入力としてPythonコンソールの出力を扱いたいと思うのですが、 'subprocess.Popen'を使ってssh接続を広げ、それにメッセージを送ることができます。あるいは、Pythonのsshライブラリhttp://www.paramiko.org/のように、 "出力"が表示されずにサブプロセスまたはsshモジュールに送られるようにスクリプトを変更する必要があります。 –