在 SSH/Paramiko 中使用不同的 shell 执行 command/script
Execute command/script using different shell in SSH/Paramiko
我对 Linux 和 Paramiko 比较陌生,但我遇到的问题是任何时候我尝试更改 shell 远程 Paramiko 会话都会挂起。
远程主机默认在/etc/csh
我正在 运行 编写各种脚本,有些需要 csh
,有些需要 bash
。我在 csh
中的任何脚本 运行ning 都可以正常工作,因为默认情况下远程主机在 csh
中。
到运行我需要在bash
中的其他脚本。
每当我尝试使用 bash
或 /bin/bash
更改 shell 时,paramiko 连接都会挂起。我正在使用以下命令在连接之前和尝试临时更改 shell 之后验证 shells 以查看有效的方法,但没有任何效果。这是使用 Paramiko 和 Python 3.6.5。
注意:反之亦然;如果我默认将远程主机放在 bash
中,它将无法切换到 csh
main.py
connection = SSH.SSH(hostname, username, password)
connection.changeShell('echo [=13=] ; echo $shell; /bin/bash ; echo $shell ; echo [=13=]')
这也被试过 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
完成后才会执行,它从未执行过,因此挂起。
您实际上不想在 bash
之后执行echo
- 您想要在echo
bash
.
如果你想在不同的shell中执行script/commands,你有三个选择:
使用 shebang 在脚本本身中指定脚本需要的 shell - 这是脚本的正确方法。
#!/bin/bash
使用shell命令行执行script/commands:
/bin/bash script.sh
或
/bin/bash -c "command1 ; command2 ; ..."
将要执行的 script/command 写入 shell 输入,就像我在上一个问题中向您展示的那样:
我对 Linux 和 Paramiko 比较陌生,但我遇到的问题是任何时候我尝试更改 shell 远程 Paramiko 会话都会挂起。
远程主机默认在/etc/csh
我正在 运行 编写各种脚本,有些需要 csh
,有些需要 bash
。我在 csh
中的任何脚本 运行ning 都可以正常工作,因为默认情况下远程主机在 csh
中。
到运行我需要在bash
中的其他脚本。
每当我尝试使用 bash
或 /bin/bash
更改 shell 时,paramiko 连接都会挂起。我正在使用以下命令在连接之前和尝试临时更改 shell 之后验证 shells 以查看有效的方法,但没有任何效果。这是使用 Paramiko 和 Python 3.6.5。
注意:反之亦然;如果我默认将远程主机放在 bash
中,它将无法切换到 csh
main.py
connection = SSH.SSH(hostname, username, password)
connection.changeShell('echo [=13=] ; echo $shell; /bin/bash ; echo $shell ; echo [=13=]')
这也被试过 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
完成后才会执行,它从未执行过,因此挂起。
您实际上不想在 bash
之后执行echo
- 您想要在echo
bash
.
如果你想在不同的shell中执行script/commands,你有三个选择:
使用 shebang 在脚本本身中指定脚本需要的 shell - 这是脚本的正确方法。
#!/bin/bash
使用shell命令行执行script/commands:
/bin/bash script.sh
或
/bin/bash -c "command1 ; command2 ; ..."
将要执行的 script/command 写入 shell 输入,就像我在上一个问题中向您展示的那样: