0

Use case

On a unix server , when login manually ,opens a command shell of its own to run the command.

Vshell prompt

I am trying to automate this by using paramiko , however , somehow i am not able to execute the command on command shell using paramiko

What i have done ?

I created a simple script which is able to make connection, but its not executing command on Vshell as the ouput is always coming empty.

import paramiko
import sys

ssh_client=paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=sys.argv[1],port=sys.argv[2],username=sys.argv[3],password=sys.argv[4])
command="show hwid"
stdin,stdout,stderr=ssh_client.exec_command(command)
out=stdout.read()
print out
err=stderr.read()
print err
ssh_client.close()

The same script runs perfectly fine , when its used on server where vshell is not being used

Anyhelp or suggestion on this?

1 Answer 1

1
stdin,stdout,stderr=ssh_client.exec_command(command)

Regarding this line of code, I suspect that the SSH server is not properly configured to allow commands to be executed in this way (this is the equivalent of ssh myserver show hwid, rather than typing it into the terminal after login).

You might want to imitate the behaviour of typing the command in after logging into the server, and for that I think this is appropriate:

shell = ssh_client.invoke_shell()
stdin, stdout, stderr = shell.exec_command(command)
Sign up to request clarification or add additional context in comments.

2 Comments

its raising exeception paramiko.ssh_exception.SSHExceptionn: Channel closed
Although this did not exactly solved my problem, but i used invoke_shell method as suggested by you to solve my problem, i will update my answer

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.