Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exec in bashrc to start zsh, paramiko exec_command cannot return result #2071

Open
xiaoxinbioinfo opened this issue Jun 21, 2022 · 3 comments
Labels

Comments

@xiaoxinbioinfo
Copy link

Hello guys,

I ran into some problems using Paramiko to SSH to my server A:

My server is Centos7. I edit my .bashrc file on server A exec /data/install/bin/zsh to start ZSH, because I do not have root permission.

When I tried to connect to server A locally using Paramiko and execute commands, it seemed to work, but executing commands using exec_command didn't get the correct results.

When I comment the ZSH related statements in my .bashrc with "#", Paramiko exec_command returns normal result.

A code example is as follows:

import paramiko

pkey='/home/nich/.ssh/id_rsa'
key=paramiko.RSAKey.from_private_key_file(pkey)

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='192.168.1.12',   port=22,    username='nich',    pkey=key)
stdin, stdout, stderr = ssh.exec_command('ls /data/nich/')
print(stdout.read())

When I have exec /data/install/bin/ ZSH in my.bashrc file, this Python code freezes and keeps running with no return unless I use CTRL +C to force it to end. Error info as follow:

File "", line 1, in
File "/data/nich/software/miniconda/lib/python3.9/site-packages/paramiko/file.py", line 200, in read
new_data = self._read(self._DEFAULT_BUFSIZE)
File "/data/nich/software/miniconda/lib/python3.9/site-packages/paramiko/channel.py", line 1361, in _read
return self.channel.recv(size)
File "/data/nich/software/miniconda/lib/python3.9/site-packages/paramiko/channel.py", line 699, in recv
out = self.in_buffer.read(nbytes, self.timeout)
File "/data/nich/software/miniconda/lib/python3.9/site-packages/paramiko/buffered_pipe.py", line 160, in read
self._cv.wait(timeout)
File "/data/nich/software/miniconda/lib/python3.9/threading.py", line 312, in wait
waiter.acquire()
KeyboardInterrupt

When I comment the ZSH related info with "#", the program returns a normal result:

b'2021.9.3.file\ntestfile.txt\n'

My question is :
My server A still wants to keep the ZSH-related configuration because I'm used to working with ZSH (can not work without zsh).
When I connect to server A using Paramiko, how do I execute commands and return results?

Thank you in advance for your help!

@bskinn bskinn added the Support label Jun 21, 2022
@hujiawei34
Copy link

i had same issue,but could use invoke_shell to get command output

@xiaoxinbioinfo
Copy link
Author

i had same issue,but could use invoke_shell to get command output

I had solved it by moving exec zsh from .bashrc to .bash_profile.

The reason seems to related to which file was load when ssh to a server. I can't explain it well.
You can search the difference between bashrc and bash_profile.

@hujiawei34
Copy link

i had same issue,but could use invoke_shell to get command output

I had solved it by moving exec zsh from .bashrc to .bash_profile.

The reason seems to related to which file was load when ssh to a server. I can't explain it well. You can search the difference between bashrc and bash_profile.

In fact, the problem is that the paramiko is used to log in to the IBMC of some servers. After the exec_cmd command is executed, the result cannot be obtained. In this case, i need to manually run the send and recv method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants