def hostname(): hostname = socket.getfqdn() form = HostSettingsForm() if not form.is_submitted(): form.hostname.data = hostname if form.validate_on_submit(): new_hostname = form.hostname.data if os.access(HOSTS_FILE, os.W_OK): _sethostname(HOSTS_FILE, hostname, new_hostname) else: flash('Unable to write HOSTS FILE, check permissions', 'error') if os.access(HOSTNAME_FILE, os.W_OK): _sethostname(HOSTNAME_FILE, hostname, new_hostname) else: flash('Unable to write HOSTNAME FILE, check permissions', 'error') with sh.sudo: try: sh.hostname("-b", new_hostname) except sh.ErrorReturnCode_1: flash('Error setting hostname with the hostname command.', 'error') if hasservice: try: sh.service("avahi-daemon", "restart") except sh.ErrorReturnCode_1: flash('Error restarting the avahi-daemon', 'error') return redirect(url_for('settings.host')) return render_template('settings/hostname.html', hostname=hostname, form=form, active="hostname")
def connectToCamera(cameraNumber): sh.wpa_cli("-i", "wlan0", "select_network", cameraNumber) print(sh.wpa_cli("-i", "wlan0", "list_networks")) time.sleep(2) while sh.hostname('-I') == '\n': print('Waiting for connection...') print(sh.hostname('-I')) time.sleep(1) print(sh.hostname('-I'))
def extract_config(): def git_sha(base=''): try: return str(sh.git('rev-parse', 'HEAD', _cwd=base)).strip() except Exception: return 'NA' config = c = {} versions = v = {} v['bloscpack'] = bp.__version__ v['numpy'] = np.__version__ v['joblib'] = jb.__version__ v['conda'] = str(sh.conda('--version', _tty_in=True)).strip() v['python'] = str(sh.python('--version', _tty_in=True)).strip() hashes = h = {} h['bloscpack'] = git_sha(os.path.dirname(bp.__file__)) h['joblib'] = git_sha(jb.__path__[0]) h['numpy'] = git_sha(np.__path__[0]) h['benchmark'] = git_sha() c['uname'] = str(sh.uname('-a')).strip() c['hostname'] = str(sh.hostname()).strip() c['whoami'] = str(sh.whoami()).strip() c['date'] = str(sh.date()).strip() c['versions'] = versions c['hashes'] = hashes return config
def get_info(wide=False): try: ip = get_ip_address("eth0") except IOError: try: ip = get_ip_address("wlan0") except IOError: try: ip = get_ip_address("lo") except IOError: ip = "0.0.0.0" f = {"host": sh.hostname().strip(), "ip": ip, # sh.hostname("-i").strip(), "version": "0.0.1", } if os.path.exists(MSP430SERIAL): f.update({"msp430": {"port": MSP430SERIAL}}) if os.path.exists(VIDEO_DEVICE): f.update({"video": {"port": VIDEO_DEVICE}}) if wide: try: f.update({"ifconfig": sh.Command("/sbin/ifconfig")(), "route": sh.Command("/sbin/route")(), }) except: pass return f
def extract_config(): def git_sha(base=''): try: return str(sh.git('rev-parse', 'HEAD', _cwd=base)).strip() except Exception: return 'NA' config = c = {} versions = v = {} v['bloscpack'] = bp.__version__ v['blosc'] = blosc.__version__ v['numpy'] = np.__version__ v['joblib'] = jb.__version__ v['tables'] = tables.__version__ v['conda'] = str(sh.conda('--version', _tty_in=True)).strip() v['python'] = str(sh.python('--version', _tty_in=True)).strip() hashes = h = {} h['bloscpack'] = git_sha(os.path.dirname(bp.__file__)) h['joblib'] = git_sha(jb.__path__[0]) h['blosc'] = git_sha(blosc.__path__[0]) h['numpy'] = git_sha(np.__path__[0]) h['tables'] = git_sha(tables.__path__[0]) h['benchmark'] = git_sha() c['uname'] = str(sh.uname('-a')).strip() c['hostname'] = str(sh.hostname()).strip() c['whoami'] = str(sh.whoami()).strip() c['date'] = str(sh.date()).strip() c['versions'] = versions c['hashes'] = hashes return config
def __init__(self, datadir=None, currdir=None, archive=None, writeInfoFile=True, suffix=''): if (datadir is None): datadir = os.path.join(os.getcwd(), 'data') if (currdir is None): currdir = os.path.join(datadir, DEFAULT_CURRDIR) if (archive is None): archive = os.path.join(datadir, 'archive') self.DATADIR = datadir self.CURRDIR = currdir self.ARCHIVE = archive self.writeInfoFile = writeInfoFile self.suffix = suffix self.info = OrderedDict() self.info['init'] = getTs() hostname = 'unknown' try: hostname = str(sh.hostname()).strip() except Exception as e: pass self.info['hostname'] = hostname error_message = 'No paths should end in slash' assert not '/' in map( lambda x: x[-1], [self.ARCHIVE, self.DATADIR, self.CURRDIR]), error_message return
def make_ip_window(): H, W = screen.getmaxyx() ip_win = screen.subwin(H / 2, W, H / 2 - HINT_WIDTH, 0) ip_win.box() try: ip = sh.head(sh.awk(sh.getent("ahosts", sh.hostname().strip()), "{print $1}"), n="1").strip() if ip == "127.0.0.1": raise DHCPMisconfiguration() except sh.ErrorReturnCode: ip_win.addstr(1, 2, "===================================") ip_win.addstr(2, 2, "Connectivity issues detected!") ip_win.addstr(3, 2, "===================================") ip_win.addstr(4, 2, "Check VM setup instructions") ip_win.addstr(6, 2, "For details, see VM setup instructions") except DHCPMisconfiguration: ip_win.addstr(1, 2, "===================================") ip_win.addstr(2, 2, "Connectivity issues detected!") ip_win.addstr(3, 2, "===================================") ip_win.addstr(4, 2, "Check connection of Host-only interface") ip_win.addstr(5, 2, "and check DHCP is enabled for it") ip_win.addstr(7, 2, "For details, see VM setup instructions") else: ip_win.addstr(1, 2, "To initiate your Hortonworks Sandbox session,") ip_win.addstr(2, 2, "please open a browser and enter this address ") ip_win.addstr(3, 2, "in the browser's address field: ") ip_win.addstr(4, 2, "http://%s/" % ip)
def make_ip_window(): H, W = screen.getmaxyx() ip_win = screen.subwin(H / 2, W, H / 2 - HINT_WIDTH, 0) ip_win.box() try: ip = sh.head( sh.awk( sh.getent("ahosts", sh.hostname().strip()), "{print $1}"), n="1").strip() if ip == "127.0.0.1": raise DHCPMisconfiguration() except sh.ErrorReturnCode: ip_win.addstr(1,2,"===================================") ip_win.addstr(2,2,"Connectivity issues detected!") ip_win.addstr(3,2,"===================================") ip_win.addstr(4,2,"Check VM setup instructions") ip_win.addstr(6,2,"For details, see VM setup instructions") except DHCPMisconfiguration: ip_win.addstr(1,2,"===================================") ip_win.addstr(2,2,"Connectivity issues detected!") ip_win.addstr(3,2,"===================================") ip_win.addstr(4,2,"Check connection of Host-only interface") ip_win.addstr(5,2,"and check DHCP is enabled for it") ip_win.addstr(7,2,"For details, see VM setup instructions") else: ip_win.addstr(1,2,"To initiate your Hortonworks Sandbox session,") ip_win.addstr(2,2,"please open a browser and enter this address ") ip_win.addstr(3,2,"in the browser's address field: ") ip_win.addstr(4,2,"http://%s/" % ip)
def main(argv, settings): logging.basicConfig(filename = settings.LOG_FILE, level = logging.DEBUG, format='[%(asctime)s][%(levelname)s] %(message)s') first = True for line in sh.tail('-f', settings.WATCH_LOG, _iter=True): line = line.strip() if not first: match = re.search(settings.MATCH_LINES, line) logging.debug('Looking for "%s" in "%s" -> %r.', settings.MATCH_LINES, line, bool(match)) if match: message = '[{stamp}][{host}] {match}'.format(stamp = getDateTime(), host = sh.hostname('-s').strip(), match = match.group(0)) sendDm(settings.TWITTER_AUTH, settings.DM_RECIPIENTS, message) first = False
def set_hostname(self, hostname): """Update hostname Args: hostname (str): hostname to be updated """ try: old_hostname = self.get_hostname() is_valid_hostname(hostname) sh.hostname("-b", hostname) sh.echo(hostname, _out="/etc/hostname") try: # sed -i 's/ old$/ new/g' /etc/hosts sh.sed("-i", "s/ {}$/ {}/g".format(old_hostname, hostname), "/etc/hosts") except: with open("/etc/hosts", "a") as f: f.write("127.0.0.1 localhost {}\n".format(hostname)) self.update(id=1, newObj={"hostname": hostname}) except Exception as e: raise e
def send_email_update(old_ip, new_ip): hostname = str(sh.hostname('-s')).strip() date_time = get_date() HOST = "{}:{}".format(sender_server, int(sender_port)) TO = "To: {}".format(receiver_address) FROM = "From: {}".format(sender_address) SUBJECT = "Subject: {}'s WAN IP has changed!".format(hostname) msg = "{}\n\n{}'s WAN IP has changed from {} to {}".format( date_time, hostname, old_ip, new_ip) BODY = "{}\n{}\n{}\n\n{}".format(TO, FROM, SUBJECT, msg) username = "******".format(sender_username) password = "******".format(sender_password) server = SMTP_SSL(HOST) server.login(username, password) server.sendmail(FROM, TO, BODY) server.quit()
def make_status_window(): Height, Width = screen.getmaxyx() status_win = screen.subwin(Height / 4 - Width_Factor / 2 , Width, Height / 4 , 0) status_win.box() try: ip = sh.head(sh.awk(sh.getent("ahosts", sh.hostname().strip()),"{print $1}"),n="1").strip() if ip == "127.0.0.1": raise NetworkMisconfiguredException() except sh.ErrorReturnCode: status_win.addstr(1,2,"MapR-Platfora-Sandbox-For-Hadoop setup did not succseed.") raise ServiceFailedtoStartException() make_error_window() except NetworkMisconfiguredException: make_error_window() else: status_win.addstr(1,2,"MapR Services failed to start.", curses.A_BOLD) make_error_window()
def send_alert(updates): HOST = _MailserverAndPort username = _MailserverUser password = _MailserverPassword TO = 'To: {}'.format(_SendTo) FROM = 'From: {}'.format(_SendFrom) SUBJECT = "Subject: Updates available on {}".format(sh.hostname()) msg = updates.strip() BODY = "{}\n{}\n{}\n\n{}".format(TO,FROM,SUBJECT,msg) try: server = SMTP_SSL(HOST) server.login(username, password) server.sendmail(FROM, TO, BODY) except: raise finally: server.quit()
def make_status_window(): Height, Width = screen.getmaxyx() status_win = screen.subwin(Height / 4 - Width_Factor / 2, Width, Height / 4, 0) status_win.box() try: ip = sh.head(sh.awk(sh.getent("ahosts", sh.hostname().strip()), "{print $1}"), n="1").strip() if ip == "127.0.0.1": raise NetworkMisconfiguredException() except sh.ErrorReturnCode: status_win.addstr(1, 2, "_MAPR_BANNER_NAME_ setup did not succseed.") raise ServiceFailedtoStartException() make_error_window() except NetworkMisconfiguredException: make_error_window() else: status_win.addstr(1, 2, "MapR Services failed to start.", curses.A_BOLD) make_error_window()
#!/usr/bin/env python from sh import ratpoison, hostname windows = ratpoison(command='windows %n|%s%t') for w in windows: h = hostname().stdout.strip() if h in w and 'byobu' in w: wid = w.split('|')[0] ratpoison(command="select %s" % wid)
# All Rights Reserved MapR import curses import sh import subprocess import os screen = None Width_Factor = 4 ip = sh.head(sh.awk(sh.getent("ahosts", sh.hostname().strip()), "{print $1}"), n="1").strip() if not os.path.exists("/vmware"): ip = "127.0.0.1" ssh_cmd = "" if ip == "127.0.0.1": ssh_cmd = "ssh mapr@localhost -p 2222" else: ssh_cmd = "ssh mapr@%s" % (ip) class NetworkMisconfiguredException(Exception): pass class ServiceFailedtoStartException(Exception): pass def make_welcome_window(): Height, Width = screen.getmaxyx() welcome_win = screen.subwin(Height / 2 - Width_Factor - 2, Width, 0, 0)
def ui_command_hostname(self): """ Displays the system hostname. """ print sh.hostname('-f')
#!/usr/bin/python import sh HOSTNAME = str(sh.hostname().stdout, 'utf8').replace("\n", "") SOURCEFOLDER_CONTENTS = [ "^\.idea$", "^main.py$", "^manage.py$", "^main.c$", "^manage.py$", "^main.cpp$", "^.metadata$", "^\.git$", "^\.svn$", "^\.svn-base$", "^\.project$", "^include$", "^src$", "^debug$", "^Classes$", "^.*\.xcodeproj$" ]
pass #Do nothing, just try again FORMAT = '%(asctime)-15s %(levelname)s %(message)s' logging.basicConfig(format=FORMAT, level=logging.DEBUG) parser = argparse.ArgumentParser() parser.add_argument('remote_host') parser.add_argument('--remoteuser', dest='remote_user') parser.add_argument('--remoteuserkey', dest='remote_user_sshkeyfile') parser.add_argument('--remoteport', dest='remote_port', default=22) parser.add_argument('--localport', dest='local_port', default=22) args = parser.parse_args() fqdn = sh.hostname('-f') if not '.' in fqdn: raise ValueError( "FQDN does not have a domain. Run 'hostname -f' to troubleshoot.") if not os.path.exists(args.remote_user_sshkeyfile): raise IOError("SSH key file '{0}' for remote user does not exist.".format( args.remote_user_sshkeyfile)) wait_for_internet_connection(args.remote_host, args.remote_port) logging.info('Allocating remote port...') port = sh.ssh('-p', args.remote_port, '-o', 'UserKnownHostsFile=/dev/null', '-o', 'StrictHostKeyChecking=no', '-o',
#All Rights Reserved MapR import curses import sh import subprocess import os screen = None Width_Factor = 4 ip = sh.head(sh.awk(sh.getent("ahosts", sh.hostname().strip()), "{print $1}"), n="1").strip() if not os.path.exists("/vmware"): ip = "127.0.0.1" ssh_cmd = "" if ip == "127.0.0.1": ssh_cmd = "ssh mapr@localhost -p 2222" else: ssh_cmd = "ssh mapr@%s" % (ip) class NetworkMisconfiguredException(Exception): pass class ServiceFailedtoStartException(Exception): pass def make_welcome_window():
import sh from subprocess import check_output print('Ip:' + str(sh.hostname('-I')) + 'hi') if check_output(['hostname','-I']) == '\n': print('Hello') else: print('waiting')
BORG_REPOSITORY = '/your/borg/repo/name' # Get a push code following this: https://jokerqyou.github.io/ethbot PUSH_CODE = 'xxxxxx' # Add the directories you want to backup DIRECTORIES = ( '/important/data', '/home/user/secret/data', ) # Add the directories or patterns you want to exclude EXCLUDES = ( '*.pyc', '*.swp', '/i/dont/care/about/this/data', '/home/Ben/Music/Justin\ Bieber', ) HOSTNAME = hostname().strip() DATE = date('+%Y-%m-%d').strip() def backup(*directories, **kwargs): ''' Backup a directory using borg ''' directories = [d for d in directories if os.path.exists(d)] repository = '{}::{}-{}'.format(BORG_REPOSITORY, HOSTNAME, DATE) excludes = kwargs.pop('excludes', []) excludes = [excludes, ]\ if not isinstance(excludes, (list, tuple, ))\ else excludes arguments = ['--stats', '--compression', 'zlib,5', repository, ] arguments.extend(directories)
vmreload = 'vmreload' in str(procfs.Proc().cmdline) sysbase = sh.Command('systemd-nspawn').bake(D='/aufs/sysbase',M='sysbase-%s' % vmname, _out=sys.stdout) yum = sysbase.yum class Logger(object): def __init__(self): self.terminal = sys.stdout self.log = open("/tmp/onetime.log", "a") def write(self, message): self.terminal.write(message) self.log.write(message) sys.stdout = Logger() tr('starting') # set hostname so things work right hostname(vmname) ########################################################################### ################ ##################### ################ SYSTEMD ##################### ################ ##################### ########################################################################### if not vmreload: tr('systemd') for i in glob.glob(vms('systemd/*')): tgt = '/etc/systemd/system/%s' % os.path.basename(i) if os.path.exists(tgt): os.unlink(tgt) shutil.copy(i, tgt)
#All Rights Reserved MapR import curses import sh import subprocess import os screen = None Width_Factor = 4 ip = sh.head(sh.awk(sh.getent("ahosts", sh.hostname().strip()),"{print $1}"),n="1").strip() if not os.path.exists("/vmware"): ip = "127.0.0.1" ssh_cmd = "" if ip == "127.0.0.1": ssh_cmd = "ssh mapr@localhost -p 2222" else: ssh_cmd = "ssh mapr@%s" % (ip) class NetworkMisconfiguredException(Exception): pass class ServiceFailedtoStartException(Exception): pass def make_welcome_window(): Height, Width = screen.getmaxyx() welcome_win = screen.subwin(Height / 2 - Width_Factor - 2, Width, 0, 0) welcome_win.box() welcome_win.addstr(1,2,"=== Datatorrent-Sandbox-On-MapR ===", curses.A_BOLD)
def hostname(*args): for line in sh.hostname(*args, _iter=True): return line.rstrip("\n")
print(sh.wpa_cli("-i", "wlan0", "list_networks")) #lists all networks #choose cameras from networks listed input1 = input('Choose first camera using the network id (Ex: 1): ') input2 = input('Choose second camera using the network id (Ex: 2): ') recordCmd = input( 'Would you like to start recording for all selected cameras once connected? (y/n): ' ) sh.wpa_cli("-i", "wlan0", "select_network", input1) print(sh.wpa_cli("-i", "wlan0", "list_networks")) time.sleep(2) while sh.hostname('-I') == '\n': print('Waiting for connection...') print(sh.hostname('-I')) time.sleep(1) print(sh.hostname('-I')) gp1 = GoProCamera.GoPro() gp1.overview() if recordCmd == 'y': gp1.shutter(constants.start) if gp1.IsRecording() == 1: print('First camera IsRecording confirmation: YES, RECORDING NOW') endRecording = input('Press [Enter] to End Recording: ')