跳至主要内容

ubuntu下利用expect实现screen多窗口开机运行

ubuntu下利用expect实现screen多窗口开机运行

1. expect的安装与使用

1.1. expect的安装与使用

是什么

expect 是用来进行自动化控制和测试的工具。主要是和交互式软件telnet ftp ssh 等进行自动化的交互。

如何安装

1.2. 检测是否安装

ls /usr/bin |grep expect

如果不存在,则进行安装

1.3.安装

apt-get install tcl tk expect

ls /usr/bin |grep expect
autoexpect
expect
expect_autoexpect
expect_autopasswd
expect_cryptdir
expect_decryptdir
expect_dislocate
expect_ftp-rfc
expect_kibitz
expect_lpunlock
expect_mkpasswd
expect_multixterm
expect_passmass
expect_rftp
expect_rlogin-cwd
expect_timed-read
expect_timed-run
expect_tknewsbiff
expect_tkpasswd
expect_unbuffer
expect_weather
expect_xkibitz
expect_xpstat

wiki对于expect说明https://ift.tt/2MugVKV

2. 安装screen

apt-get install screen

screen用法这里不做过多的介绍,请大家google搜索

3. 利用expect实现screen多窗口运行

3.1. 建立start.sh开机运行

#!/usr/bin/env bash
screen_name1=$"baidu"
screen -dmS $screen_name1
cmd1=$"ping www.baidu.com";
screen -x -S $screen_name1 -p 0 -X stuff "$cmd1" #创建screen
screen -x -S $screen_name1 -p 0 -X stuff $'\n' #回车
# 嵌套expect
# send "\01" 模拟输入 ctrl-a 
# send "d" 模拟输入d  ctrl-a+d是挂起screen
/usr/bin/expect <<EOF
send "\01"
send "d"
expect eof
EOF

screen_name2=$"google"
screen -dmS $screen_name2
cmd2=$"ping www.baidu.com";
screen -x -S $screen_name2 -p 0 -X stuff "$cmd2"
screen -x -S $screen_name2 -p 0 -X stuff $'\n'
/usr/bin/expect <<EOF
send "\01"
send "d"
expect eof
EOF

测试脚本

sh start.sh

查看screen窗口

screen -ls

#输出文字,字样就成功启动
There is a screen on:
    168.baidu   (06/08/19 07:14:04) (Detached)
    158.google  (06/08/19 07:14:04) (Detached)
1 Socket in /run/screen/S-root.

3.2 创建stop.sh文件

#!/usr/bin/env bash
screen_name1=$"baidu"
screen -X -S $screen_name1 quit #退出screen

screen_name2=$"google"
screen -X -S $screen_name2 quit

3.3 crontab定时执行这两个脚本

crontab  -e

0 10 * * * /root/start.sh #每天早晨10点
30 23 * * * /root/stop.sh #每天晚上23.30

3.4 开机运行

设置开机启用,仅限ubuntu18版本

#首先检查rc-local是不是启动了,如果启动最后完全还要restart,这里的坑,sandy踩了半个小时
systemctl status rc-local.service

vim /lib/systemd/system/rc-local.service
#最下加入
[Install]
WantedBy=multi-user.target
Alias=rc-local.service

vim /etc/rc.local
#加入下面代码
#!/bin/bash
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
bash /root/start.sh
exit 0

#给予权限
chmod +x /etc/rc.local
#开启服务
systemctl enable rc-local
#启动服务并检查状态
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service

#重新启动服务器
reboot

#检测服务器是不是已经启动
sudo systemctl status rc-local.service
#输出文字,字样就成功启动
Active: active (running) since Fri 2019-06-07 03:25:59 UTC; 19s ago
screen -ls
#输出文字,字样就成功启动
There is a screen on:
    168.baidu   (06/08/19 07:14:04) (Detached)
    158.google  (06/08/19 07:14:04) (Detached)
1 Socket in /run/screen/S-root.

评论

此博客中的热门博文

阿里云通过Nginx中转Linode/digitalocean/AWS的V2ray协议,实现负载均衡

开篇文的废话.原因是Sandy做站服务器太多了,发现利用率不高,皮又痒了又想折腾了.v2ray是利器但是给人的感觉就是只能用一台服务器,官方也说不支持负载均衡,不会A服务器挂机,自动跳转到B服务器.这里就有点折磨人了.每个服务器挂了都要自己去改配置文件. 我们访问国外的服务器,如果没CN2的线路是非常慢的,起码不快!哈哈~现在VPS都便宜起码人手一个人有5台以上吧?这样的话我们就可以开整. 开整条件:国内VPS或者阿里云有CN2线路的机器最好是100MB-200MB的带宽的.保证你爽歪歪.但是Sandy在阿里云HK30MB下也测试了也可以达到30000KB以上,羡慕吧~我们可以开始了! 老规则工具先出: 没被gwf和谐的服务器1台(下文统称:主服务器) 国外服务器2台以上(下文统称:从服务器) nginx 1.9.5版本以上(推荐:lnmp1.4) v2ray croe 第一步: 在 主服务器 安装lnmp1.4,这里就不细说教程了.直接去 官方 看教程 安装完毕以后我们来升级Nginx让他支持stream模块 cd lnmp1.4 vi lnmp.conf 在Nginx_Modules_Options=‘’里面加入--with-stream Nginx_Modules_Options='--with-stream’ 然后执行升级命令 ./upgrade.sh nginx  第二步 修改nginx.conf文件 cd /usr/local/nginx/conf/nginx.conf 在 events { use epoll; worker_connections 51200; multi_accept on; } 下面加入 stream{ server{ listen 0.0.0.0:8080; proxy_pass v2ray_backend; proxy_timeout 10m; proxy_connect_timeout 5000ms; } upstream v2ray_backend{ server ...

利用haproxy来做V2RAY负载均衡

Sandy在之前有说过 阿里云通过Nginx中转Linode/digitalocean/AWS的V2ray协议,实现负载均衡 ,有些学童不想折腾nginx,那么我们可以用haproxy来折腾,不管是haproxy还是Nginx都可以在树莓派和局域网的PC机器折腾! 安装haproxy的教程就移步到-> haproxy安装教程 apt-get install haproxy 安装完毕,我们来修改haproxy的配置 vi /etc/haproxy/haproxy.cfg 里面内容全部删除,加入下面的内容 global ulimit-n 51200 log /dev/log local0 log /dev/log local1 notice chroot /var/lib/haproxy pidfile /var/run/haproxy.pid user haproxy group haproxy daemon defaults log global mode tcp retries 3 option dontlognull option redispatch #上游TCP服务器连接等待时间 timeout connect 5s timeout client 24h timeout server 24h #以下为Web管理页面设置,如不需要可以删除 listen admin_stats bind 0.0.0.0:8888 mode http log 127.0.0.1 local0 err stats refresh 30s stats uri /haproxy stats realm welcome login\ Haproxy stats auth admin:admin stats hide-version stat...

ssh over socks5:通过socks5 proxy来连接ssh服务器[转载]

最近因为不可描述的原因,我在aws soul的云主机访问不了,ssh、80、ss全部都被禁掉了。 80端口在chrome配置SwitchyOmega就可以了,但ssh不太好办,shell上配置http_proxy对ssh没什么用。 其实ssh自己就支持proxy。 注意这跟在ssh端口转发:远程和本地中介绍过使用ssh作为proxy的方法不同,ssh -D是为了用ssh做proxy,而不是通过proxy来连接ssh服务器。 TL;DR ssh -o ProxyCommand='nc -x 192.0.2.0:1080 %h %p' user@awshost 原理解析 $ man ssh_config ProxyCommand Specifies the command to use to connect to the server. The command string extends to the end of the line, and is executed using the user's shell ‘exec’ directive to avoid a lingering shell process. In the command string, any occurrence of ‘%h’ will be substituted by the host name to connect, ‘%p’ by the port, and ‘%r’ by the remote user name. The command can be basically anything, and should read from its standard input and write to its standard output. It should eventually connect an sshd(8) server running on some machine, or execute sshd -i somewhere. Host key management will be done using the HostName of the host being connected (defa...