我想将python的脚本输出发送到SSH-sh

2024-04-27 13:06:16 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好,我正在用覆盆子皮3造车。我正在发送一些嘘命令。我的问题是我不想直接发送shh命令,尽管我想发送python控制台输出的输出,我该怎么做?在


Tags: 命令覆盆子shh造车
2条回答

我无法帮助您使用ssh功能,因为我从未尝试过,也没有工具可供我尝试。但我不相信这个工具的目的是对的。 相反,我附加了一个带有tkinter GUI的socket服务器示例,按箭头键将事件发送到socket服务器,然后可以对其进行操作。在

在服务器.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

在客户端.py在

^{pr2}$

您可以使用python中的OS库,这将允许python程序与unixshell集成。在

#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脚本是否应该采用任何形式的用户输入?在

相关问题 更多 >