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

2024-06-02 05:02:02 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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()

Tags: import服务器脚本cmdparamikosql远程server
1条回答
网友
1楼 · 发布于 2024-06-02 05:02:02

正如@Charles在评论中提到的

read() consumes content to EOF. There is no EOF here because MySQL is still open. The easy answer is to not just flush stdin, but close it.

您不能这样做,因为MySQL仍然处于打开状态,您的脚本将挂在那里。因此,如果您只想获取数据库,那么将查询传递给MySQL连接,就可以正常工作:)。你知道吗

    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()

相关问题 更多 >