diff options
author | vmaffione <v.maffione@gmail.com> | 2017-04-15 07:34:21 +0000 |
---|---|---|
committer | vmaffione <v.maffione@gmail.com> | 2017-04-15 07:34:21 +0000 |
commit | 28d6a8729fac3d109e68afed1bbacc27d526048b (patch) | |
tree | cacdb7db6e44712c0a4ca2dc617afb355bc2c852 /rumba/ssh_support.py | |
parent | ca1d77df271defb08d5f73b54398491d1049c9f9 (diff) | |
parent | 6eceae4bf7ee823d6eed276935741b7c107f6105 (diff) | |
download | rumba-28d6a8729fac3d109e68afed1bbacc27d526048b.tar.gz rumba-28d6a8729fac3d109e68afed1bbacc27d526048b.zip |
Merge branch 'master-marco' into 'master'
IRATI config file generation (and other)
See merge request !22
Diffstat (limited to 'rumba/ssh_support.py')
-rw-r--r-- | rumba/ssh_support.py | 96 |
1 files changed, 79 insertions, 17 deletions
diff --git a/rumba/ssh_support.py b/rumba/ssh_support.py index e66db43..0179c5d 100644 --- a/rumba/ssh_support.py +++ b/rumba/ssh_support.py @@ -17,9 +17,10 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301 USA - +import os import paramiko + def get_ssh_client(): ssh_client = paramiko.SSHClient() ssh_client.load_system_host_keys() @@ -27,6 +28,7 @@ def get_ssh_client(): return ssh_client + def _print_stream(stream): o = str(stream.read()).strip('b\'\"\\n') if o != "": @@ -35,6 +37,7 @@ def _print_stream(stream): print(oi) return o + def execute_commands(testbed, ssh_config, commands, time_out=3): ''' Remote execution of a list of shell command on hostname. By @@ -77,6 +80,7 @@ def execute_commands(testbed, ssh_config, commands, time_out=3): print(str(e)) return + def execute_command(testbed, ssh_config, command, time_out=3): ''' Remote execution of a list of shell command on hostname. By @@ -95,6 +99,7 @@ def execute_command(testbed, ssh_config, command, time_out=3): if o != None: return o + def copy_file_to_testbed(testbed, ssh_config, text, file_name): ''' Write a string to a given remote file. @@ -137,6 +142,58 @@ def copy_file_to_testbed(testbed, ssh_config, text, file_name): except Exception as e: print(str(e)) + +def copy_paths_to_testbed(testbed, ssh_config, paths, destination): + ''' + Write a string to a given remote file. + Overwrite the complete file if it already exists! + + @param testbed: testbed info + @param ssh_config: ssh config of the node + @param paths: source paths (local) as an iterable + @param destination: destination folder name (remote) + ''' + ssh_client = get_ssh_client() + + if destination is not '' and not destination.endswith('/'): + destination = destination + '/' + + try: + ssh_client.connect(ssh_config.hostname, ssh_config.port, + testbed.username, + testbed.password, + look_for_keys=True) + + sftp_client = ssh_client.open_sftp() + + for path in paths: + file_name = os.path.basename(path) + dest_file = destination + file_name + print("Copying %s to %s@%s:%s path %s" % ( + path, + testbed.username, + ssh_config.hostname, + ssh_config.port, + dest_file)) + sftp_client.put(path, dest_file) + + except Exception as e: + print(str(e)) + + +def copy_path_to_testbed(testbed, ssh_config, path, destination): + ''' + Write a string to a given remote file. + Overwrite the complete file if it already exists! + + @param testbed: testbed info + @param ssh_config: ssh config of the node + @param path: source path (local) + @param destination: destination folder name (remote) + ''' + copy_paths_to_testbed(testbed, ssh_config, [path], destination) + + def setup_vlan(testbed, node, vlan_id, int_name): ''' Gets the interface (ethx) to link mapping @@ -146,22 +203,27 @@ def setup_vlan(testbed, node, vlan_id, int_name): @param vlan_id: the VLAN id @param int_name: the name of the interface ''' + if testbed.username == 'root': + def sudo(s): + return s + else: + def sudo(s): + return 'sudo ' + s + print("Setting up VLAN on node " + node.name) - cmds = list() - cmd = "sudo ip link add link " + \ - str(int_name) + \ - " name " + str(int_name) + \ - "." + str(vlan_id) + \ - " type vlan id " + str(vlan_id) - cmds.append(cmd) - cmd = "sudo ifconfig " + \ - str(int_name) + "." + \ - str(vlan_id) + " up" - cmds.append(cmd) - cmd = "sudo ethtool -K " + \ - str(int_name) + " rxvlan off" - cmds.append(cmd) - cmd = "sudo ethtool -K " + \ - str(int_name) + " txvlan off" + args = {'ifname': str(int_name), 'vlan': str(vlan_id)} + + cmds = [sudo("ip link set %(ifname)s up" + % args), + sudo("ip link add link %(ifname)s name " + "%(ifname)s.%(vlan)s type vlan id %(vlan)s" + % args), + sudo("ifconfig %(ifname)s.%(vlan)s up" + % args)] + # TODO: is ethtool needed? Should install or check if it is present. + # cmds += [sudo("ethtool -K %(ifname)s rxvlan off" + # % args), + # sudo("ethtool -K %(ifname)s txvlan off" + # % args)] execute_commands(testbed, node.ssh_config, cmds) |