首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在python ssh中获得任何输出。

问无法在python ssh中获得任何输出。
EN

Stack Overflow用户
提问于 2019-08-05 13:36:06
回答 1查看 199关注 0票数 0

我正在尝试连接到远程服务器,然后尝试在该机器中登录sql服务器。但是我没有得到这个脚本的任何输出。

代码语言:javascript
复制
import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy() )
ssh.connect(hostname='172.18.109.244', username='bgf', password='bgf')
print "Logged In to EMS"
cmd = 'mysql -uroot -root'

stdin, stdout, stderr = ssh.exec_command(cmd)
stdin.write("show databases;")
stdin.write('\n')
stdin.flush()
print stdout.read()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-05 13:50:31

正如@Charles在评论中提到的,

read()向EOF消耗内容。这里没有EOF,因为MySQL仍然是开放的。简单的答案是,不仅要冲洗stdin,而且要关闭它。

您不能这样做,因为MySQL仍然处于打开状态,并且您的脚本将挂在那里。因此,如果您只想获取数据库,那么将查询传递给MySQL连接,并运行良好:)。

代码语言:javascript
复制
    import paramiko
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy() )
    ssh.connect(hostname='172.18.109.244', username='somuser',password='bgf')
    print "Logged In to EMS"
    cmd = 'mysql -h somehost.example.com -uroot -proot -e \'show databases;\''

    stdin, stdout, stderr = ssh.exec_command(cmd)
    # stdin.write("show databases;")
    stdin.write('\n')
    stdin.flush()
    print stdout.read()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57359742

复制
相关文章

相似问题

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