如何在 fabric 2.4 中指定远程 shell
How to specify remote shell in fabric 2.4
我需要通过 ssh 连接到来自 python 的远程主机。我选择了 fabric 2.4,因为它可以在单个 ssh 会话中 运行 多个命令。但我需要使用不同于 sh/bash/etc 的遥控器 shell,我的 shell 由 clixon 提供支持。
我发现的所有示例都描述了在结构 1.X 中更改 shell。
如何在 fabric 2.4 中配置它?
或者您可以为 python 建议另一个 ssh 库,它可以 运行 在单个 ssh 会话中执行多个命令?
P.S。我无法更改 /etc/passwd
.
中用户的默认值 shell
我找到了 paramiko 的解决方法(使用频道):
class ParamikoWraper:
def __init__(self, host, user, password, port=22):
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.client.connect(host, username=user, password=password, port=port)
self.channel = self.client.invoke_shell()
self.stdin = self.channel.makefile('wb')
self.stdout = self.channel.makefile('r')
def __del__(self):
self.client.close()
def run(self, command):
command = command.strip('\n')
self.stdin.write(command + '\n')
self.stdin.flush()
time.sleep(1)
return command, ""
remote = ParamikoWraper('192.168.33.10', "vagrant", "qwerty")
remote.run("sudo -s") # start shell
for cmd in ["echo $(whoami) >> /root/test", "echo 2 >> /root/test", "echo 3 >> /root/test"]:
print(remote.run(cmd))
我需要通过 ssh 连接到来自 python 的远程主机。我选择了 fabric 2.4,因为它可以在单个 ssh 会话中 运行 多个命令。但我需要使用不同于 sh/bash/etc 的遥控器 shell,我的 shell 由 clixon 提供支持。
我发现的所有示例都描述了在结构 1.X 中更改 shell。
如何在 fabric 2.4 中配置它?
或者您可以为 python 建议另一个 ssh 库,它可以 运行 在单个 ssh 会话中执行多个命令?
P.S。我无法更改 /etc/passwd
.
我找到了 paramiko 的解决方法(使用频道):
class ParamikoWraper:
def __init__(self, host, user, password, port=22):
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.client.connect(host, username=user, password=password, port=port)
self.channel = self.client.invoke_shell()
self.stdin = self.channel.makefile('wb')
self.stdout = self.channel.makefile('r')
def __del__(self):
self.client.close()
def run(self, command):
command = command.strip('\n')
self.stdin.write(command + '\n')
self.stdin.flush()
time.sleep(1)
return command, ""
remote = ParamikoWraper('192.168.33.10', "vagrant", "qwerty")
remote.run("sudo -s") # start shell
for cmd in ["echo $(whoami) >> /root/test", "echo 2 >> /root/test", "echo 3 >> /root/test"]:
print(remote.run(cmd))