def refresh_credentials(vps247_account, ip_address, new_root_password, new_stats_password, new_stats_username): ssh = psi_ssh.make_ssh_session(ip_address, vps247_account.base_ssh_port, 'root', vps247_account.base_root_password, None) ssh.exec_command('echo "root:%s" | chpasswd' % (new_root_password, )) ssh.exec_command('useradd -M -d /var/log -s /bin/sh -g adm %s' % (new_stats_username)) ssh.exec_command('echo "%s:%s" | chpasswd' % (new_stats_username, new_stats_password)) user_exists = ssh.exec_command('grep %s /etc/ssh/sshd_config' % new_stats_username) if not user_exists: ssh.exec_command( 'sed -i "s/^AllowUsers.*/& %s/" /etc/ssh/sshd_config' % new_stats_username) ssh.exec_command('service ssh restart') ssh.exec_command('rm /home/debian/*') ssh.exec_command('rm /etc/ssh/ssh_host_*') ssh.exec_command('rm -rf /root/.ssh') ssh.exec_command('dpkg-reconfigure openssh-server') return ssh.exec_command('cat /etc/ssh/ssh_host_rsa_key.pub')
def set_host_name(digitalocean_account, ip_address, password, new_hostname): # Note: hostnamectl is for systemd servers ssh = psi_ssh.make_ssh_session(ip_address, digitalocean_account.base_ssh_port, 'root', None, None, digitalocean_account.base_rsa_private_key) ssh.exec_command('hostnamectl set-hostname %s' % new_hostname)
def get_egress_ip_address(linode_account, ip_address, password, host_public_key): ssh = psi_ssh.make_ssh_session(ip_address, linode_account.base_ssh_port, 'root', password, host_public_key) egress_ip = ssh.exec_command( "/sbin/ifconfig eth0 | grep 'inet ' | awk '{print $2}'") return egress_ip.split("\n")[0]
def refresh_credentials(digitalocean_account, ip_address, new_root_password, new_stats_password, stats_username): """ Sets a new unique password on the droplet and removes the old ssh_host key. digitalocean_account : Digitalocean account details ip_address : droplet.ip_address new_root_password : new root password to set new_stats_password : new stats password to set stats_username : stats username to change password for """ # Note: using auto-add-policy for host's SSH public key here since we can't get it through the API. # There's a risk of man-in-the-middle. ssh = psi_ssh.make_ssh_session(ip_address, digitalocean_account.base_ssh_port, 'root', None, None, digitalocean_account.base_rsa_private_key) ssh.exec_command('echo "root:%s" | chpasswd' % (new_root_password, )) ssh.exec_command('useradd -M -d /var/log -s /bin/sh -g adm %s' % (stats_username)) ssh.exec_command('echo "%s:%s" | chpasswd' % (stats_username, new_stats_password)) ssh.exec_command('rm /etc/ssh/ssh_host_*') ssh.exec_command('rm -rf /root/.ssh') ssh.exec_command( 'export DEBIAN_FRONTEND=noninteractive && dpkg-reconfigure openssh-server' ) ssh.exec_command( 'sed -i -e "/^PasswordAuthentication no/s/^.*$/PasswordAuthentication yes/" /etc/ssh/sshd_config' ) ssh.exec_command('service ssh restart') return ssh.exec_command('cat /etc/ssh/ssh_host_rsa_key.pub')
def set_allowed_users(linode_account, ip_address, password, host_public_key, stats_username): ssh = psi_ssh.make_ssh_session(ip_address, linode_account.base_ssh_port, 'root', password, host_public_key) user_exists = ssh.exec_command('grep %s /etc/ssh/sshd_config' % stats_username) if not user_exists: ssh.exec_command('sed -i "s/^AllowUsers.*/& %s/" /etc/ssh/sshd_config' % stats_username) ssh.exec_command('service ssh restart')
def update_system_packages(digitalocean_account, ip_address): ssh = psi_ssh.make_ssh_session(ip_address, digitalocean_account.base_ssh_port, 'root', None, None, digitalocean_account.base_rsa_private_key) ssh.exec_command( 'export DEBIAN_FRONTEND=noninteractive && aptitude update -q && aptitude safe-upgrade -y -o Dpkg::Options::="--force-confdef"' )
def install_tcs(vps247_account, ip_address): ssh = psi_ssh.make_ssh_session(ip_address, 22, 'root', vps247_account.base_root_password, None) ssh.exec_command( 'bash /home/debian/native.sh > /home/debian/installing.log') ssh.close() return
def _refresh_credentials(self, ssh_info, new_root_password, new_stats_password): ssh = psi_ssh.make_ssh_session(*ssh_info, verbose=self._verbose) ssh.exec_command('echo "%s:%s" | chpasswd' % (self._account.root_username, new_root_password,)) ssh.exec_command('echo "%s:%s" | chpasswd' % (self._account.stats_username, new_stats_password)) ssh.exec_command('rm /etc/ssh/ssh_host_*') ssh.exec_command('rm -rf /root/.ssh') ssh.exec_command('dpkg-reconfigure openssh-server') return ssh.exec_command('cat /etc/ssh/ssh_host_rsa_key.pub')
def _reboot(self, ssh_info): ''' Reboots the server and waits until it is available again. This function SSHes into the server and issues a reboot command. Alternatively, we could use the ElasticHosts API to do an ACPI reboot. ''' ssh = psi_ssh.make_ssh_session(*ssh_info, verbose=self._verbose) ssh.exec_command('reboot') ssh.close() # Try to connect again, retrying. When it succeeds, the reboot will be done. # Wait a little to make sure we're not connecting *before* the reboot. time.sleep(5) ssh = psi_ssh.make_ssh_session(*ssh_info, verbose=self._verbose) ssh.close()
def add_swap_file(scaleway_account, ip_address): ssh = psi_ssh.make_ssh_session(ip_address, scaleway_account.base_ssh_port, 'root', None, None, host_auth_key=scaleway_account.base_rsa_private_key) ssh.exec_command('dd if=/dev/zero of=/swapfile bs=1024 count=1048576 && mkswap /swapfile && chown root:root /swapfile && chmod 0600 /swapfile') ssh.exec_command('echo "/swapfile swap swap defaults 0 0" >> /etc/fstab') ssh.exec_command('swapon -a') ssh.close() return
def set_allowed_users(scaleway_account, ip_address, stats_username): ssh = psi_ssh.make_ssh_session(ip_address, scaleway_account.base_ssh_port, 'root', None, None, host_auth_key=scaleway_account.base_rsa_private_key) user_exists = ssh.exec_command('grep %s /etc/ssh/sshd_config' % stats_username) if not user_exists: ssh.exec_command('sed -i "s/^AllowUsers.*/& %s/" /etc/ssh/sshd_config' % stats_username) ssh.exec_command('service ssh restart')
def update_kernel(digitalocean_account, do_mgr, droplet): """ This updates the kernel to use the same one as provided in the apt system packages. digitalocean_account : DigitalOcean account information do_mgr : digitalocean.Manager droplet : droplet details. Gathered from droplet.load() returns: droplet : droplet details. """ current_kernel_name = None ssh = psi_ssh.make_ssh_session(droplet.ip_address, digitalocean_account.base_ssh_port, 'root', None, None, digitalocean_account.base_rsa_private_key) droplet_kernel_pkg = ssh.exec_command('aptitude show linux-image-`uname -r`').split('\n') droplet_uname = ssh.exec_command('uname -r').strip() if len(droplet_kernel_pkg) > 0: for line in droplet_kernel_pkg: if 'State: installed' in line: print line if 'Version: ' in line: print line current_kernel_name = line.split(': ')[1].split('+')[0] break if not current_kernel_name: raise Exception('Current Kernel version is not found') droplet_kernels = droplet.get_kernel_available() new_kernel = None if current_kernel_name not in droplet.kernel['name']: for kernel in droplet_kernels: if current_kernel_name in kernel.name and droplet_uname == kernel.version: print 'Kernel found. ID: %s, Name: %s' % (kernel.id, kernel.name) new_kernel = kernel break if new_kernel: print 'Change to use new kernel. ID: %s' % (new_kernel.id) result = droplet.change_kernel(new_kernel) if not wait_on_action(do_mgr, droplet, result['action']['id'], 30, 'change_kernel', 'completed'): raise Exception('Event did not complate on time') droplet = droplet.load() result = droplet.power_cycle() print result if not wait_on_action(do_mgr, droplet, result['action']['id'], 30, 'power_cycle', 'completed'): raise Exception('Event did not complete in time') droplet = droplet.load() if droplet.status != 'active': result = droplet.power_on() if not wait_on_action(do_mgr, droplet, result['action']['id'], 30, 'power_on', 'completed'): raise Exception('Event did not complete in time') droplet = droplet.load() return droplet
def refresh_credentials(linode_account, ip_address, new_root_password, new_stats_password): ssh = psi_ssh.make_ssh_session(ip_address, linode_account.base_ssh_port, 'root', linode_account.base_root_password, linode_account.base_host_public_key) ssh.exec_command('echo "root:%s" | chpasswd' % (new_root_password,)) ssh.exec_command('echo "%s:%s" | chpasswd' % (linode_account.base_stats_username, new_stats_password)) ssh.exec_command('rm /etc/ssh/ssh_host_*') ssh.exec_command('rm -rf /root/.ssh') ssh.exec_command('dpkg-reconfigure openssh-server') return ssh.exec_command('cat /etc/ssh/ssh_host_rsa_key.pub')
def refresh_credentials(digitalocean_account, ip_address, new_root_password, new_stats_password): # Note: using auto-add-policy for host's SSH public key here since we can't get it through the API. # There's a risk of man-in-the-middle. ssh = psi_ssh.make_ssh_session(ip_address, digitalocean_account.base_ssh_port, 'root', None, None, digitalocean_account.base_rsa_private_key) ssh.exec_command('echo "root:%s" | chpasswd' % (new_root_password,)) ssh.exec_command('echo "%s:%s" | chpasswd' % (digitalocean_account.base_stats_username, new_stats_password)) ssh.exec_command('rm /etc/ssh/ssh_host_*') ssh.exec_command('rm -rf /root/.ssh') ssh.exec_command('dpkg-reconfigure openssh-server') return ssh.exec_command('cat /etc/ssh/ssh_host_rsa_key.pub')
def refresh_credentials(scaleway_account, ip_address, new_root_password, new_stats_password, stats_username): ssh = psi_ssh.make_ssh_session(ip_address, scaleway_account.base_ssh_port, 'root', None, None, host_auth_key=scaleway_account.base_rsa_private_key) ssh.exec_command('echo "root:%s" | chpasswd' % (new_root_password,)) ssh.exec_command('useradd -M -d /var/log -s /bin/sh -g adm %s' % (stats_username)) ssh.exec_command('echo "%s:%s" | chpasswd' % (stats_username, new_stats_password)) ssh.exec_command('rm /etc/ssh/ssh_host_*') ssh.exec_command('rm -rf /root/.ssh') ssh.exec_command('export DEBIAN_FRONTEND=noninteractive && dpkg-reconfigure openssh-server') return ssh.exec_command('cat /etc/ssh/ssh_host_rsa_key.pub')
def update_system_packages(digitalocean_account, ip_address): """ Updates system packages using apt. This should only be used when updating the base image. digitalocean_account : DigitalOcean account details ip_address : droplet.ip_address """ ssh = psi_ssh.make_ssh_session(ip_address, digitalocean_account.base_ssh_port, 'root', None, None, digitalocean_account.base_rsa_private_key) ssh.exec_command('export DEBIAN_FRONTEND=noninteractive && aptitude update -q && aptitude safe-upgrade -y -o Dpkg::Options::="--force-confdef"') ssh.close()
def _change_hostname(self, ssh_info, random_name, reboot): ''' Changes the hostname of the host (ElasticHosts "server") at ip_address. Note that the name change won't actually take place until the server is rebooted. ''' # Note: using base image credentials; call before changing credentials ssh = psi_ssh.make_ssh_session(*ssh_info, verbose=self._verbose) ssh.exec_command('echo "%s" > /etc/hostname' % random_name).strip() if reboot: self._reboot(ssh_info)
def add_swap_file(vps247_account, ip_address): ssh = psi_ssh.make_ssh_session(ip_address, vps247_account.base_ssh_port, 'root', vps247_account.base_root_password, None) ssh.exec_command( 'dd if=/dev/zero of=/swapfile bs=1024 count=1048576 && mkswap /swapfile && chown root:root /swapfile && chmod 0600 /swapfile' ) ssh.exec_command('echo "/swapfile swap swap defaults 0 0" >> /etc/fstab') ssh.exec_command('swapon -a') ssh.close() return
def upload_certs(vps247_account, ip_address): ssh = psi_ssh.make_ssh_session(ip_address, vps247_account.base_ssh_port, 'root', vps247_account.base_root_password, None) ssh.put_file(AUTOMATION_DIR + "/ssl/logs.cert.pem", "/opt/psiphon/certs/logs.cert.pem") ssh.put_file(AUTOMATION_DIR + "/ssl/beats.psiphon3.com.cert.pem", "/opt/psiphon/certs/beats.psiphon3.com.cert.pem") ssh.put_file(AUTOMATION_DIR + "/ssl/beats.psiphon3.com.key.pem", "/opt/psiphon/certs/beats.psiphon3.com.key.pem") ssh.close() return
def refresh_credentials(ramnode_account, ip_address, password, host_public_key, new_root_password, new_stats_password, stats_username): ssh = psi_ssh.make_ssh_session(ip_address, ramnode_account.base_ssh_port, 'root', password, host_public_key) ssh.exec_command('echo "root:%s" | chpasswd' % (new_root_password, )) ssh.exec_command('useradd -M -d /var/log -s /bin/sh -g adm %s' % (stats_username)) ssh.exec_command('echo "%s:%s" | chpasswd' % (stats_username, new_stats_password)) ssh.exec_command('rm /etc/ssh/ssh_host_*') ssh.exec_command('rm -rf /root/.ssh') ssh.exec_command('dpkg-reconfigure openssh-server') return ssh.exec_command('cat /etc/ssh/ssh_host_rsa_key.pub')
def reset_root_password(vps247_account, ip_address, init_pass): ssh = psi_ssh.make_ssh_session(ip_address, 22, 'debian', None, None, vps247_account.base_rsa_private_key) ssh.put_file(AUTOMATION_DIR + "/update_root_password.sh", "/home/debian/update.sh") ssh.put_file(AUTOMATION_DIR + "/base_image_init_native.sh", "/home/debian/native.sh") cmd = 'sh -c "sleep 1; echo ' + init_pass + '" | script -qc "su -c \'bash /home/debian/update.sh\' - root"' ssh.exec_command(cmd) ssh.close() return
def upload_certs(vps247_account, ip_address): ssh = psi_ssh.make_ssh_session(ip_address, vps247_account.base_ssh_port, 'root', vps247_account.base_root_password, None) ssh.put_file(AUTOMATION_DIR + "/ssl/old-ca.pem", "/opt/psiphon/certs/old-ca.pem") ssh.put_file(AUTOMATION_DIR + "/ssl/new-ca.pem", "/opt/psiphon/certs/new-ca.pem") ssh.put_file(AUTOMATION_DIR + "/ssl/client-psiphon3.pem", "/opt/psiphon/certs/client-psiphon3.pem") ssh.put_file(AUTOMATION_DIR + "/ssl/client-psiphon3-key.pem", "/opt/psiphon/certs/client-psiphon3-key.pem") ssh.exec_command("chmod 400 /opt/psiphon/certs/*") ssh.close() return
def refresh_credentials(digitalocean_account, ip_address, new_root_password, new_stats_password): # Note: using auto-add-policy for host's SSH public key here since we can't get it through the API. # There's a risk of man-in-the-middle. ssh = psi_ssh.make_ssh_session(ip_address, digitalocean_account.base_ssh_port, 'root', None, None, digitalocean_account.base_rsa_private_key) ssh.exec_command('echo "root:%s" | chpasswd' % (new_root_password, )) ssh.exec_command( 'echo "%s:%s" | chpasswd' % (digitalocean_account.base_stats_username, new_stats_password)) ssh.exec_command('rm /etc/ssh/ssh_host_*') ssh.exec_command('rm -rf /root/.ssh') ssh.exec_command('dpkg-reconfigure openssh-server') return ssh.exec_command('cat /etc/ssh/ssh_host_rsa_key.pub')
def pave_linode(linode_account, ip_address, password): # Note: using auto-add-policy for host's SSH public key here since we can't get it through the Linode API. # There's a risk of man-in-the-middle. ssh = psi_ssh.make_ssh_session(ip_address, 22, 'root', password, None) ssh.exec_command('mkdir -p /root/.ssh') ssh.exec_command('echo "%s" > /root/.ssh/known_hosts' % (linode_account.base_known_hosts_entry,)) ssh.exec_command('echo "%s" > /root/.ssh/id_rsa' % (linode_account.base_rsa_private_key,)) ssh.exec_command('chmod 600 /root/.ssh/id_rsa') ssh.exec_command('echo "%s" > /root/.ssh/id_rsa.pub' % (linode_account.base_rsa_public_key,)) ssh.exec_command('scp -P %d root@%s:%s %s' % (linode_account.base_ssh_port, linode_account.base_ip_address, linode_account.base_tarball_path, linode_account.base_tarball_path)) ssh.exec_command('apt-get update > /dev/null') ssh.exec_command('apt-get install -y bzip2 > /dev/null') ssh.exec_command('tar xvpfj %s -C / > /dev/null' % (linode_account.base_tarball_path,))
def update_system_packages(digitalocean_account, ip_address): """ Updates system packages using apt. This should only be used when updating the base image. digitalocean_account : DigitalOcean account details ip_address : droplet.ip_address """ ssh = psi_ssh.make_ssh_session(ip_address, digitalocean_account.base_ssh_port, 'root', None, None, digitalocean_account.base_rsa_private_key) ssh.exec_command( 'export DEBIAN_FRONTEND=noninteractive && aptitude update -q && aptitude safe-upgrade -y -o Dpkg::Options::="--force-confdef"' ) ssh.close()
def upgrade_debian_distro(digitalocean_account, ip_address, old_version, new_version): '''upgrade_debian_distro is used to perform a distribution upgrade on a host. ''' ssh = psi_ssh.make_ssh_session(ip_address, digitalocean_account.base_ssh_port, 'root', None, None, digitalocean_account.base_rsa_private_key) ssh.exec_command("cp /etc/apt/sources.list{,.old}") ssh.exec_command("sed -i 's/%s/%s/g' /etc/apt/sources.list" % (old_version, new_version)) ssh.exec_command("sed -i 's/%s/%s/g' /etc/apt/sources.list" % (old_version, new_version)) ssh.exec_command('export DEBIAN_FRONTEND=noninteractive && apt-get update -q && apt-get dist-upgrade -y -f -o Dpkg::Options::="--force-confdef"') ssh.exec_command('apt-get update && apt-get autoremove -y -f') ssh.exec_command('shutdown -r now') ssh.close()
def upgrade_debian_distro(digitalocean_account, ip_address, old_version, new_version): '''upgrade_debian_distro is used to perform a distribution upgrade on a host. ''' ssh = psi_ssh.make_ssh_session(ip_address, digitalocean_account.base_ssh_port, 'root', None, None, digitalocean_account.base_rsa_private_key) ssh.exec_command("cp /etc/apt/sources.list{,.old}") ssh.exec_command("sed -i 's/%s/%s/g' /etc/apt/sources.list" % (old_version, new_version)) ssh.exec_command("sed -i 's/%s/%s/g' /etc/apt/sources.list" % (old_version, new_version)) ssh.exec_command( 'export DEBIAN_FRONTEND=noninteractive && apt-get update -q && apt-get dist-upgrade -y -f -o Dpkg::Options::="--force-confdef"' ) ssh.exec_command('apt-get update && apt-get autoremove -y -f') ssh.exec_command('shutdown -r now') ssh.close()
def set_allowed_users(digitalocean_account, ip_address, password, stats_username): """ Adds user account to AllowUsers config in /etc/ssh/sshd_config digitalocean_account : Digitalocean account details ip_address : droplet IP address password : password to connect to server host_public_key : droplet ssh public key stats_username : user account to add to AllowUsers """ ssh = psi_ssh.make_ssh_session(ip_address, digitalocean_account.base_ssh_port, 'root', None, None, digitalocean_account.base_rsa_private_key) user_exists = ssh.exec_command('grep %s /etc/ssh/sshd_config' % stats_username) if not user_exists: ssh.exec_command( 'sed -i "s/^AllowUsers.*/& %s/" /etc/ssh/sshd_config' % stats_username) ssh.exec_command('service ssh restart')
def set_host_name(linode_account, ip_address, password, host_public_key, new_hostname): # Note: hostnamectl is for systemd servers ssh = psi_ssh.make_ssh_session(ip_address, linode_account.base_ssh_port, 'root', password, host_public_key) ssh.exec_command('hostnamectl set-hostname %s' % new_hostname)
def set_host_name(scaleway_account, ip_address, new_hostname): # Note: hostnamectl is for systemd servers ssh = psi_ssh.make_ssh_session(ip_address, scaleway_account.base_ssh_port, 'root', None, None, host_auth_key=scaleway_account.base_rsa_private_key) ssh.exec_command('hostnamectl set-hostname %s' % new_hostname)
def get_host_name(scaleway_account, ip_address): # Note: using base image credentials; call before changing credentials ssh = psi_ssh.make_ssh_session(ip_address, scaleway_account.base_ssh_port, 'root',None, None, host_auth_key=scaleway_account.base_rsa_private_key) return ssh.exec_command('hostname').strip()
def update_kernel(digitalocean_account, do_mgr, droplet): """ This updates the kernel to use the same one as provided in the apt system packages. digitalocean_account : DigitalOcean account information do_mgr : digitalocean.Manager droplet : droplet details. Gathered from droplet.load() returns: droplet : droplet details. """ current_kernel_name = None ssh = psi_ssh.make_ssh_session(droplet.ip_address, digitalocean_account.base_ssh_port, 'root', None, None, digitalocean_account.base_rsa_private_key) droplet_kernel_pkg = ssh.exec_command( 'aptitude show linux-image-`uname -r`').split('\n') droplet_uname = ssh.exec_command('uname -r').strip() if len(droplet_kernel_pkg) > 0: for line in droplet_kernel_pkg: if 'State: installed' in line: print line if 'Version: ' in line: print line current_kernel_name = line.split(': ')[1].split('+')[0] break if not current_kernel_name: raise Exception('Current Kernel version is not found') droplet_kernels = droplet.get_kernel_available() new_kernel = None if current_kernel_name not in droplet.kernel['name']: for kernel in droplet_kernels: if current_kernel_name in kernel.name and droplet_uname == kernel.version: print 'Kernel found. ID: %s, Name: %s' % (kernel.id, kernel.name) new_kernel = kernel break if new_kernel: print 'Change to use new kernel. ID: %s' % (new_kernel.id) result = droplet.change_kernel(new_kernel) if not wait_on_action(do_mgr, droplet, result['action']['id'], 30, 'change_kernel', 'completed'): raise Exception('Event did not complate on time') droplet = droplet.load() result = droplet.power_cycle() print result if not wait_on_action(do_mgr, droplet, result['action']['id'], 30, 'power_cycle', 'completed'): raise Exception('Event did not complete in time') droplet = droplet.load() if droplet.status != 'active': result = droplet.power_on() if not wait_on_action(do_mgr, droplet, result['action']['id'], 30, 'power_on', 'completed'): raise Exception('Event did not complete in time') droplet = droplet.load() return droplet
def get_host_name(linode_account, ip_address): # Note: using base image credentials; call before changing credentials ssh = psi_ssh.make_ssh_session(ip_address, linode_account.base_ssh_port, 'root', linode_account.base_root_password, linode_account.base_host_public_key) return ssh.exec_command('hostname').strip()
def update_system_packages(digitalocean_account, ip_address): ssh = psi_ssh.make_ssh_session(ip_address, digitalocean_account.base_ssh_port, 'root', None, None, digitalocean_account.base_rsa_private_key) ssh.exec_command('export DEBIAN_FRONTEND=noninteractive && aptitude update -q && aptitude safe-upgrade -y -o Dpkg::Options::="--force-confdef"')