我正在尝试连接到远程服务器,然后尝试在该计算机上登录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()
答案 0 :(得分:1)
如@Charles的评论中所述,
read()将内容消耗到EOF。这里没有EOF,因为MySQL是 还开着。简单的答案是不仅刷新标准输入,而且将其关闭。
您不能这样做,因为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()