一、引言:如何远程控制
也许你会有这样的场景,交互程序(以下简称UI程序)跑在windows
端,而控制程序跑在Linux
上。我们想要通过windows端 UI程序
来启动Linux
下面的服务,来一场酣畅淋漓的御剑飞行咋办,难道要自己十年磨一剑,在Linux
下编写一个受控服务程序么.计算机科技发展到现在,咱们早就已经站在巨人的肩膀上了。而且这样的想法前人已有,解决方案早已帮我们做好。
现在,我要给你介绍的就是plink工具。
plink 是Putty工具集的子程序之一,而他强大之处,正是为了解决我们上面提到的应用场景之一。
二、Putty - plink 下载/安装
- putty下载地址 https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
- putty支持多平台,按自己的实际情况下载安装
- 安装完成,你将看到plink工具,以笔者为例
三、心欲无痕之御剑有形
那前面说了这么多plink,这玩意究竟怎么远程控制么,必须要有心法,对,心法。
我们先贴这儿:
Plink: command-line connection utility
Release 0.81
Usage: plink [options] [user@]host [command]
("host" can also be a PuTTY saved session name)
Options:
-V print version information and exit
-pgpfp print PGP key fingerprints and exit
-v show verbose messages
-load sessname Load settings from saved session
-ssh -telnet -rlogin -raw -serial
force use of a particular protocol
-ssh-connection
force use of the bare ssh-connection protocol
-P port connect to specified port
-l user connect with specified username
-batch disable all interactive prompts
-proxycmd command
use 'command' as local proxy
-sercfg configuration-string (e.g. 19200,8,n,1,X)
Specify the serial configuration (serial only)
The following options only apply to SSH connections:
-pwfile file login with password read from specified file
-D [listen-IP:]listen-port
Dynamic SOCKS-based port forwarding
-L [listen-IP:]listen-port:host:port
Forward local port to remote address
-R [listen-IP:]listen-port:host:port
Forward remote port to local address
-X -x enable / disable X11 forwarding
-A -a enable / disable agent forwarding
-t -T enable / disable pty allocation
-1 -2 force use of particular SSH protocol version
-4 -6 force use of IPv4 or IPv6
-C enable compression
-i key private key file for user authentication
-noagent disable use of Pageant
-agent enable use of Pageant
-no-trivial-auth
disconnect if SSH authentication succeeds trivially
-noshare disable use of connection sharing
-share enable use of connection sharing
-hostkey keyid
manually specify a host key (may be repeated)
-sanitise-stderr, -sanitise-stdout, -no-sanitise-stderr, -no-sanitise-stdout
do/don't strip control chars from standard output/error
-no-antispoof omit anti-spoofing prompt after authentication
-m file read remote command(s) from file
-s remote command is an SSH subsystem (SSH-2 only)
-N don't start a shell/command (SSH-2 only)
-nc host:port
open tunnel in place of session (SSH-2 only)
-sshlog file
-sshrawlog file
log protocol details to a file
-logoverwrite
-logappend
control what happens when a log file already exists
-shareexists
test whether a connection-sharing upstream exists
这么长,这么多参数,我靠,这神功看来不好练。
别急,《葵花宝典》第一篇都说了,“欲练此功,必先自宫”,使用此神器,自然也有其先决条件。
plink的使用先决条件:
- 1、被控的操作系统支持 -ssh -telnet -rlogin -raw -serial 这几种连接方式
- 2、以ssh为例,你得知道远控端的ip、账号和密码。远控端就像一个装满飞剑的剑匣子的藏剑格。若大的江湖,你得知道他放在哪个门派的藏剑阁中(ip),他在藏剑格的哪个剑匣(账号)里,瞎子的锁怎么开(密码)
- 3、知道以上的一切,那就好办了,plink提供的正是你尚不具备的超强神念,让你的意念直接抵达你要的剑阁,并附在你欲 御之剑上。
四、飞一次给你看
@echo off
set username=your_username
set password=your_password
set ip_address=server_ip
plink -ssh %username%@%ip_address% -pw %password% "/tmp/app"
譬如我们在上面编写了一个bat脚本,脚本中用于启动远程的程序(神剑),这把剑放在远程端(剑阁)的 /tmp
目录下,应用名(剑名)app
( 读者应该适自己的实际情况来写名称)
但是这里有个比较操蛋的地方,我要启动飞剑,你还得让我再次确认一下。就像打算进去了,你弱弱的问一句“你确认要进来么”,影响心情。
但是,这怎么可能难倒我们,稍做优化:
@echo off
set username=your_username
set password=your_password
set ip_address=server_ip
plink -batch -ssh %username%@%ip_address% -pw %password% "/tmp/app"
这下子舒服了,进退自如,收放自如。这才是神兵利器该有的丝滑体验啊。
五、练器为骨,收发自如
这个时候会有朋友要问,这还得装putty的工具,就像施法的时候还得打个魔法杖(我没有讽刺哈利波特的意思,不过这肯定是不帅的)。要更帅,当然可以,putty是开源的,路径:
https://code.google.com/p/puttycn
你稍微花点时间进行炼化,以自己为容器(windows端UI应用集成putty的plink功能),这不就省事了么~~
笔者注:
愿各位早日成神,早日飞升。遁出红尘~~~