问题描述
我对Linux和Paramiko还是很陌生,但是我遇到的问题是,无论何时我尝试更改远程Paramiko会话将挂起的shell.
I am rather new to Linux and Paramiko, but the issue I am having is anytime I attempt to change a shell the remote Paramiko session will hang.
默认情况下,远程主机位于/etc/csh
中我正在运行各种脚本,有些需要csh
,有些需要bash
.我的任何在csh
中运行的脚本都可以正常运行,因为默认情况下远程主机位于csh
中.
The remote host is in /etc/csh
by defaultI am running various scripts, some require csh
and others require bash
. Any of my scripts running in csh
work properly since the remote host is in csh
by default.
要运行其他脚本,我需要在bash
中.每当我尝试使用bash
或/bin/bash
更改shell时,paramiko连接都将挂起.我正在使用以下命令在连接之前以及尝试临时更改外壳以查看有效的方法之后验证外壳,但没有任何效果.这是使用 Paramiko 和 Python 3.6.5 .注意:相反,这也失败了;如果我默认将远程主机放在bash
中,它将无法切换到csh
To run the other scripts I need to be in bash
.Whenever I attempt to change the shell using bash
or /bin/bash
paramiko connection will simply hang. I am using the following command to verify shells prior to connection and after attempting to temporarily change the shell to see what works, but nothing has. This is using Paramiko and Python 3.6.5.Note: this also fails the other way around; if I put the remote host in bash
by default it will fail to switch to csh
main.py
connection = SSH.SSH(hostname, username, password)
connection.changeShell('echo $0 ; echo $shell; /bin/bash ; echo $shell ; echo $0')
也已尝试将其用作bash
和chsh
SSH.py
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 changeShell(self, command):
print("Sending your command")
# Check in 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(2048)
while stdout.channel.recv_ready():
# Retrieve the next 1024 bytes
alldata += stdout.channel.recv(2048)
# Print as string with utf8 encoding
print(str(alldata, "utf8"))
stdin.close()
stdout.close()
stderr.close()
else:
print("Connection not opened.")
推荐答案
您的问题与Paramiko无关.尝试将命令粘贴到SSH终端中-也不起作用.
语法aaa ; bbb
依次执行命令 .在aaa
完成之前,将不执行bbb
.同样,/bin/bash ; echo $shell
执行bash
,并且echo
直到bash
完成才执行,因为它永远不会执行,因此会挂起.
The syntax aaa ; bbb
executes the commands one after another. bbb
won't be executed until aaa
finishes. Similarly, /bin/bash ; echo $shell
executes bash
and the echo
won't be executed until bash
finishes, what it never does, hence the hang.
您实际上不想在之后执行echo
bash
-您想在 bash
之内执行echo
.
You actually do not want to execute echo
after bash
- you want to execute echo
within bash
.
如果要在其他外壳中执行脚本/命令,则有三个选项:
If you want to execute a script/commands within a different shell, you have three options:
-
使用 shebang 在脚本本身中指定脚本所需的外壳程序-这是脚本的正确方法.
Specify the shell that the script needs in the script itself using shebang - This the the right way for scripts.
#!/bin/bash
使用shell命令行执行脚本/命令:
Execute the script/commands using shell command-line:
/bin/bash script.sh
或
/bin/bash -c "command1 ; command2 ; ..."
将要执行的脚本/命令写到shell输入中,就像我在上一个问题中向您展示的那样:
Write the script/command to be executed to a shell input, like I've shown you in your previous question:
使用Python Paramiko通过SSH将输入/变量传递给bash脚本
这篇关于在SSH/Paramiko中使用不同的Shell执行命令/脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!