Python ssh - 脚本终止后保持连接打开 [英] Python ssh - keep connection open after script terminates

查看:63
本文介绍了Python ssh - 脚本终止后保持连接打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本,它将为我 ssh 到一个盒子中.我正在使用 Python 并利用 paramiko 库.我可以在盒子上成功 ssh,但是一旦脚本终止,ssh 连接也会终止.我想在脚本完成运行后保持连接打开.

I'm trying to write a script that will ssh into a box for me. I'm using Python and leveraging the paramiko library. I can successfully ssh on the box, but as soon as the script terminates, the ssh connection also terminates. I want to keep the connection open after the script has completed running.

蟒蛇:

self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.ssh.connect(host, username=self.username, password=self.password)
stdout = execute(self.ssh, 'pwd') # test command for now to verify i'm on box
print stdout
sys.exit()

控制台:

$ ssh.py

[u'/home/myuser\n']

[u'/home/myuser\n']

myuser@xxxx ~

myuser@xxxx ~

$

我没能在网上找到类似的例子,所以任何帮助将不胜感激.

I haven't been able to find similar examples online, so any help would be appreciated.

推荐答案

试试这个:

import subprocess
subprocess.call(["ssh", "myuser@myserver"])

这篇关于Python ssh - 脚本终止后保持连接打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆