【发布时间】:2018-12-04 05:58:07
【问题描述】:
我对 Linux 和 Paramiko 还很陌生,但我遇到的问题是,每当我尝试更改 shell 时,远程 Paramiko 会话就会挂起。
远程主机默认在/etc/csh
我正在运行各种脚本,有些需要csh,有些需要bash。我在csh 中运行的任何脚本都可以正常工作,因为默认情况下远程主机在csh 中。
要运行其他脚本,我需要在bash 中。
每当我尝试使用bash 或/bin/bash 更改外壳时,paramiko 连接都会挂起。我正在使用以下命令在连接之前验证 shell,并在尝试临时更改 shell 以查看什么有效,但什么都没有。这是使用 Paramiko 和 Python 3.6.5。
注意:反之亦然;如果我默认将远程主机放在bash,它将无法切换到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.")
【问题讨论】:
标签: python bash shell ssh paramiko