0

I am trying to implement a (local fake) server with paramiko that responds to custom commands just like the original for testing purposes. Mainly copying from the supplied demo server, I managed to come up with this custom method to handle exec_requests for the server implemented via paramiko.ServerInterface:

def check_channel_exec_request(self,channel,command):
    print("User wants to execute '%s'" %(command))
    comm_main, comm_add = command.decode().split(sep=" ",maxsplit=1)
    if comm_main in self.allowed_commands:
        chout = channel.makefile("w")
        subprocess.Popen(command,stdout=chout)
        return True
        else:
            return False

After the server is running via:

PORT = 50007 # The same port as used by the server
HOST = 'localhost'
host_key = paramiko.RSAKey(filename = <FILEPATH>)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind((HOST,PORT))
    sock.listen(1)
    conn, addr = sock.accept() # Connected!
    with conn:
        trans = paramiko.Transport(conn)
        trans.add_server_key(host_key)
        trans.set_subsystem_handler("job",JobHandler)
        server = FakeCluster() # Custom class sublassing paramiko.ServerInterface
        trans.start_server(server=server)
        chan = trans.accept() # Accept authentication from client
        while trans.is_active(): # Do not close until inactive
            time.sleep(1)
        chan.close()

the client would try to execute echo 123 in the following manner:

PORT = 50007 # The same port as used by the server
HOST = 'localhost' 
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
    sock.bind((HOST,PORT))

    ssh = paramiko.client.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(HOST, PORT,username=<USERNAME>,password=<PASSWORD>)
    ssh.exec_command("echo 123")

Right now, I am getting the error trying to execute the subprocess that 'ChannelFile' has no attribute 'fileno'. Furthermore I am wondering how to later execute a python script as a custom command called by exec_command. (Maybe by calling a batchfile that calls the python script?)

I. Amon
  • 174
  • 11
  • Found a solution / explanation to the `fileno` error [here](https://stackoverflow.com/questions/44503787/use-paramikos-stdout-as-stdin-with-subprocess) – I. Amon Dec 04 '17 at 09:02

1 Answers1

-1

your question is really very confusing , here is what i did for communicating with remote server .

local machine : window

Remote Machine :ubuntu

Command on remote machine :'who'

import paramiko
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.268.21.26',port=22,username='root',password='default')
stdin,stdout,stderr=ssh.exec_command('who')
output=stdout.readlines()
print '\n'.join(output)

#output
#root    tty7         2017-11-28 14:13 (:0)

#if you wish to execute a python file there , this should work
stdin,stdout,stderr=ssh.exec_command('python file.py')
pankaj mishra
  • 2,555
  • 2
  • 17
  • 31
  • Thanks for your answer, I admit that I have been really confused writing that question. I summarized my progress in [this question](https://stackoverflow.com/questions/47623800/paramiko-server-signalling-the-client-that-stdout-is-closed) which hopefully clarifies my general problem. – I. Amon Dec 04 '17 at 08:55