2017-02-14 3 views
0
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

+0

通常、プロンプトはスペースで終了します。たぶん 'endswith( '[root @ home /]#')'?または '' [root @ home〜]# ''です。 –

+0

ギリシャありがとうございます。あなたの提案はこの問題を解決しました。 –

+0

こんにちは、私は他の場合に同様の問題に直面しました。コマンドのやりとり中に出力に文字列 'Network nowを設定しますか? (yes/no)[デフォルトyes] '、スクリプトは' yes 'コマンドを送信する必要があります。以下はこれのために書かれたコードです。 channel.send( 'yes \ n')が実行されていません。 elif channel_data.endswith( '今プライムネットワークを設定しますか?(yes/no)[デフォルトyes]'): channel.send( 'yes \ n') –

答えて

0

paramiko問題は、Pythonスクリプトではないかもしれません。 PythonスクリプトからパテSsh接続にリモートコマンドを送信しているときに同様の問題が発生しました。企業ネットワークにいる場合。いくつかの管理者は、私がputtyのために使用する-mのようなパラメータからコマンドを削除します。リモートコマンドを送信しようとするとログインできるようになります。サーバーに到達しません。 ssh接続のリモートコマンドについて企業ネットワーク上にいるかどうかは、管理者に確認してください。

+0

Thanks Sivaprasath。実際には企業ネットワーク上のLinuxサーバのみ。私はあなたの提案について管理者に確認しますが、現在私は "cd /"や "pwd"や "mkdir pn444"のような非常に単純なコマンドを送信しています –

+0

あなたはセキュリティの問題であるリモートコマンドを送信しています。もう1つのことは、CISCOサーバまたはルータを使用していて、ssh接続コマンドからリモートコマンドを取り除くための組み込みセキュリティです。 –

+0

スクリプトからフォルダを作成することができます。スクリプトからコマンドを正常に送信できることを意味します。以下はコードです。 SSH = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect( 'サーバ'、ポート= 22、ユーザ名= 'ユーザー'、パスワード= 'pass123') プリント(paramikoインポート"WindowsマシンからLinuxマシンに接続しました。Okay basu") stdin、stdout、stderr = ssh.exec_command( 'mkdir BB444') output1 = stdout.readlines() print( '新しいディレクトリが作成されました'、output1 ) –

関連する問題