Skype Robot in Ubuntu
skype linux 机器人
1.安装所需的软件包
apt-get install python-software-properties sudo add-apt-repository "deb http://archive.canonical.com/$(lsb_release -sc) partner" sudo apt-get update sudo apt-get install gcc sudo apt-get install skype sudo apt-get install xvfb sudo apt-get install fluxbox x11vnc sudo apt-get install dbus sudo apt-get install vnc4server
http://www.qxs.ch/2011/01/07/skype-instant-messages-from-zabbix/
wget http://sourceforge.net/projects/skype4py/files/skype4py/1.0.32.0/Skype4Py-1.0.32.0.tar.gz tar -xzf Skype4Py-1.* cd Skype4Py-1.0.32.0/ python setup.py install vi ./build/lib.linux-i686-2.7/Skype4py/api/posix_dbus.py (64位: vi ./build/lib.linux-x86_64-2.7/Skype4Py/api/posix_dbus.py) replace os.execlp('skype') with os.execlp('skype', 'skype') python setup.py install
2.create file skype_chat.py
import os import sys import optparse import Skype4Py parser = optparse.OptionParser(version='%prog 1.0.0'); parser.add_option('--create-channel', help='create a new channel'); parser.add_option('--user', help='skype username'); parser.add_option('--disband-channel', help='disband a channel'); parser.add_option('--sendto-channel', help='select a channel'); parser.add_option('--message', help='send msg to channel'); parser.add_option('--from-channel', help='from channel'); parser.add_option('--kick', help='kicks member(s) from chat'); opts, args = parser.parse_args(); if args: parser.error('unexpected argument(s)'); skype = Skype4Py.Skype(Transport='x11'); channel_file = 'skype_channel'; def SkypeInit(): if not skype.Client.IsRunning: skype.Client.Start(); skype.Attach(); def GetChatName(channel): if not os.path.isfile(channel_file): print 'can not find channel file skype_channel, please create new channel.'; exit(); f = open(channel_file,'r'); lines = f.readlines(); chat_name = None; for line in lines: channel_list = line.split(); channel_name = channel_list[0]; if channel_name == channel: chat_name = channel_list[1]; break; f.close(); return chat_name; if opts.create_channel != None and opts.user != None: channel = opts.create_channel; user = opts.user; SkypeInit(); chat = skype.CreateChatWith(user); f = open(channel_file,'a+'); lines = f.readlines(); for line in lines: cname = line.split(); cname = cname[0]; if cname == channel: f.close(); print 'channel ', cname , ' has existed!'; exit(); f.write(channel + ' ' + chat.Name + '\n'); f.close(); print 'channel ', channel, ' created successfull.'; elif opts.sendto_channel != None and opts.message != None: channel = opts.sendto_channel; msg = opts.message; chat_name = GetChatName(channel); if chat_name != None: SkypeInit(); chat = skype.Chat(chat_name); chat.SendMessage(msg); else: print 'channel ', channel_name, ' not find!'; exit(); elif opts.kick != None and opts.from_channel != None: channel = opts.from_channel; chat_name = GetChatName(channel); if chat_name != None: SkypeInit(); chat = skype.Chat(chat_name); chat.Kick(opts.kick); else: print 'channel ', channel_name, ' not find!';
3.server socket:
listen port 47777
vi skype_socket.c
skype_socket.c
|
---|
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <netinet/in.h>
#define MAXLINE 1024 #define SERV_PORT 47777
int substr(char* dchr,char *schr,int begin,int len){ int slen=0; int rc=0; if (begin<=0) begin=1; slen=strlen(schr)-begin;
if (slen<=0||len==0){ *dchr=NULL; return rc; }
if (len<0){ len=-len; if(len>strlen(schr)) begin=1; else if((begin-len)<=0){ len=begin; begin=1; } else { begin-=len; begin++; } }
begin–; schr+=begin; int i=0;
for(i=0;i<len&&*schr!=0;i++){ *dchr++ = *schr++; }
*dchr=0; rc=i;
return rc; }
int main(int argc,char *argv[]){ pid_t pid; struct sockaddr_in servaddr,cliaddr; socklen_t cliaddr_len; int listenfd,connfd; char buf[MAXLINE]; char str[INET_ADDRSTRLEN]; int i,n,m; char delim[10] = “;;”; //default delim int max_size = 800; //default max msg size
if(argc == 2 ){ strcpy(delim, argv[1]); } if(argc == 3){ max_size = (int)argv[2]; }
listenfd = socket(AF_INET,SOCK_STREAM,0); if( listenfd < 0){ printf(“Create Socket Failed!”); exit(1); }
bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT);
if(bind(listenfd,(struct sockaddr *)(&servaddr),sizeof(servaddr)) == -1){ perror(“bind Error”); exit(1); }
if(listen(listenfd,20) < 0){ perror(“listen Error”); exit(1); }
while(1){ cliaddr_len = sizeof(struct sockaddr_in); connfd = accept(listenfd,(struct sockaddr *)(&cliaddr),&cliaddr_len); if(connfd < 0){ perror(“accept Error”); exit(1); }
m = fork();
if(m == -1){ perror(“call to fork error”); exit(1); }else if(m == 0){ //sub process while(1){ n = read(connfd,buf,MAXLINE); if(n < 0){ perror(“read Error”); exit(1); } if(n == 0){ //printf(“the other side has been closed.\n”); break; }
char *p; char channel[20], msg[max_size]; memset(channel, ‘\0′, sizeof(channel)); p = strstr(buf, delim); substr(channel, buf, 0, strlen(buf)-strlen(p)); substr(msg, p, 3, max_size); //printf(“%s\n”, msg);
/* if(!strcasecmp(channel, “ls”)){ system(“ls -lh /data/binary/ “); } */
char cmdfile[10] = “./”; char filename[] = “send.sh”; strcat(cmdfile, filename); char *arg[]={filename, channel, msg, NULL}; if( access(filename, F_OK) != 0 ) { //printf(“file %s not find.\n”, filename); exit(1); }
//execute shell script if(execvp(cmdfile, arg) < 0){ //printf(“execvp error”); exit(1); } }
close(connfd); exit(0); }else{ close(connfd); } } } |
create “send.sh” :
#!/bin/bash channel=$1 msg=$2 python skype_chat.py --sendto-channel="$channel" --message="(*) $msg"
4. compile skype_socket.c:
gcc -o skype_socket skype_socket.c
5.start socket and test send message by VNC:
on server that skype installed run vncserver:
vncserver
enter password: skype
on your client computer run vnc clien and connect to server, you will open a window and a shell window, then run command:
skype &
then login skype with ROBOT account, and then start socket:
./skype_socket >/dev/null 2>&1 &
run “netstat -tlnp|grep skype”, you can find listen port 47777
create chat channel:
python skype_chat.py --create-channel="AAA" --user="your_skype_account"
6.on any server ,send message to chat chennel:
echo "AAA;;Hello everyone."|nc Server 47777
定一个 skype机器人!!!