脚本语言目前发展很快,python流行度也越来越大,对我而言,脚本语言主要作为系统的粘合剂,一般不做主体业务逻辑的开发,但作为系统管理,整合程序还是不错的。所以,今天开始学习python,有点懒,因为有个小需求:linux下SSH远程登录后,发送短信通知管理员。原来一般使用shell直接完成,今天学习python,就作为第一个hello world试试。花了尽2个小时,完全根据程序逻辑需求通过google查语法(怎么定义程序,if,字符串处理等,哈哈),终于完成。记录下咱的first python程序哈。
#!/usr/bin/python import time import base64 import urllib import httplib import os import sys mobileNos=138xxxxxxxx def sendSms(mobileNo,content): url = "/api/......" queryString=urllib.urlencode({"mobileNo": mobileNo, "content": content, "username":"aaaaa", "password":"xxxxxx"}) conn = httplib.HTTPConnection("192.168.xxx,xxx") conn.request("GET",url+"?"+queryString) response = conn.getresponse() conn.close() def logger(log): f=open('lm.log','a+') print >> f, log f.close() def loginNotify(): client = os.getenv("SSH_CLIENT") if client is None: return clientInfo = client.split(' ') ip = clientInfo[0] now = time.strftime('%Y-%m-%d %H:%M:%S') user = os.getenv("USER") hostname = os.getenv("HOSTNAME") log = now + " - " + user + " login " + hostname + " from " + ip logger(log) sendSms(mobileNos,log) if __name__ == "__main__": loginNotify()