インラインSSHコマンド


15

単純なインラインスタイルのSSHコマンドを実行することは可能ですか、例えば:

ssh foo@bar.com { cd foo/bar && rm *.foobar }

回答:


27

cd foo/bar && rm *.foobarリモートマシンで実行したい場合は、単に

ssh foo@bar.com  'cd foo/bar && rm *.foobar'

そして参照してくださいman ssh...

ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec] [-D
[bind_address:]port] [-e escape_char] [-F configfile]
[-i identity_file] [-L   [bind_address:]port:host:hostport]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port] [-R
[bind_address:]port:host:hostport] [-S ctl_path] [-w tunnel:tunnel
[user@]hostname [command]

必要な部品:

ssh [user@]hostname [command]

2
この方法でもSTDINとSTDOUTをパイプで接続できることに注意してください。先日、sshでDDを使用してリモートマシンのHDイメージを作成しました。
crazy2be

9

スタイル1: ssh user@host 'embedded command'

スタイル2:ssh user@host " $(cat cmd.txt) "
cmd.txtは、ローカルマシンにあるリモートコマンドを含むファイルです


弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.