如何通过端口号远程启动和停止服务?

时间:2017-01-07 09:33:15

标签: bash shell command-line scripting pipe

我的文件只包含IP地址和端口号。我知道如何通过名称启动服务,但如何通过端口号启动和停止服务?

for port in $(cat /home/asd/iplog.txt); do
    service /etc/init.d/$port | cut -d\: -f2 start
done

这是iplog.txt输出:

192.168.1.2:21
192.168.2.25:80
192.168.5.230:25

和脚本的输出:

/etc/init.d/192.168.1.2:21: unrecognized service
cut: start: no such file or directory
/etc/init.d/192.168.2.25:80: unrecognized service
cut: start: no such file or directory

现在我有两个问题:

  • 如何通过端口号启动和停止服务?
  • 如何远程办理?

因为我不熟悉shell脚本,所以我真的需要你的帮助。

1 个答案:

答案 0 :(得分:0)

如何通过端口号查找服务的PID /命令?

你可以杀死它们/停止服务。但这不是最好的方式。

#lsof-i :22
Command   PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
sshd    14543 root    3u  IPv4 4132251      0t0  TCP *:ssh (LISTEN)
sshd    14543 root    4u  IPv6 4132253      0t0  TCP *:ssh (LISTEN)
#ps 14543
PID TTY      STAT   TIME COMMAND
14543 ?        Ss     0:00 /usr/sbin/sshd

如何远程操作?

ssh user@remote 'your command to stop service'
相关问题