Networking

Sending email

import sy.net

sy.net.sendmail(to=['unix@foo.com', 'me@foo.com'],
                   subject='Script output',
                   message='The script did ...',
                   attachments=['/var/log/script.log'])
sy.net.sendmail(to, subject, message, sender=None, attachments=(), mailhost='localhost', mailport=25, timeout=60.0)

Send an email

Parameters:
  • to – List of recipients
  • subject – Subject of the message
  • message – Message body
  • sender – From address of the sender default to <current user>@<hostname>
  • attachments – List if paths to files you want to attach
  • mailhost – Mailserver to send through, default localhost
  • mailport – Mailserver port to send through, default 25
  • timeout – Timeout for sending the mail

Downloading files

import sy.net

# Downloads file from http server
sy.net.download('http://server/file.tgz', '/var/tmp/myfile.tgz')

# Downloads file from ftp server
sy.net.download('ftp://server/file.tgz', '/var/tmp/myfile.tgz')

Warning

Proxy settings from the environment will be used. To be sure that no proxy is configured remove the variable before download:

import os
del os.environ['http_proxy']
del os.environ['ftp_proxy']

See the standard library urllib2 module for more advanced scenarios.

sy.net.download(url, target)

Download a file through http or ftp

IP tools - sy.net.ip

synopsis:Utilities for working with ip addressing and hosts
sy.net.ip.add_hostentry(ipaddress, hostname, append_hostname=False)

Add a hostname to /etc/hosts

Parameters:
  • ipaddress – IP of the host
  • hostname – Hostname (and aliases)
  • append_hostname – Append the hostnames if an entry for the IP already exists
sy.net.ip.add_netmask(network, netmask)

Add a netmask to /etc/inet/netmask

Overwrites any old netmask with the same network IP

sy.net.ip.can_ping(host)

Test if host or ipaddress is pingable

sy.net.ip.get_network(ipaddress, netmask)

Returns the network address given an ipaddress and netmask

sy.net.ip.is_valid(ipaddress)

Checks if a IP address is valid

sy.net.ip.port_is_open(ipaddress, port, timeout=1)

Check if a port is reachable and open

Parameters:
  • ipaddress – IP address to the host
  • port – Port number
  • timeout – Number of seconds to wait for a connection
Returns:

True if the port is open, False otherwise

Interfaces - sy.net.intf

Warning

These are only (slightly) tested and only works on Solaris 10

Table Of Contents

Previous topic

Utilities & Logging

This Page