別のコンピュータのシェルスクリプトを実行する python

これができればとても便利になりますが、もちろんどんなコンピュータでもできるわけではなくて、ssh ログインして実行することになります。

実行するシェルスクリプト

~/import.sh

#!/bin/bash

origin="/home/user/unitea"

for fl in `\find $origin -maxdepth 6 -type f`; do
    a=`file -b $fl`
    if [[ $a = "DICOM medical imaging data" ]];then
        fn=`basename $fl`
        echo $fl
        /home/user/dcm4che-2.0.29/bin/dcmsnd DCM4CHEE@localhost:11112 $fl
    fi
done

python プログラム

別のコンピュータ上の python プログラムです。

remote_shell

import paramiko

class RemoteImport():
    
    def __init__( self ):

        self.hostname = '192.168.56.101'
        self.username = 'user'
        self.password = 'pass'
        self.command = "./import.sh"
        self.client = object

    def sshclient(self):
        self.client = paramiko.SSHClient()
        self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        try:
            self.client.connect(hostname=self.hostname, username=self.username, password=self.password)
        except:
            print("[!] Cannot connect to the SSH Server")
            exit()

    def exec_commant(self):
        print("="*50, self.command, "="*50)
        stdin, stdout, stderr = self.client.exec_command(self.command)
        print(stdout.read().decode())
        err = stderr.read().decode()
        if err:
            print(err)

if __name__ == "__main__":
    rs = RemoteImport()
    rs.sshclient()
    rs.exec_commant()

これができるのであれば、サーバー上の python からクライアントに向かって dicom ファイルを scp put してクライアントに weasis.jnlp を送ってそれを実行することができるはずです。