それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

putty のマクロでファイル転送とコマンド実行を行う。

putty をマクロで使うにはどうすればいいですかという質問を受けたので書いたモノ。


Puttyをマクロから使うというのもアレですが。VBS/WScriptな人には難しいのかもしれません。


リモートサーバーのコマンドを実行するには

putty -m "ls -alt /etc"

の様に、 -m オプションをつけて対応できます。WscriptのEXECで上記のコマンド文字列を実行します

ファイルを転送するには pscp / psftp を使います。

pscp c:\users\takuya\hoge takuya@192.168.2.5:/home/takuya

と転送出来ます。

psftp は sftp プロトコルで転送するのでマクロ向きではないです。
*1

putty とは

puttySSHクライアントです。

putty(SSH)は通信経路と認証とプログラム実行に分けて考えてみて下さい。

通信経路
ssh
認証
公開鍵やパスワード
プログラム実行
通常は bash(ttyコンソール)

Puttyでリモートサーバーを扱えるのは、SSHで通信して,認証を経由してプログラムを実行しているから。

ssh でプログラムを実行しているから、実行プログラムをbash 以外にすることができる。

ssh コマンドはBashスクリプトなのでputty にをコマンドラインか使う



その他のコマンドラインオプションは次の通りです。

3.8.3.1 -load: load a saved session 
3.8.3.2 Selecting a protocol: -ssh, -telnet, -rlogin, -raw 
3.8.3.3 -v: increase verbosity 
3.8.3.4 -l: specify a login name 
3.8.3.5 -L, -R and -D: set up port forwardings 
3.8.3.6 -m: read a remote command or script from a file 
3.8.3.7 -P: specify a port number 
3.8.3.8 -pw: specify a password 
3.8.3.9 -agent and -noagent: control use of Pageant for authentication 
3.8.3.10 -A and -a: control agent forwarding 
3.8.3.11 -X and -x: control X11 forwarding 
3.8.3.12 -t and -T: control pseudo-terminal allocation 
3.8.3.13 -N: suppress starting a shell or command 
3.8.3.14 -nc: make a remote network connection in place of a remote shell or command 
3.8.3.15 -C: enable compression 
3.8.3.16 -1 and -2: specify an SSH protocol version 
3.8.3.17 -4 and -6: specify an Internet protocol version 
3.8.3.18 -i: specify an SSH private key 
3.8.3.19 -pgpfp: display PGP key fingerprints 


3.8.3.6 -m: read a remote command or script from a file
The -m option performs a similar function to the ‘Remote command’ box in the SSH panel of the PuTTY configuration box (see section 4.18.1). However, the -m option expects to be given a local file name, and it will read a command from that file. 

With some servers (particularly Unix syste

3.8.3.6 -m: read a remote command or script from a file
The -m option performs a similar function to the ‘Remote command’ box in the SSH panel of the PuTTY configuration box (see section 4.18.1). However, the -m option expects to be given a local file name, and it will read a command from that file. 

With some servers (particularly Unix syste

まとめ

  • putty をWSCriptのexec で実行するとPuttyをマクロでファイル実行や、転送が可能になる,
  • putty -m "プログラム名" でサーバー側のプログラムを指定できる。
  • SSHではシェル/bin/bashを必ず起動する必要はなくコマンドを実行して終了とできる。
  • SSHでターミナルが開くのは,/bin/bash実行のおかげ。リモート側で/bin/bash以外を実行すればいい。

他の選択肢

putty 以外のSSHクライアントもある。サーバー側のプログラム起動するなら、putty以外のSSHクライアントで別に何でもイイと思うんだけど。

他の選択肢として、RubySSHCygwinSSH,Windows版のOpenSSHClientでも十分だ。
windowsのsshコマンド
rubyでSSHを使う
cygwinをおすすめする。

ファイル転送するだけなら、WinSCPのコマンドライン実行もオススメです
winscpのコマンドライン実行

*1:もちろん scp と同様の転送もできるけれど、初心者向けに違うものとして扱う