linux shell 远程控制 后台长期运行 shell 脚本
shell 脚本 如何 实现 socket 服务器?
shell 脚本 如何实现 socket client?
import paramiko
import time
def start_arm_tcpdump():
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("10.0.0.2", 22, "root", "dev2021")
# ssh.exec_command("pwd")
# stdin,stdout,stderr = ssh.exec_command("pwd")
# print(stdout.read())
# # ssh.exec_command("tcpdump -i eth1 -l '(host 192.168.99.219) and (not port 22)' -s0 -w -| nc 10.0.0.6 8888 &")
# # print(stdout.read())
# # nohup your_shell.sh > /dev/null 2>&1 &
# ssh.exec_command("nohup /root/do_tcpdump.sh abc > /dev/null 2>&1 &")
#stdin, stdout , stderr = ssh.exec_command("/root/poll_shell/stop_cmd.sh")
# print (stdin, stdout , stderr)
stdin,stdout,stderr = ssh.exec_command("/root/poll_shell/stop_cmd.sh")
result = stdout.read()
if result:
print(result)
ssh.exec_command("nohup /root/poll_shell/start_cmd.sh tcpdump -i eth0 host 192.168.99.219 and port not 22 -l -s0 -w - > /dev/null 2>&1 &")
ssh.close()
def stop_arm_tcpdump():
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("10.0.0.2", 22, "root", "dev2021")
# ssh.exec_command("pwd")
# stdin,stdout,stderr = ssh.exec_command("pwd")
# print(stdout.read())
# # ssh.exec_command("tcpdump -i eth1 -l '(host 192.168.99.219) and (not port 22)' -s0 -w -| nc 10.0.0.6 8888 &")
# # print(stdout.read())
# # nohup your_shell.sh > /dev/null 2>&1 &
# ssh.exec_command("nohup /root/do_tcpdump.sh abc > /dev/null 2>&1 &")
stdin,stdout,stderr = ssh.exec_command("/root/poll_shell/stop_cmd.sh")
result = stdout.read()
if result:
print(result)
#ssh.exec_command("nohup /root/start_cmd.sh tcpdump -i eth0 host 192.168.99.219 and port not 22 -l -s0 -w - > /dev/null 2>&1 &")
ssh.close()
def start_ubuntu_tcpdump():
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("192.168.99.31", 22, "root", "1234")
# ssh.exec_command("pwd")
# stdin,stdout,stderr = ssh.exec_command("pwd")
# print(stdout.read())
# # ssh.exec_command("tcpdump -i eth1 -l '(host 192.168.99.219) and (not port 22)' -s0 -w -| nc 10.0.0.6 8888 &")
# # print(stdout.read())
# # nohup your_shell.sh > /dev/null 2>&1 &
# ssh.exec_command("nohup /root/do_tcpdump.sh abc > /dev/null 2>&1 &")
#stdin, stdout , stderr = ssh.exec_command("/root/poll_shell/stop_cmd.sh")
# print (stdin, stdout , stderr)
stdin,stdout,stderr = ssh.exec_command("/home/jack/poll_shell/stop_cmd.sh")
result = stdout.read()
if result:
print(result)
ssh.exec_command("nohup /home/jack/poll_shell/start_cmd.sh tcpdump -i enp4s0 host 192.168.99.219 and port not 22 -l -s0 -w - > /dev/null 2>&1 &")
ssh.close()
def stop_ubuntu_tcpdump():
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("192.168.99.31", 22, "root", "1234")
# ssh.exec_command("pwd")
# stdin,stdout,stderr = ssh.exec_command("pwd")
# print(stdout.read())
# # ssh.exec_command("tcpdump -i eth1 -l '(host 192.168.99.219) and (not port 22)' -s0 -w -| nc 10.0.0.6 8888 &")
# # print(stdout.read())
# # nohup your_shell.sh > /dev/null 2>&1 &
# ssh.exec_command("nohup /root/do_tcpdump.sh abc > /dev/null 2>&1 &")
stdin,stdout,stderr = ssh.exec_command("/home/jack/poll_shell/stop_cmd.sh")
result = stdout.read()
if result:
print(result)
#ssh.exec_command("nohup /root/start_cmd.sh tcpdump -i eth0 host 192.168.99.219 and port not 22 -l -s0 -w - > /dev/null 2>&1 &")
ssh.close()
if __name__ == '__main__':
# stop_arm_tcpdump()
# start_arm_tcpdump()
start_ubuntu_tcpdump()
#stop_ubuntu_tcpdump()
1
/root/poll_shell/start_cmd.sh
#!/bin/sh
echo $* > /root/poll_shell/123.txt
$* | nc 10.0.0.6 8888 &
2
/root/poll_shell/stop_cmd.sh
#!/bin/sh
check_results=`ps -ef|grep tcpdump|grep -v grep|awk '{print $1}'`
for num in $check_results
do
echo $num
if [ -n $num ]
then
echo $num
kill -9 $num
fi
done
check_nc_results=`ps -ef|grep nc|grep -v grep|awk '{print $1}'`
for num in $check_nc_results
do
echo $num
if [ -n $num ]
then
echo $num
kill -9 $num
fi
done
echo "999" > /root/poll_shell/123.txt
本文介绍了如何使用Linux Shell脚本创建socket服务器和客户端,并探讨了如何确保脚本能在后台长期稳定运行的方法,包括启动和停止脚本的示例。
1001

被折叠的 条评论
为什么被折叠?



