plink.but 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. \C{plink} Using the command-line connection tool \i{Plink}
  2. \i{Plink} is a command-line connection tool similar to UNIX \c{ssh}.
  3. It is mostly used for \i{automated operations}, such as making CVS
  4. access a repository on a remote server.
  5. Plink is probably not what you want if you want to run an
  6. \i{interactive session} in a console window.
  7. \H{plink-starting} Starting Plink
  8. Plink is a command line application. This means that you cannot just
  9. double-click on its icon to run it and instead you have to bring up
  10. a \i{console window}. In Windows 95, 98, and ME, this is called an
  11. \q{MS-DOS Prompt}, and in Windows NT, 2000, and XP, it is called a
  12. \q{Command Prompt}. It should be available from the Programs section
  13. of your Start Menu.
  14. In order to use Plink, the file \c{plink.exe} will need either to be
  15. on your \i{\c{PATH}} or in your current directory. To add the
  16. directory containing Plink to your \c{PATH} environment variable,
  17. type into the console window:
  18. \c set PATH=C:\path\to\putty\directory;%PATH%
  19. This will only work for the lifetime of that particular console
  20. window. To set your \c{PATH} more permanently on Windows NT, 2000,
  21. and XP, use the Environment tab of the System Control Panel. On
  22. Windows 95, 98, and ME, you will need to edit your \i\c{AUTOEXEC.BAT}
  23. to include a \c{set} command like the one above.
  24. \H{plink-usage} Using Plink
  25. This section describes the basics of how to use Plink for
  26. interactive logins and for automated processes.
  27. Once you've got a console window to type into, you can type
  28. \c{plink --help} to bring up a usage message. This tells you the
  29. version of Plink you're using, and gives you a brief summary of how to
  30. use Plink:
  31. \c C:\>plink --help
  32. \c Plink: command-line connection utility
  33. \c Release 0.82
  34. \c Usage: plink [options] [user@]host [command]
  35. \c ("host" can also be a PuTTY saved session name)
  36. \c Options:
  37. \c -V print version information and exit
  38. \c -pgpfp print PGP key fingerprints and exit
  39. \c -v show verbose messages
  40. \c -load sessname Load settings from saved session
  41. \c -ssh -telnet -rlogin -raw -serial
  42. \c force use of a particular protocol
  43. \c -ssh-connection
  44. \c force use of the bare ssh-connection protocol
  45. \c -P port connect to specified port
  46. \c -l user connect with specified username
  47. \c -batch disable all interactive prompts
  48. \c -proxycmd command
  49. \c use 'command' as local proxy
  50. \c -sercfg configuration-string (e.g. 19200,8,n,1,X)
  51. \c Specify the serial configuration (serial only)
  52. \c The following options only apply to SSH connections:
  53. \c -pwfile file login with password read from specified file
  54. \c -D [listen-IP:]listen-port
  55. \c Dynamic SOCKS-based port forwarding
  56. \c -L [listen-IP:]listen-port:host:port
  57. \c Forward local port to remote address
  58. \c -R [listen-IP:]listen-port:host:port
  59. \c Forward remote port to local address
  60. \c -X -x enable / disable X11 forwarding
  61. \c -A -a enable / disable agent forwarding
  62. \c -t -T enable / disable pty allocation
  63. \c -1 -2 force use of particular SSH protocol version
  64. \c -4 -6 force use of IPv4 or IPv6
  65. \c -C enable compression
  66. \c -i key private key file for user authentication
  67. \c -noagent disable use of Pageant
  68. \c -agent enable use of Pageant
  69. \c -no-trivial-auth
  70. \c disconnect if SSH authentication succeeds trivially
  71. \c -noshare disable use of connection sharing
  72. \c -share enable use of connection sharing
  73. \c -hostkey keyid
  74. \c manually specify a host key (may be repeated)
  75. \c -sanitise-stderr, -sanitise-stdout, -no-sanitise-stderr, -no-sanitise-stdout
  76. \c do/don't strip control chars from standard output/error
  77. \c -no-antispoof omit anti-spoofing prompt after authentication
  78. \c -m file read remote command(s) from file
  79. \c -s remote command is an SSH subsystem (SSH-2 only)
  80. \c -N don't start a shell/command (SSH-2 only)
  81. \c -nc host:port
  82. \c open tunnel in place of session (SSH-2 only)
  83. \c -sshlog file
  84. \c -sshrawlog file
  85. \c log protocol details to a file
  86. \c -logoverwrite
  87. \c -logappend
  88. \c control what happens when a log file already exists
  89. \c -shareexists
  90. \c test whether a connection-sharing upstream exists
  91. Once this works, you are ready to use Plink.
  92. \S{plink-usage-interactive} Using Plink for interactive logins
  93. To make a simple interactive connection to a remote server, just
  94. type \c{plink} and then the host name:
  95. \c C:\>plink login.example.com
  96. \c
  97. \c Debian GNU/Linux 2.2 flunky.example.com
  98. \c flunky login:
  99. You should then be able to log in as normal and run a session. The
  100. output sent by the server will be written straight to your command
  101. prompt window, which will most likely not interpret terminal \i{control
  102. codes} in the way the server expects it to. So if you run any
  103. full-screen applications, for example, you can expect to see strange
  104. characters appearing in your window. Interactive connections like
  105. this are not the main point of Plink.
  106. In order to connect with a different protocol, you can give the
  107. command line options \c{-ssh}, \c{-ssh-connection}, \c{-telnet},
  108. \c{-rlogin}, or \c{-raw}. To make an SSH connection, for example:
  109. \c C:\>plink -ssh login.example.com
  110. \c login as:
  111. If you have already set up a PuTTY saved session, then instead of
  112. supplying a host name, you can give the saved session name. This
  113. allows you to use public-key authentication, specify a user name,
  114. and use most of the other features of PuTTY:
  115. \c C:\>plink my-ssh-session
  116. \c Sent username "fred"
  117. \c Authenticating with public key "fred@winbox"
  118. \c Last login: Thu Dec 6 19:25:33 2001 from :0.0
  119. \c fred@flunky:~$
  120. (You can also use the \c{-load} command-line option to load a saved
  121. session; see \k{using-cmdline-load}. If you use \c{-load}, the saved
  122. session exists, and it specifies a hostname, you cannot also specify a
  123. \c{host} or \c{user@host} argument - it will be treated as part of the
  124. remote command.)
  125. \S{plink-usage-batch} Using Plink for automated connections
  126. More typically Plink is used with the SSH protocol, to enable you to
  127. talk directly to a program running on the server. To do this you
  128. have to ensure Plink is \e{using} the SSH protocol. You can do this
  129. in several ways:
  130. \b Use the \c{-ssh} option as described in
  131. \k{plink-usage-interactive}.
  132. \b Set up a PuTTY saved session that describes the server you are
  133. connecting to, and that also specifies the protocol as SSH.
  134. \b Set the Windows environment variable \i\c{PLINK_PROTOCOL} to the
  135. word \c{ssh}.
  136. Usually Plink is not invoked directly by a user, but run
  137. automatically by another process. Therefore you typically do not
  138. want Plink to prompt you for a user name or a password.
  139. Next, you are likely to need to avoid the various interactive
  140. prompts Plink can produce. You might be prompted to verify the host
  141. key of the server you're connecting to, to enter a user name, or to
  142. enter a password.
  143. To avoid being prompted for the server host key when using Plink for
  144. an automated connection, you can first make a \e{manual}
  145. connection (using either of PuTTY or Plink) to the same server,
  146. verify the host key (see \k{gs-hostkey} for more information), and
  147. select \q{Accept} to add the host key to the Registry. After that,
  148. Plink commands connecting to that server should not give a host key
  149. prompt unless the host key changes. Alternatively, you can specify
  150. the appropriate host key(s) on Plink's command line every time you
  151. use it; see \k{using-cmdline-hostkey}.
  152. To avoid being prompted for a user name, you can:
  153. \b Use the \c{-l} option to specify a user name on the command line.
  154. For example, \c{plink login.example.com -l fred}.
  155. \b Set up a PuTTY saved session that describes the server you are
  156. connecting to, and that also specifies the username to log in as
  157. (see \k{config-username}).
  158. To avoid being prompted for a password, you should almost certainly
  159. set up \i{public-key authentication}. (See \k{pubkey} for a general
  160. introduction to public-key authentication.) Again, you can do this
  161. in two ways:
  162. \b Set up a PuTTY saved session that describes the server you are
  163. connecting to, and that also specifies a private key file (see
  164. \k{config-ssh-privkey}). For this to work without prompting, your
  165. private key will need to have no passphrase.
  166. \b Store the private key in Pageant. See \k{pageant} for further
  167. information.
  168. Once you have done all this, you should be able to run a remote
  169. command on the SSH server machine and have it execute automatically
  170. with no prompting:
  171. \c C:\>plink login.example.com -l fred echo hello, world
  172. \c hello, world
  173. \c
  174. \c C:\>
  175. Or, if you have set up a saved session with all the connection
  176. details:
  177. \c C:\>plink mysession echo hello, world
  178. \c hello, world
  179. \c
  180. \c C:\>
  181. Then you can set up other programs to run this Plink command and
  182. talk to it as if it were a process on the server machine.
  183. \S{plink-options} Plink command line options
  184. Plink accepts all the general command line options supported by the
  185. PuTTY tools. See \k{using-general-opts} for a description of these
  186. options.
  187. Plink also supports some of its own options. The following sections
  188. describe Plink's specific command-line options.
  189. \S2{plink-option-batch} \I{-batch-plink}\c{-batch}: disable all
  190. interactive prompts
  191. If you use the \c{-batch} option, Plink will never give an
  192. interactive prompt while establishing the connection. If the
  193. server's host key is invalid, for example (see \k{gs-hostkey}), then
  194. the connection will simply be abandoned instead of asking you what
  195. to do next.
  196. This may help Plink's behaviour when it is used in automated
  197. scripts: using \c{-batch}, if something goes wrong at connection
  198. time, the batch job will fail rather than hang.
  199. If another program is invoking Plink on your behalf, then you might
  200. need to arrange that that program passes \c{-batch} to Plink. See
  201. \k{plink-git} for an example involving Git.
  202. \S2{plink-option-s} \I{-s-plink}\c{-s}: remote command is SSH subsystem
  203. If you specify the \c{-s} option, Plink passes the specified command
  204. as the name of an SSH \q{\i{subsystem}} rather than an ordinary command
  205. line.
  206. (This option is only meaningful with the SSH-2 protocol.)
  207. \S2{plink-option-share} \I{-share-plink}\c{-share}:
  208. Test and try to share an existing connection.
  209. This option tries to detect if an existing connection can be shared
  210. (See \k{config-ssh-sharing} for more information about SSH connection
  211. sharing.) and reuses that connection.
  212. A Plink invocation of the form:
  213. \c plink -share <session>
  214. \e iiiiiiiii
  215. will test whether there is currently a viable \q{upstream} for the
  216. session in question, which can be specified using any syntax you'd
  217. normally use with Plink to make an actual connection (a host/port
  218. number, a bare saved session name, \c{-load}, etc). If no \q{upstream}
  219. viable session is found and \c{-share} is specified, this connection
  220. will be become the \q{upstream} connection for subsequent connection
  221. sharing tries.
  222. (This option is only meaningful with the SSH-2 protocol.)
  223. \S2{plink-option-shareexists} \I{-shareexists-plink}\c{-shareexists}:
  224. test for connection-sharing upstream
  225. This option does not make a new connection; instead it allows testing
  226. for the presence of an existing connection that can be shared.
  227. (See \k{config-ssh-sharing} for more information about SSH connection
  228. sharing.)
  229. A Plink invocation of the form:
  230. \c plink -shareexists <session>
  231. \e iiiiiiiii
  232. will test whether there is currently a viable \q{upstream} for the
  233. session in question, which can be specified using any syntax you'd
  234. normally use with Plink to make an actual connection (a host/port
  235. number, a bare saved session name, \c{-load}, etc). It returns a
  236. zero exit status if a usable \q{upstream} exists, nonzero otherwise.
  237. (This option is only meaningful with the SSH-2 protocol.)
  238. \S2{plink-option-sanitise} \I{-sanitise-stderr}\I{-sanitise-stdout}\I{-no-sanitise-stderr}\I{-no-sanitise-stdout}\c{-sanitise-}\e{stream}: control output sanitisation
  239. In some situations, Plink applies a sanitisation pass to the output
  240. received from the server, to strip out control characters such as
  241. backspace and the escape character.
  242. The idea of this is to prevent remote processes from sending confusing
  243. escape sequences through the standard error channel when Plink is
  244. being used as a transport for something like \cw{git} or CVS. If the
  245. server actually wants to send an error message, it will probably be
  246. plain text; if the server abuses that channel to try to write over
  247. unexpected parts of your terminal display, Plink will try to stop it.
  248. By default, this only happens for output channels which are sent to a
  249. Windows console device, or a Unix terminal device. (Any output stream
  250. going somewhere else is likely to be needed by an 8-bit protocol and
  251. must not be tampered with at all.) It also stops happening if you tell
  252. Plink to allocate a remote pseudo-terminal (see \k{using-cmdline-pty}
  253. and \k{config-ssh-pty}), on the basis that in that situation you often
  254. \e{want} escape sequences from the server to go to your terminal.
  255. But in case Plink guesses wrong about whether you want this
  256. sanitisation, you can override it in either direction, using one of
  257. these options:
  258. \dt \c{-sanitise-stderr}
  259. \dd Sanitise server data written to Plink's standard error channel,
  260. regardless of terminals and consoles and remote ptys.
  261. \dt \c{-no-sanitise-stderr}
  262. \dd Do not sanitise server data written to Plink's standard error
  263. channel.
  264. \dt \c{-sanitise-stdout}
  265. \dd Sanitise server data written to Plink's standard output channel.
  266. \dt \c{-no-sanitise-stdout}
  267. \dd Do not sanitise server data written to Plink's standard output
  268. channel.
  269. \S2{plink-option-antispoof} \i{-no-antispoof}: turn off authentication spoofing protection prompt
  270. In SSH, some possible server authentication methods require user input
  271. (for example, password authentication, or entering a private key
  272. passphrase), and others do not (e.g. a private key held in Pageant).
  273. If you use Plink to run an interactive login session, and if Plink
  274. authenticates without needing any user interaction, and if the server
  275. is malicious or compromised, it could try to trick you into giving it
  276. authentication data that should not go to the server (such as your
  277. private key passphrase), by sending what \e{looks} like one of Plink's
  278. local prompts, as if Plink had not already authenticated.
  279. To protect against this, Plink's default policy is to finish the
  280. authentication phase with a final trivial prompt looking like this:
  281. \c Access granted. Press Return to begin session.
  282. so that if you saw anything that looked like an authentication prompt
  283. \e{after} that line, you would know it was not from Plink.
  284. That extra interactive step is inconvenient. So Plink will turn it off
  285. in as many situations as it can:
  286. \b If Plink's standard input is not pointing at a console or terminal
  287. device \dash for example, if you're using Plink as a transport for
  288. some automated application like version control \dash then you
  289. \e{can't} type passphrases into the server anyway. In that situation,
  290. Plink won't try to protect you from the server trying to fool you into
  291. doing so.
  292. \b If Plink is in batch mode (see \k{plink-usage-batch}), then it
  293. \e{never} does any interactive authentication. So anything looking
  294. like an interactive authentication prompt is automatically suspect,
  295. and so Plink omits the anti-spoofing prompt.
  296. But if you still find the protective prompt inconvenient, and you
  297. trust the server not to try a trick like this, you can turn it off
  298. using the \cq{-no-antispoof} option.
  299. \H{plink-batch} Using Plink in \i{batch files} and \i{scripts}
  300. Once you have set up Plink to be able to log in to a remote server
  301. without any interactive prompting (see \k{plink-usage-batch}), you
  302. can use it for lots of scripting and batch purposes. For example, to
  303. start a backup on a remote machine, you might use a command like:
  304. \c plink root@myserver /etc/backups/do-backup.sh
  305. Or perhaps you want to fetch all system log lines relating to a
  306. particular web area:
  307. \c plink mysession grep /~fred/ /var/log/httpd/access.log > fredlog
  308. Any non-interactive command you could usefully run on the server
  309. command line, you can run in a batch file using Plink in this way.
  310. \H{plink-git} Using Plink with \i{Git}
  311. To use Plink for Git operations performed over SSH, you can set the
  312. environment variable \i\c{GIT_SSH_COMMAND} to point to Plink.
  313. For example, if you've run PuTTY's full Windows installer and it has
  314. installed Plink in the default location, you might do this:
  315. \c set GIT_SSH_COMMAND="C:\Program Files\PuTTY\plink.exe"
  316. or if you've put Plink somewhere else then you can do a similar thing
  317. with a different path.
  318. This environment variable accepts a whole command line, not just an
  319. executable file name. So you can add Plink options to the end of it if
  320. you like. For example, if you're using Git in a batch-mode context,
  321. where your Git jobs are running unattended and nobody is available to
  322. answer interactive prompts, you might also append the \cq{-batch}
  323. option (\k{plink-option-batch}):
  324. \c set GIT_SSH_COMMAND="C:\Program Files\PuTTY\plink.exe" -batch
  325. and then if Plink unexpectedly prints a prompt of some kind (for
  326. example, because the SSH server's host key has changed), your batch
  327. job will terminate with an error message, instead of stopping and
  328. waiting for user input that will never arrive.
  329. (However, you don't \e{always} want to do this with Git. If you're
  330. using Git interactively, you might \e{want} Plink to stop for
  331. interactive prompts \dash for example, to let you enter a password for
  332. the SSH server.)
  333. \H{plink-cvs} Using Plink with \i{CVS}
  334. To use Plink with CVS, you need to set the environment variable
  335. \i\c{CVS_RSH} to point to Plink:
  336. \c set CVS_RSH=\path\to\plink.exe
  337. You also need to arrange to be able to connect to a remote host
  338. without any interactive prompts, as described in
  339. \k{plink-usage-batch}.
  340. You should then be able to run CVS as follows:
  341. \c cvs -d :ext:user@sessionname:/path/to/repository co module
  342. If you specified a username in your saved session, you don't even
  343. need to specify the \q{user} part of this, and you can just say:
  344. \c cvs -d :ext:sessionname:/path/to/repository co module
  345. \H{plink-wincvs} Using Plink with \i{WinCVS}
  346. Plink can also be used with WinCVS. Firstly, arrange for Plink to be
  347. able to connect to a remote host non-interactively, as described in
  348. \k{plink-usage-batch}.
  349. Then, in WinCVS, bring up the \q{Preferences} dialogue box from the
  350. \e{Admin} menu, and switch to the \q{Ports} tab. Tick the box there
  351. labelled \q{Check for an alternate \cw{rsh} name} and in the text
  352. entry field to the right enter the full path to \c{plink.exe}.
  353. Select \q{OK} on the \q{Preferences} dialogue box.
  354. Next, select \q{Command Line} from the WinCVS \q{Admin} menu, and type
  355. a CVS command as in \k{plink-cvs}, for example:
  356. \c cvs -d :ext:user@hostname:/path/to/repository co module
  357. or (if you're using a saved session):
  358. \c cvs -d :ext:user@sessionname:/path/to/repository co module
  359. Select the folder you want to check out to with the \q{Change Folder}
  360. button, and click \q{OK} to check out your module. Once you've got
  361. modules checked out, WinCVS will happily invoke plink from the GUI for
  362. CVS operations.
  363. \# \H{plink-whatelse} Using Plink with... ?