如何使用Python ssh连接执行多个命令?

时间:2016-05-09 12:04:43

标签: python sql-server linux ssh paramiko

我想在ssh连接后在linux机器上执行多个命令。 第一个命令是一个sql命令,它给出了一个sql提示符。 第二个命令是一个SQL查询

我尝试了下面的代码,但它没有退出会话。

import paramiko
nmsIp = "IP"
user="user"
pwd = "pwd"
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(nmsIp, 22, username=user, password=pwd)
channel = ssh.invoke_shell()
stdin = channel.makefile('wb')
stdout = channel.makefile('rb')

stdin.write('''
sqlplus 
select * from *');
exit
exit
''')

print stdout.read()

我已经退出了2'退出'分别退出sql和linux会话。 我在这里错过了什么? 有没有更好的方法来实现这个目标?

0 个答案:

没有答案
相关问题