Created
January 24, 2021 19:56
-
-
Save fukasawah/f16342490e70e176f401c81c642cffe0 to your computer and use it in GitHub Desktop.
paramikoのexit_status_readyの検証
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 標準出力とエラー出力をそれぞれ10MiB行うコマンド | |
#command = '''bash -c 'dd if=/dev/urandom bs=512 count=20480 >&1 & dd if=/dev/urandom bs=512 count=20480 >&2; wait' ''' | |
command = '''bash -c 'cat test.dat >&1 & cat test.dat >&2; wait' ''' | |
import logging | |
logging.basicConfig() | |
logging.getLogger("paramiko").setLevel(logging.DEBUG) | |
import paramiko | |
client = paramiko.SSHClient() | |
client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy()) # 未知の公開鍵の場合は無視 | |
client.connect(hostname="127.0.0.1", port=22, username="test", password="test") # 接続の確立 | |
channel = client.get_transport().open_session(timeout=3) # コマンド実行 | |
try: | |
channel.exec_command(command) | |
RECV_SIZE = 1024 * 32 | |
stdout_data = b'' | |
stderr_data = b'' | |
while not channel.exit_status_ready() or channel.recv_ready() or channel.recv_stderr_ready(): | |
stdout_data += channel.recv(RECV_SIZE) | |
channel.recv_stderr(RECV_SIZE) | |
# 終了コードを受け取る | |
code = channel.recv_exit_status() | |
# 色々処理 | |
finally: | |
channel.close() | |
print(f"stdout_data = {len(stdout_data)} byte (expected 10485760 => {len(stdout_data) == 10485760})") | |
print(f"stderr_data = {len(stderr_data)} byte") | |
if len(stdout_data) == 10485760: | |
exit(0) | |
else: | |
exit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while python test.py ; do date; done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
失敗時の実行結果(抜粋)