当前位置: 动力学知识库 > 问答 > 编程问答 >

Python script not sending linux commands. i used Paramiko for remote SSH connection

问题描述:

import paramiko

import time

import os

ssh=paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect('server',port=22,username='user',password='pass123')

print("connected to the linux machine from windows machine.")

channel=ssh.invoke_shell()

channel_data = str()

while True:

if channel.recv_ready():

channel_data += channel.recv(9999).decode(encoding='utf_8', errors='strict')

os.system('cls')

print("##### Device Output #####")

print("\n",channel_data)

print("\n #####################")

else:

continue

time.sleep(5)

if channel_data.endswith('[[email protected] ~]#'):

#if block statements are not executed why

print("Hi,why not executing this statement")

ssh.send('cd /\n')

#stdin,stdout,stderr=ssh.exec_command('pwd')

#output1=stdout.readlines()

print("My present working directory is")

elif channel_data.endswith('[[email protected] /]#'):

#Also elif block statements are not executed why

ssh.send('mkdir BB444')

#stdin,stdout,stderr=ssh.exec_command('mkdir /pn444')

#output1=stdout.readlines()

print("created pn444 directory")

I am using paramiko for ssh connection. I am able to login to linux machine. then i am checking the condition i.e if channel_data.endswith('[[email protected] ~]#') then send "cd /" command else if channel_data.endswith('[[email protected] /]#') then send 'mkdir BB444' command but this script is not sending these commands. after debugging i see that these sending command statements are not executed. Please let me know what mistake i am making here.

I am using python 3.6, paramiko 2.1.1

网友答案:

The problem might not be the python script. i encountered similar problems while sending remote commands to putty ssh connection from a python script. If you are in a corporate network. some administrators strip the commands from the parameters like -m which i use for putty. so you can login but when you try to send a remote command. it does not reach the server. Check with your administrator if you are on a corporate network about remote commands for ssh connections.

分享给朋友:
您可能感兴趣的文章:
随机阅读: