首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用python转到远程服务器(网络操作系统)中的bash模式

使用python转到远程服务器(网络操作系统)中的bash模式
EN

Stack Overflow用户
提问于 2019-03-12 22:29:07
回答 1查看 102关注 0票数 0

我想使用paramiko自动获取当前内核版本。我尝试使用python.I连接到服务器(网络操作系统),然后尝试执行以下命令

代码语言:javascript
复制
OS10# system bash
admin@OS10:~$ uname -r
3.16.51

下面是我的python脚本

代码语言:javascript
复制
import sys
import time
import select
import paramiko

host = '100.97.100.12'
i = 1


while True:
    print "Trying to connect to %s (%i/30)" % (host, i)

    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())     
        ssh.connect(hostname=host,port=22,username="admin",password="admin",
          timeout=float(10),allow_agent=False ,look_for_keys=False)
        print "Connected to %s" % host
        break
    except paramiko.AuthenticationException:
        print "Authentication failed when connecting to %s" % host
        sys.exit(1)
    except:
        print "Could not SSH to %s, waiting for it to start" % host
        i += 1
        time.sleep(2)

    # If we could not connect within time limit
    if i == 30:
        print "Could not connect to %s. Giving up" % host

stdin, stdout, stderr = ssh.exec_command("system bash")

ssh_output = stdout.read()
ssh_error = stderr.read()
print(ssh_output)
print(ssh_error)
ssh.close()

我可以连接到主机,但它没有给任何error没有output时,执行该命令。

EN

回答 1

Stack Overflow用户

发布于 2019-03-13 18:19:51

尝试执行以下命令:

/path/to/bash -c 'uname -r'

在那里,您可以通过SSHing连接到您的服务器并查看cat /etc/shellswhich bash上的path/to/bash路径来确定bash。或者,您可以使用chsh永久更改默认shell。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55123931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档