Windows和Linux之间的端口映射和端口转发(ncat、nmap)
Tags: ncatnmapOSWSL2端口映射端口转发组网
安装nmap和ncat
1 2 3 | yum install -y nmap nmap-ncat apt install -y nmap ncat |
宿主机直接访问WSL2的Ubuntu系统
系统访问如下所示:
需求:客户端如何访问Ubuntu系统呢??
答案:使用ncat进行端口转发,ncat对于Linux和Windows 通用。
1、在Windows11上安装nmap-7.80-setup.exe
软件(必须),在Ubuntu上安装apt install -y ncat
(可选)
2、在Windows11上运行如下命令即可,表示监听本机的 7777 端口,将数据转发到192.168.8.8的 22 端口,实现 TCP 数据转发。:
1 | ncat --sh-exec "ncat 192.168.8.8 22" -l 7777 --keep-open |
此命令会一直卡住。。。不能关闭窗口
3、在客户端上登陆:
1 | ssh root@192.168.59.131 -p7777 |
在客户端使用SecureCRT连接Ubuntu系统:
后台运行bat脚本
若要后台运行,则必须在powershell里运行才可以:
1 | Start-Process -WindowStyle hidden -FilePath "f:\ncat.bat" |
或者在cmd中运行如下命令:
1 | powershell -c "Start-Process -WindowStyle hidden -FilePath 'f:\ncat.bat'" |
文件f:\ncat.bat的内容如下:
1 | ncat --sh-exec "ncat 192.168.8.8 22" -l 7777 --keep-open |
开机启动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | root@lhrxxt:~# more /etc/rc.local #!/bin/sh ip addr del $(ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | head -n 1) dev eth0 ip addr add 192.168.8.8/24 broadcast 192.168.8.255 dev eth0 ip route add 0.0.0.0/0 via 192.168.8.1 dev eth0 cat > /etc/resolv.conf <<"EOF" nameserver 114.114.114.114 nameserver 8.8.8.8 EOF export WSL_INTEROP=/run/WSL/8_interop export PATH=$PATH:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Users/lhr/AppData/Local/Microsoft/WindowsApps:/mnt/f/platform-tools/ cmd.exe /c "f:\ubuntu_static_ip.bat" 1> nul cmd.exe /c "f:\ncat_start.bat" 1>nul exit 0 |
文件f:\ubuntu_static_ip.bat的内容如下:
1 | powershell -c "Start-Process -WindowStyle hidden -FilePath 'f:\ncat.bat'" |
文件f:\ncat.bat的内容如下:
1 | ncat --sh-exec "ncat 192.168.8.8 22" -l 7777 --keep-open |
参考
https://segmentfault.com/a/1190000042320194
https://www.xmmup.com/sshminglingjiduankouzhuanfa.html
https://www.xmmup.com/windowshelinuxxiadeduankouyingshe.html