1

I want to execute multiple commands on a linux machine after ssh connection to it. The first command is a sql command which gives an sql prompt. second commands is an sql query

I tried the below code but it doesnt exit out of session.

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

I have given 2 'exits' to exit out of sql and the linux session respectively. What am I missing here? Is there any better method to achieve this?

Flying falcon
  • 133
  • 2
  • 9

0 Answers0