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

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()
分析解答

正如@Charles的评论所述,

read() consumes content to EOF. There is no EOF here because MySQL is still open. The way 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()