ubuntu下开机自动链接vpnc并1秒跳动检测

1、安装vpnc客户端
sudo apt-get install vpnc
2、安装expect
sudo apt-get install expect;
3、编写vpnc客户端链接信息
/data/shell/example.conf

IPSec gateway <ipaddress>
IPSec ID <groupuser>
IPSec secret <grouppassword>
#IKE Authmode hybrid
Xauth username <user>
Xauth password <password>

4、编写vpnc链接脚本,实现sudo自动输入密码
/data/shell/example.sh

#!/usr/bin/expect -f
spawn sudo /usr/sbin/vpnc /data/shell/example.conf --local-port 0 --dns-update no
expect {        
        ":" {
                send -- "123456\r"
        }
}
interact

5、编写脚本判断当前是否成功链接vpnc
/data/shell/vpnc.sh

 
#!/bin/bash
t0=`ps aux |grep example.conf | grep -v grep | wc -l`
if [ $t0 -eq 0 ]
then 
	/data/shell/example.sh
fi

6、实现1秒检测守护进程
编写代码
/data/shell/hehe.c

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
void init_daemon(void)
{
int pid;
int i;
if(pid=fork())
{exit(0);}else if(pid< 0)
{exit(1);}
setsid();
if(pid=fork())
{exit(0);}else if(pid< 0)
{exit(1);}
 
for(i=0;i< NOFILE;++i){
close(i);
}
chdir("/tmp");
umask(0);
return;
}
void init_daemon(void);
int main( int argc, char *argv[] ){	
	signal(SIGCHLD, SIG_IGN);
    init_daemon();
    openlog( "hehe",LOG_PID,LOG_USER );
    syslog( LOG_INFO,"%s\n","I love you!");
 
	while(1){
		system("/data/shell/vpnc.sh");
		sleep(1);//1秒跳动
	}
	closelog();
	exit( 1 );
}

编译
gcc -c hehe.c -o hehe.o && gcc hehe.o -o hehe
7、开机启动
sudo vi /etc/rc.locale
加入一行

sudo -u wangliang nohup  /data/shell/hehe &

8、不知道为什么用crontab死活连不上vpnc报错如下

#*/1 *   * * *   root	/data/shell/vpnc.sh > /var/log/vpnc.txt
Sep  4 22:10:01 ubuntu CRON[15457]: (root) CMD (/data/shell/vpnc.sh > /var/log/vpnc.txt)
Sep  4 22:11:01 ubuntu CRON[15568]: (root) CMD (/data/shell/vpnc.sh > /var/log/vpnc.txt)
Sep  4 22:12:01 ubuntu CRON[15592]: (root) CMD (/data/shell/vpnc.sh > /var/log/vpnc.txt)
Sep  4 22:13:01 ubuntu CRON[15616]: (root) CMD (/data/shell/vpnc.sh > /var/log/vpnc.txt)
Sep  4 22:13:56 ubuntu NetworkManager[870]:    SCPlugin-Ifupdown: devices removed (path: /sys/devices/virtual/net/tun0, iface: tun0)
Sep  4 22:14:01 ubuntu CRON[15732]: (root) CMD (/data/shell/vpnc.sh > /var/log/vpnc.txt)
Sep  4 22:14:01 ubuntu NetworkManager[870]:    SCPlugin-Ifupdown: devices added (path: /sys/devices/virtual/net/tun0, iface: tun0)
Sep  4 22:14:01 ubuntu NetworkManager[870]:    SCPlugin-Ifupdown: device added (path: /sys/devices/virtual/net/tun0, iface: tun0): no ifupdown configuration found.
Sep  4 22:14:01 ubuntu NetworkManager[870]:    SCPlugin-Ifupdown: devices removed (path: /sys/devices/virtual/net/tun0, iface: tun0)
Sep  4 22:14:49 ubuntu NetworkManager[870]:    SCPlugin-Ifupdown: devices added (path: /sys/devices/virtual/net/tun0, iface: tun0)
Sep  4 22:14:49 ubuntu NetworkManager[870]:    SCPlugin-Ifupdown: device added (path: /sys/devices/virtual/net/tun0, iface: tun0): no ifupdown configuration found.
spawn sudo /usr/sbin/vpnc /data/shell/of.conf --local-port 0 --dns-update no
RTNETLINK answers: File exists

参考资料
http://hi.baidu.com/pianistsoftwar/item/ba163c98f6525edf7b7f01f0
http://hwcrazy.com/207ef3f070ba11e1ad05000d601c5586/

原创文章,转载请注明: 转载自海波无痕

本文链接地址: ubuntu下开机自动链接vpnc并1秒跳动检测

文章的脚注信息由WordPress的wp-posturl插件自动生成

此条目发表在server分类目录,贴了, , 标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

评论链接可以 移除 nofollow.