Scripting over ssh through putty on windows

2019-02-14 12:01发布

问题:

I need to script a mysql restore of a linux machine from a windows machine. I have putty and I know it has ssh capability. Here is what I have:

C:\Progra~1\Putty\putty.exe -ssh root@10.1.2.3 && mysql -u USER -pPASS db < /tmp/dump.sql

My problem in writing a script is that putty opens a new window and transfers control there, so anything coming after

putty.exe -ssh 

doesn't do anything.

回答1:

You can use the -m option to specify a file which contains a command to run on the remote connection.

Example:

C:\Progra~1\Putty\putty.exe -ssh -m C:\cmd.txt root@10.1.2.3

Contents of C:\cmd.txt

 mysql -u USER -pPASS db < /tmp/dump.sql

From the Putty Documentation:

3.8.3.6 `-m': read a remote command or script from a file

The -m' option performs a similar function to theRemote 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 systems), you can even put multiple lines in this file and execute more than one command in sequence, or a whole shell script; but this is arguably an abuse, and cannot be expected to work on all servers. In particular, it is known not to work with certain `embedded' servers, such as Cisco routers.

This option is not available in the file transfer tools PSCP and PSFTP.



标签: mysql ssh putty