1

I am working on a Python 3.7 script to connect and execute commands on the remote Linux server.

I have used paramiko and ssh2.session libraries, the script was able to connect to the remote server but command is not executed.

Remote server details

cat /etc/issue

**Welcome to SUSE Linux Enterprise Server 12 SP2  (x86_64) - Kernel \r (\l).**

Windows python version

C:\Users\Desktop>python --version

**Python 3.7.4**

I have gone through the link python paramiko ssh and used similar python script

Please check the below code using paramiko library

import paramiko

hostname='x.x.x.x' #ip not mentioned for privacy reasons
port=4422
username='ts_r'
password='a'


ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=hostname,port=port,username=username,password=password)
stdin, stdout, stderr = ssh.exec_command("ls -lrt")
print(stdout.readlines())
print(stderr.readlines())
ssh.close()


script output:
--------------
C:\Users\Desktop>python test.py
[]
[]

using ssh2.session

I have gone through the link https://pypi.org/project/ssh2-python/ and used similar python script

Please check the below code using ssh2.session library

from __future__ import print_function

import socket

from ssh2.session import Session

host = 'x.x.x.x' #ip not mentioned for privacy reasons
user = 'ts_r'
port = 4422
password = 'a'
cmd="ls -lrt"
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))

session = Session()
session.handshake(sock)
session.userauth_password(user, password)

channel = session.open_session()
channel.execute(cmd)
size, data = channel.read()
while size > 0:
    print(data)
    size, data = channel.read()
channel.close()
print("Exit status: %s" % channel.get_exit_status())


script output:
--------------
C:\Users\Desktop>python test3.py
Exit status: 1

I'm using port 4422 instead of port 22 as it is used for other application on the remote linux server.

Could someone explain what could be the reason for not executing the command on the remote server and how to solve the problem.

The problem here is the script neither giving output of the executed command nor throwing error.

I have tried different commands such as "pwd", "uname -a" none of them are executed.

I have found the below description about exec_command from the paramiko document

exec_command(*args, **kwds)

Execute a command on the server. If the server allows it, the channel will then be directly connected to the stdin, stdout, and stderr of the command being executed.

I wonder if server allowed the connection but did not allow to execute the command how to check that.

rohit
  • 65
  • 10
  • 1
    What is the output? Could `ls` have executed correctly but the working directory was empty? Have you tried another command such as `whoami` or `cwd`? – steviestickman Jan 06 '20 at 07:48
  • Directory is not empty, I have tried various commands including (whoami, cwd) none of them are executed, I also have some application commands installed on our remote server, when they are executed corresponding log file will be updated. I ran the script with those commands and nothing is updated in the log file. – rohit Jan 06 '20 at 09:03
  • Have you tried to manually ssh in to port 4422? – steviestickman Jan 06 '20 at 13:17
  • Yes, I have manually connected from another linux server and executed the commands.It works – rohit Jan 06 '20 at 13:25
  • I have connected to the remote linux server and executed commands through windows putty by keeping connection type as ssh. It works – rohit Jan 06 '20 at 13:32

0 Answers0