3

I am having issues passing responses to a bash script on a remote server over SSH.

I am writing a program in Python 3.6.5 that will SSH to a remote Linux server. On this remote Linux server there is a bash script that I am running which requires user input to fill in. For whatever reason I cannot pass a user input from my original python program over SSH and have it fill in the bash script user input questions.

main.py

from tkinter import *
import SSH

hostname = 'xxx'
username = 'xxx'
password = 'xxx'

class Connect:
    def module(self):
        name = input()
        connection = SSH.SSH(hostname, username, password)
        connection.sendCommand(
            'cd xx/{}/xxxxx/ && source .cshrc && ./xxx/xxxx/xxxx/xxxxx'.format(path))

SSH.py

from paramiko import client

class SSH:

    client = None

    def __init__(self, address, username, password):
        print("Login info sent.")
        print("Connecting to server.")
        self.client = client.SSHClient()    # Create a new SSH client
        self.client.set_missing_host_key_policy(client.AutoAddPolicy())
        self.client.connect(
            address, username=username, password=password, look_for_keys=False) # connect

    def sendCommand(self, command):
        print("Sending your command")
        # Check if connection is made previously
        if (self.client):
            stdin, stdout, stderr = self.client.exec_command(command)
            while not stdout.channel.exit_status_ready():
                # Print stdout data when available
                if stdout.channel.recv_ready():
                    # Retrieve the first 1024 bytes
                    alldata = stdout.channel.recv(1024)
                    while stdout.channel.recv_ready():
                        # Retrieve the next 1024 bytes
                        alldata += stdout.channel.recv(1024)


                    # Print as string with utf8 encoding
                    print(str(alldata, "utf8"))
        else:
            print("Connection not opened.")

The final /xxxxxx in class Connect is the remote script that is launched. It will open a text response awaiting a format such as

What is your name:

and I cannot seem to find a way to properly pass the response to the script from my main.py file within the class Connect.

Every way I have tried to pass name as an argument or a variable the answer seems to just disappear (likely since it is trying to print it at the Linux prompt and not within the bash script)

I think using the read_until function to look for the : at the end of the question may work.

Suggestions?

1 Answer 1

1

Write the input that your command needs to the stdin:

stdin, stdout, stderr = self.client.exec_command(command)
stdin.write(name + '\n')
stdin.flush()

(You will of course need to propagate the name variable from module to sendCommand, but I assume you know how to do that part).

Sign up to request clarification or add additional context in comments.

1 Comment

this worked, thank you very much! I was not aware of the .write function for stdin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.