I need to execute multiple commands(in a specific sequential order) in the same established channel of a switch in a storage network.
But every time I use exec_command(command)
a new channel is opened and the command is sent to the switch.
Since the commands have to be sequentially executed, nothing is going through.
So, my doubt is, how to send multiple sequential commands through a single ssh channel, implemented using paramiko
library.
To execute multiple command in single channel of ssh. you need to invoke interactive shell by
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname, port, username, password)
channel = ssh.invoke_shell()
Then send command to terminal by
channel.send(command)
and receive command output by
channel.recv(9999)
print the output by
channel.recv(9999).decode()
If your goal is to send multiple commands in the same host and receive the outcome of those commands, I've written a script that works well (in this case to ping several elements):
# -*- coding: utf-8 -*-
"""
Created on Thu May 5 10:56:10 2016
@author: Me
"""
import paramiko as pk
class cii(Exception):
pass
num = int(input('number : '))
while True :
try:
if len(str(num)) != 5 :
raise cii
except cii:
print ('error message')
break
else:
with open (r'path.txt') as search :
try:
for line in search :
line = line.rstrip()
line1 = line.split(",")
IP = line1[2]
line2 = line1[2].split('.')
line3 = (int(line2[3])+1)
del line2[3]
line2.append(str(line3))
CA = str(".".join(line2))
Machine = line1[1]
Command = 'ping -n 1'
if str(num) in line and yy == 'xx' :
ssh = pk.SSHClient()
ssh.set_missing_host_key_policy(
pk.AutoAddPolicy())
ssh.connect('{}'.format(IP), port=xxxx, username='xxxxxx',
password='xxxx')
stdin, stdout, stderr = \
ssh.exec_command('ping -n 1 xxx.xxx.xxx.xxx\n')
print('Ping xx: \n', stdout.readlines())
ssh = pk.SSHClient()
ssh.set_missing_host_key_policy(
pk.AutoAddPolicy())
ssh.connect('{}'.format(IP), port=xxxx, username='xxxxxx',
password='xxxx')
stdin, stdout, stderr = \
ssh.exec_command('ping -n 1 xxx.xxx.xxx.xxx\n')
print('Ping xx: \n', stdout.readlines())
ssh = pk.SSHClient()
ssh.set_missing_host_key_policy(
pk.AutoAddPolicy())
ssh.connect('{}'.format(IP), port=xxxx, username='xxxxxx',
password='xxxx')
stdin, stdout, stderr = \
ssh.exec_command('ping -n 1 xxx.xxx.xxx.xxx\n')
print('Ping xx: \n', stdout.readlines())
ssh = pk.SSHClient()
ssh.set_missing_host_key_policy(
pk.AutoAddPolicy())
ssh.connect('{}'.format(xx), port=xxxx, username='xxxxxxx',
password='xxxxxxx')
stdin, stdout, stderr = \
ssh.exec_command('{} {}'.format(Command, CA).encode('ascii') + b"\n")
print('Ping CA: \n', stdout.readlines())
break
except TimeoutError:
print ('error message')
break
input ('\n keep windows open and then Enter to exit \n')
You can send multiple commands by opening a new connection each time. Hope it helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.