Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1416038
  • 博文数量: 264
  • 博客积分: 5810
  • 博客等级: 大校
  • 技术积分: 3528
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-13 17:15
文章分类

全部博文(264)

文章存档

2011年(264)

分类: Python/Ruby

2011-08-02 22:05:40

  1. #!/usr/bin/env python
  2. import socket
  3. import sys
  4. import re
  5. def check_serer(address,port):
  6.     #create a TCP socket
  7.     s = socket.socket()
  8.     try:
  9.         s.connect((address,port))
  10.         return True
  11.     except socket.error,e:
  12.         print str(e)
  13.         return False

  14. if __name__ == '__main__':
  15.     from optparse import OptionParser
  16.     parser = OptionParser()
  17.     parser.add_option("-a","--address",dest="address",default="localhost",help="Address for server",metavar="ADDRESS")
  18.     parser.add_option("-p","--port",type="int",default=80,help="Port for server",metavar="port")
  19.     (options,args) = parser.parse_args()
  20.     check = check_serer(options.address, options.port)
  21.     sys.exit(not check)


片段2. 
import socket,sys,re
import httplib
def check_webserver(address,port,resource):
    if not resource.startswith('/'):
        resource  = '/' + resource
    request_string = "GET %s HTTP/1.1\r\nHost:%s\r\n\r\n" % (resource,address)
    #create a tcp socket
    s = socket.socket()
    try:
        s.connect((address,port))
        s.send(request_string) 
        rsp = s.recv(1024)
    except socket.error,e:
        return False
    finally:
        s.close()
    lines = rsp.splitlines()
    print 'first line of HTTP response:%s'%lines[0]
    try:
        version,status,message = re.split(r'\s+', lines[0], 2)
        print 'version:%s,Status:%s.Message:%s'%(version,status,message)
    except ValueError:
        return False
    if status in ['200','301']:
        print 'sucess'
        return True
    else:
        print 'down'
        return False

def check_webserver_http(address,port,resource):
    if not resource.startswith('/'):
        resource  = '/' + resource
    try:
        conn = httplib.HTTPConnection(address,port)
        req = conn.request('GET',resource)
        response = conn.getresponse()
        print 'response status:%s' % response.stats
    except socket.error,e:
        return False
    finally:
        conn.close()
    if response.stats in [200,301]:
        return True
    else:
        return False
    
if __name__ == '__main__':
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-a","--address",dest="address",default="localhost",help="Address for server",metavar="ADDRESS")
    parser.add_option("-p","--port",type="int",default=80,help="Port for server",metavar="port")
    parser.add_option("-r","--resource",default="index.html",help="resource to check",metavar="RESOURCE")
    
    (options,args) = parser.parse_args()
    check = check_webserver(options.address, options.port,options.resource)
    sys.exit(not check)

片段3. FTP相关的
from ftplib import FTP
import ftplib
import sys
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-a", "--remote_host_address", dest="remote_host_address",
help="REMOTE FTP HOST.",
metavar="REMOTE FTP HOST")

parser.add_option("-r", "--remote_file", dest="remote_file",
help="REMOTE FILE NAME to download.",
metavar="REMOTE FILE NAME")

parser.add_option("-l", "--local_file", dest="local_file",
help="LOCAL FILE NAME to save remote file to", metavar="LOCAL FILE NAME")

parser.add_option("-u", "--username", dest="username",
help="USERNAME for ftp server", metavar="USERNAME")

parser.add_option("-p", "--password", dest="password",
help="PASSWORD for ftp server", metavar="PASSWORD")

(options, args) = parser.parse_args()

if not (options.remote_file and options.local_file and options.remote_host_address):
    parser.error('REMOTE HOST, LOCAL FILE NAME, and REMOTE FILE NAME are mandatory')

if options.username and not options.password:
    parser.error('password is mandatory')

ftp  = FTP(options.remote_host_address)
if options.username:
    try:
        ftp.login(options.username,options.password)
    except ftplib.error_perm,e:
        sys.exit(1)

else:
    try:
        ftp.login()
    except ftplib.error_perm,e:
        sys.exit(1)

try:
    local_file = open(options.local_file, 'wb')
    ftp.retrbinary('RETR %s' % options.remote_file, local_file.write) #retrieves a binary file from an FTP server.
finally:
    local_file.close()
    ftp.close()

片段4.
import paramiko
hostname = '192.168.1.15'
port = 22
username = 'jmjones'
pkey_file = '/home/jmjones/.ssh/id_rsa'
if __name__ == "__main__":
    key = paramiko.RSAKey.from_private_key_file(pkey_file)
    s = paramiko.SSHClient()
    s.load_system_host_keys()
    s.connect(hostname, port, pkey=key)
    stdin, stdout, stderr = s.exec_command('ifconfig')
    print stdout.read()
    s.close()

阅读(1259) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~