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()