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")
私はssh接続のためにparamikoを使用しています。私はLinuxマシンにログインすることができます。 channel_data.endswith( '[root @ home /]#')を送信し、次に 'mkdir BB444'を送信すると、 "cd /"コマンドを送信します。 'コマンドを使用していますが、このスクリプトはこれらのコマンドを送信していません。デバッグ後、これらの送信コマンド文は実行されないことがわかります。私がここで何をしているのか教えてください。私のpython 3.6を使用していますPythonスクリプトはLinuxコマンドを送信しません。私はリモートSSH接続のためにParamikoを使用しました
は、2.1.1
通常、プロンプトはスペースで終了します。たぶん 'endswith( '[root @ home /]#')'?または '' [root @ home〜]# ''です。 –
ギリシャありがとうございます。あなたの提案はこの問題を解決しました。 –
こんにちは、私は他の場合に同様の問題に直面しました。コマンドのやりとり中に出力に文字列 'Network nowを設定しますか? (yes/no)[デフォルトyes] '、スクリプトは' yes 'コマンドを送信する必要があります。以下はこれのために書かれたコードです。 channel.send( 'yes \ n')が実行されていません。 elif channel_data.endswith( '今プライムネットワークを設定しますか?(yes/no)[デフォルトyes]'): channel.send( 'yes \ n') –