Example #1
0
def search_atlas(ra, dec, Ryan = True):
    atlas_info = lc_cfg['atlas_info']
    if Ryan:
        remote_session = SSHSession(atlas_info[3]['address'],atlas_info[3]['username']).open()
    else:
        gateway_session1 = SSHSession(atlas_info[2]['address'],atlas_info[2]['username'], password=atlas_info[2]['password']).open()
        if len(atlas_info)==3:
            gateway_session2 = gateway_session1.get_remote_session(atlas_info[1]['address'],atlas_info[1]['username'], password=atlas_info[2]['password'])
            remote_session = gateway_session2.get_remote_session(atlas_info[2]['address'], password=atlas_info[2]['password'])
        elif len(atlas_info)==2:
            remote_session = gateway_session1.get_remote_session(atlas_info[1]['address'], password=atlas_info[1]['password'])
        else: 
            print('wrong format for atlas_params: too many jump connection')

    today = dt.today()
    con = sqlite3.connect(":memory:")
    tdate = ' '+str(list(con.execute("select julianday('"+today.strftime("%Y-%m-%d")+"')"))[0][0]-lc_cfg['lookback_days']-2400000)

    result=remote_session.get_cmd_output('./mod_force.sh '+str(ra)+' '+ str(dec)+tdate)

    atlas_lc = at.Table(names=('jd','mag', 'mag_err','flux', 'fluxerr', 'filter','maj', 'min', 'apfit', 'sky', 'zp', 'zpsys'), dtype=('f8', 'f8', 'f8', 'f8', 'f8', 'S1' , 'f8', 'f8', 'f8', 'f8','f8', 'U8'))

    split = result.split('\r\n')

    for i in split[1:]:
        k = i.split()
#        if int(k[3])>int(k[4]):
        atlas_lc.add_row([float(k[0]), float(k[1]), float(k[2]), float(k[3]), float(k[4]), k[5], float(k[12]), float(k[13]), float(k[15]), float(k[16]),  float(k[17]), 'ab'])

    return atlas_lc
Example #2
0
def test_active_close_session(docker_env):
    gateway_ip, gateway_port = docker_env.get_host_ip_port('gateway')

    gateway_session = SSHSession(host=gateway_ip,
                                 port=gateway_port,
                                 username='******',
                                 password='******').open()
    assert gateway_session.is_active()

    # open an already active session should be harmless
    gateway_session.open()
    assert gateway_session.is_active()

    remotehost_ip, remotehost_port = docker_env.get_host_ip_port('remotehost')
    remotehost_session = gateway_session.get_remote_session(
        host=tests_util.get_host_ip(),
        port=remotehost_port,
        username='******',
        password='******')
    assert remotehost_session.is_active()

    # check that gateway session is well closed
    gateway_session.close()
    assert not gateway_session.is_active()
    # remote session is also automatically closed
    assert not remotehost_session.is_active()

    # closing a closed session does nothing
    gateway_session.close()

    # running command on an inactive session will automatically open the session
    assert gateway_session.run_cmd('ls').exit_code == 0
Example #3
0
def test_file(docker_env):
    gateway_ip, gateway_port = docker_env.get_host_ip_port('gateway')
    gateway_session = SSHSession(host=gateway_ip,
                                 port=gateway_port,
                                 username='******',
                                 password='******').open()

    remotehost_ip, remotehost_port = docker_env.get_host_ip_port('remotehost')
    remotehost_session = gateway_session.get_remote_session(
        host=tests_util.get_host_ip(),
        port=remotehost_port,
        username='******',
        password='******')
    file_content = json.dumps(tests_util.create_random_json())

    # create file in a location with root access needed should fail by default
    with pytest.raises(IOError) as excinfo:
        remotehost_session.file(remote_path='/etc/a_file',
                                content=file_content)
    assert excinfo.value.errno == errno.EACCES
    assert excinfo.value.strerror == 'Permission denied'

    # do same command with root access
    remotehost_session.file(remote_path='/etc/a_file',
                            content=file_content,
                            use_sudo=True)
Example #4
0
def change_link(*args, **kwargs):
    target_link = kwargs.get('link', 'T33L')
    version = kwargs.get('version')
    if kwargs.get('server').startswith('192'):
        gateway_session = SSHSession('173.36.203.62',
                                     'wbxbuilds',
                                     password='******').open()
        print("Connected to sshlogin server.")
        remote_session = gateway_session.get_remote_session(
            kwargs.get('server'),
            kwargs.get('user', 'shuqli'),
            password=kwargs.get('pass', 'wbx@Aa$hfcm'))
        print(f"Connected to target server {kwargs.get('server')}.")
    else:
        remote_session = SSHSession(kwargs.get('server'),
                                    kwargs.get('user', 'wbxroot'),
                                    password=kwargs.get('pass',
                                                        'wbx@AaR00t')).open()

    print("Starting to change link ...")
    remote_session.get_cmd_output(
        "cd /www/htdocs/client; sudo rm -f {target_link}; sudo ln -sf "
        "`ls -lrt|grep WBXclient-{version}|grep ^d|tail -n1|awk '{{"
        "print $NF}}'`  {target_link}; sudo chown -h nobody.nobody "
        "{target_link};".format(version=version, target_link=target_link))
    ret = remote_session.get_exit_code(
        f"test -e /www/htdocs/client/{target_link}")

    print(ret)

    if ret == 0:
        return True
    else:
        return False
Example #5
0
def get_remote_session(tunnel_session: SSHSession, host_address: str, host_user: str,
                       host_key_path: str = ID_RSA_PATH) -> SSHSession:
    try:
        return tunnel_session.get_remote_session(host_address, host_user, private_key_file=host_key_path)
    except Exception as e:
        print("Could not connect to remote host")
        raise e
Example #6
0
def test_get_remote_session(docker_env):
    gateway_ip, gateway_port = docker_env.get_host_ip_port()
    gateway_session = SSHSession(host=gateway_ip,
                                 port=gateway_port,
                                 username='******',
                                 password='******').open()

    remotehost_session = gateway_session.get_remote_session(
        host='remotehost', port=22, username='******', password='******')

    # run basic command on remote host
    assert remotehost_session.get_cmd_output('hostname') == 'remotehost'

    # request twice the same remote session just return the existing one
    assert gateway_session.get_remote_session(
        host='remotehost', port=22, username='******',
        password='******') == remotehost_session

    # request another remote session to another host while an existing one already exists
    remotehost2_session = remotehost_session.get_remote_session(
        host='remotehost2', port=22, username='******', password='******')
    # check that new session is active
    assert remotehost2_session.is_active()
    assert remotehost2_session.get_cmd_output('hostname') == 'remotehost2'

    # check that previous session from gateway is still active
    assert remotehost_session.is_active()
    assert remotehost_session.get_cmd_output('hostname') == 'remotehost'

    # close a remote session and check we can still request ssh session with same parameters
    remotehost2_session.close()
    assert not remotehost2_session.is_active()
    remotehost2_session = remotehost_session.get_remote_session(
        host='remotehost2', port=22, username='******', password='******')
    assert remotehost2_session.is_active()

    # close gateway session and check all children sessions are automatically closed
    gateway_session.close()
    assert not remotehost_session.is_active()
    assert not remotehost2_session.is_active()

    # get remote session from closed session should automatically open gateway session first
    # then return remote session
    remotehost_session = gateway_session.get_remote_session(
        host='remotehost', port=22, username='******', password='******')
    assert gateway_session.is_active()
    assert remotehost_session.is_active()
Example #7
0
def test_get_remote_session(docker_env):
    gateway_ip, gateway_port = docker_env.get_host_ip_port('gateway')
    gateway_session = SSHSession(host=gateway_ip,
                                 port=gateway_port,
                                 username='******',
                                 password='******').open()

    remotehost_ip, remotehost_port = docker_env.get_host_ip_port('remotehost')
    remotehost_session = gateway_session.get_remote_session(
        host=tests_util.get_host_ip(),
        port=remotehost_port,
        username='******',
        password='******')

    # run basic command on remote host
    assert remotehost_session.get_cmd_output(
        'hostname').strip() == 'remotehost.example.com'

    # request twice the same remote session just return the existing one
    assert gateway_session.get_remote_session(
        host=tests_util.get_host_ip(),
        port=remotehost_port,
        username='******',
        password='******') == remotehost_session

    # request another remote session to another host while an existing one already exists
    remotehost2_ip, remotehost2_port = docker_env.get_host_ip_port(
        'remotehost2')
    remotehost2_session = gateway_session.get_remote_session(
        host=tests_util.get_host_ip(),
        port=remotehost2_port,
        username='******',
        password='******')
    # check that new session is active
    assert remotehost2_session.is_active()
    assert remotehost2_session.get_cmd_output(
        'hostname').strip() == 'remotehost2.example.com'

    # check that previous session from gateway has been automatically closed
    assert not remotehost_session.is_active()
Example #8
0
def ssh_jumpserver_to_server():
    """ create SSH client with jump server"""
    from jumpssh import SSHSession
    # establish ssh connection between your local machine and the jump server
    gateway_session = SSHSession('steppingstone',
                                 'my_user',
                                 password='******').open()
    # from jump server, establish connection with a remote server
    remote_session = gateway_session.get_remote_session(
        'remote.example.com', password='******')

    #execute command on end server
    remote_session.get_cmd_output('ls -lta')
Example #9
0
    def ConnectViaJH(self, JHname, user, pswd):
        print("connecting the jumphost...")
        gateway_session = SSHSession(JHname, user, password=pswd).open()
        print("connecting the device...")
        nexthop = '2a0b:2900:114a:50b::1:10'
        self.remote_session = gateway_session.get_remote_session(
            nexthop,
            username='******',
            private_key_file='D:\code\ssh\cso_id_rsa')
        print("try pwd")
        feedback = self.remote_session.get_cmd_output("bash -lc 'pwd'")

        print(feedback)
Example #10
0
def test_put(docker_env):
    gateway_ip, gateway_port = docker_env.get_host_ip_port('gateway')
    gateway_session = SSHSession(host=gateway_ip,
                                 port=gateway_port,
                                 username='******',
                                 password='******').open()

    remotehost_ip, remotehost_port = docker_env.get_host_ip_port('remotehost')
    remotehost_session = gateway_session.get_remote_session(
        host=tests_util.get_host_ip(),
        port=remotehost_port,
        username='******',
        password='******')
    # exception is raised when local file does not exist
    local_path = 'missing_folder/missing_path'
    with pytest.raises(IOError) as excinfo:
        remotehost_session.put(local_path=local_path,
                               remote_path='/tmp/my_file')
    assert excinfo.value.errno == errno.ENOENT
    assert excinfo.value.strerror == "Local file '%s' does not exist" % local_path

    # create random file locally
    local_path = os.path.join(os.path.dirname(__file__), 'random_file')
    dummy_json = tests_util.create_random_json()
    with open(local_path, 'wb') as random_file:
        random_file.write(json.dumps(dummy_json).encode('utf-8'))
    try:
        # copy file on remote session
        remote_path = '/tmp/random_file'
        assert remotehost_session.exists(remote_path) is False
        remotehost_session.close()
        remotehost_session.put(local_path=local_path, remote_path=remote_path)
        assert remotehost_session.exists(remote_path) is True

        # copy file on remote session as user2 with specific file permissions
        remote_path = '/tmp/random_file2'
        assert remotehost_session.exists(remote_path) is False
        remotehost_session.put(local_path=local_path,
                               remote_path=remote_path,
                               owner='user2',
                               permissions='600')
        assert remotehost_session.exists(remote_path) is True
        assert remotehost_session.get_cmd_output(
            "ls -l %s | awk '{print $3}'" % remote_path) == 'user2'
        assert remotehost_session.get_cmd_output(
            "stat -c '%a %n' " + remote_path + " | awk '{print $1}'") == '600'
    finally:
        os.remove(local_path)
Example #11
0
def test_get(docker_env):
    gateway_ip, gateway_port = docker_env.get_host_ip_port('gateway')
    gateway_session = SSHSession(host=gateway_ip,
                                 port=gateway_port,
                                 username='******',
                                 password='******').open()

    remotehost_ip, remotehost_port = docker_env.get_host_ip_port('remotehost')
    remotehost_session = gateway_session.get_remote_session(
        host=tests_util.get_host_ip(),
        port=remotehost_port,
        username='******',
        password='******')

    # create random file on remote host and ensure it is properly there
    remote_path = "remote_file"
    remotehost_session.file(remote_path=remote_path,
                            content=json.dumps(
                                tests_util.create_random_json()))
    assert remotehost_session.exists(remote_path)

    # download that file in local folder
    local_folder = '/tmp/'
    remotehost_session.get(remote_path=remote_path, local_path=local_folder)
    local_file_path = os.path.join(local_folder, os.path.basename(remote_path))
    assert os.path.isfile(local_file_path)
    os.remove(local_file_path)

    # download that file locally specifying local filename
    local_file_path = '/tmp/downloaded_file_' + util.id_generator(size=20)
    remotehost_session.get(remote_path=remote_path, local_path=local_file_path)
    os.remove(local_file_path)

    # get remote file from location not accessible from current user and owned by root
    local_folder = '/tmp/'
    restricted_remote_path = os.path.join('/etc', remote_path)
    remotehost_session.run_cmd([
        'sudo mv %s %s' % (remote_path, restricted_remote_path),
        'sudo chown root:root %s' % restricted_remote_path,
        'sudo chmod 700 %s' % restricted_remote_path,
    ])
    remotehost_session.get(remote_path=restricted_remote_path,
                           local_path=local_folder,
                           use_sudo=True)
    local_file_path = os.path.join(local_folder, os.path.basename(remote_path))
    assert os.path.isfile(local_file_path)
    os.remove(local_file_path)
Example #12
0
def test_handle_big_json_files(docker_env):
    gateway_ip, gateway_port = docker_env.get_host_ip_port()
    gateway_session = SSHSession(host=gateway_ip,
                                 port=gateway_port,
                                 username='******',
                                 password='******').open()

    remotehost_session = gateway_session.get_remote_session(
        host='remotehost', port=22, username='******', password='******')
    # generate big json file on remotehost
    remote_path = '/tmp/dummy.json'
    dummy_json = tests_util.create_random_json(50000)
    remotehost_session.file(remote_path=remote_path,
                            content=json.dumps(dummy_json))

    # read file from remote and check json is valid and identical to source
    dummy_json_from_remote = json.loads(
        remotehost_session.get_cmd_output('cat %s' % remote_path))
    assert dummy_json == dummy_json_from_remote
Example #13
0
def check_pfx(path_pfx_file, password, bastion, remote_host):
    p12 = crypto.load_pkcs12(open(path_pfx_file, 'rb').read(), password)
    private_key = crypto.dump_privatekey(crypto.FILETYPE_PEM,
                                         p12.get_privatekey())
    public_certificate = crypto.dump_certificate(crypto.FILETYPE_PEM,
                                                 p12.get_certificate())
    # try:
    #     ca_certificate = crypto.dump_certificate(crypto.FILETYPE_PEM, p12.get_ca_certificates())

    gateway_session = SSHSession(host=bastion,
                                 port=7022,
                                 password=None,
                                 missing_host_key_policy=None,
                                 username='******')
    remote_session = gateway_session.get_remote_session(remote_host,
                                                        password=None,
                                                        username='******',
                                                        port=7022)
    remote_session.put('./remote_backup.sh',
                       '/root/remote_backup.sh',
                       owner='root',
                       permissions='0700')
    remote_session.run_cmd('/root/remote_backup.sh')
    remote_session.file(remote_path='/etc/apache/passl/pa.cert.key',
                        content=private_key,
                        owner='root',
                        permissions='644')
    remote_session.file(remote_path='/etc/apache/passl/pa.cert.cert',
                        content=public_certificate,
                        owner='root',
                        permissions='644')
    # remote_session.file(remote_path='/etc/apache/passl/pa.cert.intermediate', content=ca_certificate, owner='root',
    #                     permissions='644')
    print(remote_session.get_cmd_output('ls -alh'))
    print(
        remote_session.get_cmd_output(
            "ls -alh /etc/apache/passl/backup-$(date +'%F')"))
    print(remote_session.get_cmd_output('ls -alh /var/qmail/control/'))

    remote_session.close()
    gateway_session.close()
def _run(cmd, sudo=False, *args, **kwargs):
    '''Executes a command on a host behind a gateway. Necessary for testing 
    in development when spawning instances in private prodiction subnet. '''

    max_retries = kwargs.pop("max_retries", 10)
    host = os.environ.get('AWS_SPAWNER_WORKER_IP')
    gateway_session = SSHSession(BASTION,
                                 BASTIONUSER,
                                 private_key_file=KEYPATH).open()
    remote_session = gateway_session.get_remote_session(host,
                                                        retry=max_retries,
                                                        retry_interval=2)
    cmd = cmd.replace('\n', '')
    if sudo:
        output = remote_session.run_cmd(cmd,
                                        username='******',
                                        raise_if_error=False)
    else:
        output = remote_session.run_cmd(cmd, raise_if_error=False)

    logger.info(output)
    return output.output
Example #15
0
"""
The main purpose of this script is to dispatch a command via ssh connection through an intermediate
connection (gateway) with another machine, in this case:
local.machine -> 10.10.10.5 -> 10.10.10.2
"""
from jumpssh import SSHSession

server_session = SSHSession('10.10.10.5', 'osboxes',
                            password='******').open()
switch_session = server_session.get_remote_session('10.10.10.2',
                                                   'admin',
                                                   password='******')
print(switch_session.get_cmd_output('show ip int brief'))
server_session.close()
Example #16
0
from jumpssh import SSHSession

# establish ssh connection between local and a jump cisco router
gateway_session = SSHSession('192.168.32.101',
                             username='******',
                             password='******',
                             look_for_keys=False,
                             allow_agent=False).open()

print('pass')
# from the jump router, establish connection with another remote router
remote_session = gateway_session.get_remote_session('12.1.1.2',
                                                    username='******',
                                                    password='******',
                                                    look_for_keys=False,
                                                    allow_agent=False)

print(remote_session.get_cmd_output('show ip int bri'))
Example #17
0
#!/usr/bin/python3
from jumpssh import SSHSession  # we need this for establishing the SSH
import getpass
import glob

csl3 = 'csl3.cs.technion.ac.il'
cscomp = 'cscomp.cs.technion.ac.il'
CDDIR = 'cd CompilationTests/hw3; '

username = input("Username: ")
password = getpass.getpass()

print('Establishing client through csl3 bridge')
gateway_session = SSHSession(csl3, username, password=password)
print('Established bridge. Connecting to remote.')
remote_session = gateway_session.get_remote_session(cscomp, password=password)
print('Connected to remote.')

testdir_exists = remote_session.exists('CompilationTests')
if testdir_exists:
    print('Test dir found')
else:
    print('Test dir not found')
    print('Cloning tests: ')
    print(
        remote_session.get_cmd_output(
            'git clone https://github.com/schorrm/CompilationTests.git'))

zips = glob.glob('[0-9]*-[0-9]*.zip')
if not zips:
    print('Submission not found!')
# Installing the jumpssh module
# pip install jumpssh

# Installing openssh server on Ubuntu Linux
# sudo apt - get install openssh - server - y

# Installing OpenSSH Client on Windows 10
# https: // docs.microsoft.com / en - us / windows - server / administration / openssh / openssh_install_firstuse

# Run remote script on the Arista switch via the Ubuntu remote server
# Open cmd as Administrator before performing SSH from Windows!
# ssh mihai @ 10.10.10.100 "python3 /home/mihai/ping_script.py"

# Run local script on the Arista switch via the Ubuntu remote server
# The dash ( - ) is optional and it means the program reads from stdin
# Open cmd as Administrator before performing SSH from Windows!
# more D:\ping_script.py | ssh mihai @ 10.10.10.100 python3 -

# Executing CLI commands and getting the output via the Ubuntu remote server
from jumpssh import SSHSession

gateway_session = SSHSession('10.10.10.100', 'mihai', password='******').open()

remote_session = gateway_session.get_remote_session('10.10.10.3',
                                                    'admin',
                                                    password='******')

print(remote_session.get_cmd_output('show ip int brief'))

gateway_session.close()
Example #19
0
# workbook = xlsxwriter.Workbook('jumphost_ssh_8Maret.xlsx')
# worksheet = workbook.add_worksheet("Basic Router Info")
# worksheet.write("A1", "Interface")
# worksheet.write('B1', "IP-Address")
# worksheet.write("C1", "Ok?")
# worksheet.write("D1", "Method")
# worksheet.write("E1", "Status up")
# worksheet.write("F1", "Protocol up")

# row = 1
# col = 0

gateway_session = SSHSession(host="192.168.100.4",
                             username="******",
                             password="******").open()

ip_list = ["192.168.122.226", "192.168.122.119"]

for ip in ip_list:
    print("connecting to ", ip)
    remote_session = gateway_session.get_remote_session(ip,
                                                        username="******",
                                                        password="******",
                                                        look_for_keys=False)
    print(remote_session.get_cmd_output("sho ip int br"))
    print(remote_session.get_cmd_output("sho version | i uptime"))
    print(
        "========================================================================================="
    )
    print("\n")
    print("正在连接 ",ip,".....")
    if hosts_dict[ip] in ud_lists:   #判断哪个主机用那个主机名
        des_username = kwords.des_Ud_HostUser
    else:
        des_username = kwords.curre_username

    a = inspect_func.RemoteHost_IsActive(jump_server, ip, des_username, kwords.des_Ud_HostPass) #判断主机是否可连接 正常返回True
    if a != True:
        with open("inspect_log.log", "a") as f:
            f.write("IP {0} 主机异常\n".format(ip))
            print("IP:{0} 主机异常,连接超时".format(ip),time.strftime('%Y/%m/%d %H:%M:%S'))
        continue

    try:  #连接目标主机
        des_remote = jump_server.get_remote_session(ip, username=des_username, password=kwords.des_Ud_HostPass,
                                                retry=1, retry_interval=5)
        #print(des_remote.get_cmd_output("ls"))
    except Exception as e:
        print(e,"aaaaaaaaa")
        des_remote.close()
        continue
#---------------------------------------------------------------------------------------------------------------#
    try:
        if hosts_dict[ip] == ud_lists[0]:  # 判断是Ud哪台服务器40
            print("正在巡检: " + hosts_dict[ip], end=" ")
            udList_1 = [cmdLists[0], cmdLists[10], cmdLists[11], cmdLists[12], cmdLists[5]]  # 用于巡检Ud的命令
            inspect_func.inspect_ud40(udList_1, " ", des_remote,result_list_info)
            print("     Done.",time.strftime('%Y/%m/%d %H:%M:%S'))

        if hosts_dict[ip] in ud_lists[1:3]:  # 判断是Ud哪台服务器45\46
            print("正在巡检: " + hosts_dict[ip], end=" ")
Example #21
0
from jumpssh import SSHSession

# establish ssh connection between local and a linux server
gateway_session = SSHSession('192.168.32.130',username='******',password='******').open()

print('pass')
# from the jump router, establish connection with another remote router
#remote_session = gateway_session.get_remote_session('192.168.32.101',username='******',password='******')
remote_session = gateway_session.get_remote_session('192.168.32.101',username='******',password='******',look_for_keys=False, allow_agent=False)

print(remote_session.get_cmd_output('show ip int bri'))


#jump机只能用linux或者windows

Example #22
0
from flask import jsonify
import sys
import psutil
import cx_Oracle
import json
import json
import time
import config
from jumpssh import SSHSession
from queries import basic_info, system_info, temp_tbs, tbs_detail, default_tbs, space_usage, tbs_quota, fra_status, \
    reco_usage, datafile_usage, rman_time, rman_current, rman_history, log_locations, alert_log_View, locked_obj, \
     asm_usage, asm_stat, mem1, mem2,listener_log,active_instance,statgather

gateway_session = SSHSession('localhost', 'oracle', password='******').open()
remote_session = gateway_session.get_remote_session('localhost',
                                                    'oracle',
                                                    password='******')

con = config.connect()


def get_paginated_list(results, url, start, limit):
    start = int(start)
    limit = int(limit)
    count = len(results)
    if count < start or limit < 0:
        abort(404)
    # make response
    obj = {}
    obj['start'] = start
    obj['limit'] = limit
Example #23
0
def action_job_with_shell(action, item, env, *args, **kwargs):
    build = kwargs.get('build')
    server = kwargs.get('server')
    action = 'install' if action == 'upgrade' else action
    downloadonly = kwargs.get('downloadonly', False)
    try:
        version = re.search('.*-(\d+.\d+.\d+)', build.split()[0]).group(1)
    except:
        version = '39'
    if kwargs.get('server').startswith('192'):
        gateway_session = SSHSession('173.36.203.62',
                                     'wbxbuilds',
                                     password='******').open()
        print("Connected to sshlogin server.")
        remote_session = gateway_session.get_remote_session(
            server,
            kwargs.get('user', 'shuqli'),
            password=kwargs.get('pass', 'wbx@Aa$hfcm'))
    else:
        remote_session = SSHSession(server,
                                    kwargs.get('user', 'wbxroot'),
                                    password=kwargs.get('pass',
                                                        'wbx@AaR00t')).open()
    print(f"Connected to target server {kwargs.get('server')}.")

    if item == 'client':

        print(f"Starting {action} {item} ...")
        exit_code = remote_session.get_exit_code(
            f"sudo yum clean all;sudo yum erase -y `rpm -qa|grep -E"
            f" 'WBXclient|WBXmsi'`;sudo yum install -y {build}")
        if exit_code == 0:
            if downloadonly:
                print("Downloaded success.")
                return True
            else:
                print("Starting to change link ...")
                if change_link(env,
                               server=server,
                               version=version,
                               link='T33L'):
                    return True
                else:
                    print("Changed Link Failed.")
                    return False
        else:
            print("Downgraded failed.")
            return False
    else:
        print(f"Starting {action} {item} ...")
        if downloadonly:
            remote_session.get_exit_code(
                r"sudo find /home -name 'WBX*.rpm' -exec rm -f {} \;")
            exit_code = remote_session.get_exit_code(
                f"sudo yum clean all;sudo yum {action} -y {build}  --downloadonly --downloaddir=."
            )
            if exit_code == 0:
                return True
            else:
                return False
        else:
            exit_code = remote_session.get_exit_code(
                f"sudo yum clean all;sudo yum {action} -y {build}")
            remote_session.get_exit_code(
                r"sudo find /home -name 'WBX*.rpm' -exec rm -f {} \;")
            if exit_code == 0:
                print(f"Starting to restart service  ...")
                ret = remote_session.get_exit_code(
                    f"sudo service wbxd stop;sudo service wbxd start")
                if ret == 0:
                    return True
                else:
                    return False
            else:
                print(f"{action} failed.")
                return False
Example #24
0
import subprocess
import sys

ip = '10.10.10.3'


ping_reply = subprocess.call(['ping','-c','2',ip],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)

if ping_reply == 0:
    print("\n{} is reachable :)".format(ip))
else:
    print("\n{} is not reachable :(".format(ip))
    sys.exit()

###Running the python using intermediary server Windows-->ubuntu-->cisko_switch

from jumpssh import SSHSession

server_session = SSHSession("10.10.10.100",'chetu',password='******').open()
switch_session = server_session.get_remote_session('10.10.103', 'admin', password='******')
print(switch_session.get_cmd_output('show ip int brief'))
server_session.close()

#Instructions
###########Example for running local script on remote server
#more filepath\file_name.py | ssh user@ip password

###########Example for running remote script via local machine
#ssh user@ip "python3 /path/file_name.py"
Example #25
0
class download_atlas_lc_class:
	def __init__(self):
		self.verbose = 0
		self.outputbasedir = '.'
		self.remote_session = None
		self.lcraw = None
		self.baseurl = 'https://fallingstar-data.com/forcedphot'
		
	def define_args(self, parser=None, usage=None,conflict_handler='resolve'):
		if parser is None:
			parser = argparse.ArgumentParser(usage=usage,conflict_handler=conflict_handler)
		parser.add_argument('RA',help="RA position")
		parser.add_argument('Dec',help="Dec position")
		
		return(parser)
		
	def define_optional_args(self, parser=None, usage=None,conflict_handler='resolve'):
		if parser is None:
			parser = argparse.ArgumentParser(usage=usage,conflict_handler=conflict_handler)
		
		parser.add_argument('--verbose', '-v', default=0, action='count')
		parser.add_argument('-d', '--debug', help="debug", action='count')
		parser.add_argument('--outputbasedir', default=None,
							help=('Define the output basedirectory (default=%(default)s)'))
		parser.add_argument('-l','--lookbacktime_days', type=int, default=None, help=("lookback time in days"))
		parser.add_argument('--atlasmachine', default='atlas-base-sc01.ifa.hawaii.edu',
							help=('address for atlas machine (default=%(default)s)'))
		parser.add_argument('--user', default=None,
							help=('user name for atlas machine (default=%(default)s)'))
		parser.add_argument('--passwd', default=None,
							help=('password for atlas machine'))
		parser.add_argument('-s','--savelc', help=("save lc with this name"))
		parser.add_argument('-f','--fileformat', default='fixed_width_two_line', choices=['basic','csv','rdb','tab','fixed_width','fixed_width_two_line'],
							help=("specify the file format (https://docs.astropy.org/en/stable/io/ascii/index.html#supported-formats) (default=%(default)s)"))
		parser.add_argument('-o','--overwrite', help="overwrite existing file if saved",
							action="store_true", default=True)
		
		return(parser)
	   
	def connect(self, address, username=None, passwd=None):
		if self.verbose:
			print('Connecting session...')

		self.gateway_session = SSHSession(address, username, password=passwd).open()

		if self.verbose:
			print('session %s@%s' % ( username, address ))
		
		self.remote_session = self.gateway_session.get_remote_session(address, password=passwd)
		
		return(self.check_connection())

	def check_connection(self):
		if not self.remote_session.is_active():
			print("Something went wrong with connection!!!")
			return(False)
		
		if self.verbose:
			print ('Connection established!!')

		return(True)
		
	def build_cmd(self, ra, dec, dodb=1, parallel=20, lookbacktime_days=None):
		print(RaInDeg(ra),DecInDeg(dec),ra,dec)
		cmd = 'force.sh %f %f' % (RaInDeg(ra), DecInDeg(dec))
		
		if dodb>0:
			cmd+=' dodb=%d' % dodb 
		
		if parallel>0:
			cmd+=' parallel=%d' % parallel
		
		if lookbacktime_days!=None:
			mjd = Time.now().mjd
			if self.verbose>1:
				print('Today\'s MJD: %.0f' % mjd)
			cmd += ' m0=%d' % int(mjd - lookbacktime_days)
		
		return(cmd)

	def exec_cmd(self,cmd):
		if self.remote_session is None:
			raise(RuntimeError,"Remote session not initiated!!")
		
		tmp=self.remote_session.get_cmd_output(cmd)
		self.lcraw = tmp.split('\r\n')
		Nlines = len(self.lcraw)-1

		if self.verbose>1:
			print('LC has %d lines of data' % Nlines)
		del(tmp)

		# remove leading #
		if Nlines>=0:
			self.lcraw[0]= re.sub('^\#+','',self.lcraw[0])
		
		return(0)

	def get_lc(self, ra, dec, lookbacktime_days=None):
		# check connection
		if not self.check_connection():
			raise(RuntimeError,"No Connection!!!")
		
		# build the cmd
		cmd = self.build_cmd(ra, dec, lookbacktime_days=lookbacktime_days)
		print('Command for photometry: %s' % cmd)

		# run the cmd
		self.exec_cmd(cmd)
		if self.verbose>2:
			print('Raw ATLAS lc:')
			for s in self.lcraw:print(s)

	def save_lc(self, filename, overwrite=False, fileformat='fixed_width_two_line'):
		self.lc.write(filename, format=fileformat, overwrite=overwrite, verbose=(self.verbose>0))

	"""
	def connect_api(self,username,password):
		#ra, dec, name = sys.argv[1:]
		token_header = connect_atlas(username,password)
		data = get_result(ra, dec, token_header)
		#ascii.write(data, name + '.csv', format = 'csv', overwrite = True)
	"""

	def connect_atlas(self,username,password):
		resp = requests.post(url=f"{self.baseurl}/api-token-auth/",data={'username':username,'password':password})
		if resp.status_code == 200:
			token = resp.json()['token']
			print(f'Your token is {token}')
			headers = {'Authorization':f'Token {token}','Accept':'application/json'}
		else:
			print(f'ERROR {resp.status_code}')
			print(resp.json())
		return headers

	# API GUIDE: https://fallingstar-data.com/forcedphot/apiguide/
	def get_result(self,ra,dec,headers,lookbacktime_days=None,mjd_max=None):
		today = dt.today()
		con = sqlite3.connect(":memory:")

		if not(lookbacktime_days is None):
			lookbacktime_days = int(Time.now().mjd - lookbacktime_days)
			#lookbacktime_days = '  '+str(list(con.execute("select julianday('"+today.strftime("%Y-%m-%d")+"')"))[0][0]-lookbacktime_days-2400000)
		else:
			lookbacktime_days = int(Time.now().mjd - 1890)
		if not(mjd_max) is None:
			mjd_max = int(Time.now().mjd - mjd_max)

		print('MJD min: ',lookbacktime_days,'. MJD max: ',mjd_max)

		task_url = None
		while not task_url:
			with requests.Session() as s:
				resp = s.post(f"{self.baseurl}/queue/",headers=headers,data={'ra':ra,'dec':dec,'send_email':False,"mjd_min":lookbacktime_days,"mjd_max":mjd_max})
				if resp.status_code == 201:  # success
					task_url = resp.json()['url']
					print(f'The task URL is {task_url}')
				elif resp.status_code == 429:  # throttled
					message = resp.json()["detail"]
					print(f'{resp.status_code} {message}')
					t_sec = re.findall(r'available in (\d+) seconds', message)
					t_min = re.findall(r'available in (\d+) minutes', message)
					if t_sec:
						waittime = int(t_sec[0])
					elif t_min:
						waittime = int(t_min[0]) * 60
					else:
						waittime = 10
					print(f'Waiting {waittime} seconds')
					time.sleep(waittime)
				else:
					print(f'ERROR {resp.status_code}')
					print(resp.json())
					sys.exit()
		result_url = None
		
		while not result_url:
			with requests.Session() as s:
				resp = s.get(task_url, headers=headers)
				if resp.status_code == 200:  # HTTP OK
					if resp.json()['finishtimestamp']:
						result_url = resp.json()['result_url']
						print(f"Task is complete with results available at {result_url}")
						break
					elif resp.json()['starttimestamp']:
						print(f"Task is running (started at {resp.json()['starttimestamp']})")
					else:
						print("Waiting for job to start. Checking again in 10 seconds...")
					time.sleep(10)
				else:
					print(f'ERROR {resp.status_code}')
					print(resp.json())
					sys.exit()
		
		with requests.Session() as s:
			result = s.get(result_url, headers=headers).text
		
		dfresult = pd.read_csv(io.StringIO(result.replace("###", "")), delim_whitespace=True)
		return dfresult