Python Forum
ssh remote command with list
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ssh remote command with list
#1
Hello I am new to python. trying to use a list to ssh to my remote server

#!/usr/bin/python
import subprocess

node = ['node41', 'node42', 'node52']
link = ['eth0', 'eth0', 'eht0']

for i in range(len(node)):
       print node[i], s16d1[i]
       subprocess.call(["ssh", node[i], 'ip link show s16d1[i]'])
getting his error
Error:
Device "link[i]" does not exist.
How can I pass device link from the list ssh remote command?

thanks,
Reply
#2
Quote:How can I pass device link from the list ssh remote command?

In shell you do it via:
ssh host "command"
Command is one argument.

In Python you've to use a list. Each element is a argument, the first argument is the binary itself.

command = ['ssh', 'node', 'ip link show eth0']
stdout = subprocess.check_output(command) 
I think you're using Public-Key authentication. If you want to use password authentication, you have to do it different. There is a module for it: http://www.paramiko.org/

Finally this should work with Python 2.7 and Python 3.x:
#!/usr/bin/python
from __future__ import print_function
# to be compatible with Python 3.x, print is since Python 3 a function and no longer a statement
from subprocess import check_output
# using check_output instead of using Popen
# call returns the exit code


def get_links(nodes, links):
   for node, link in zip(nodes, links):
        # zip can zip lists together
        # and assigned to node, link 
        # now format the command for the target host
        ip_command = 'ip link show {}'.format(link)
        # finally make a list for the ssh command
        command = ['ssh', node, ip_command]
        # save output to stdout
        stdout = check_output(command)
        # just print it. You can process also the output
        print(stdout.decode())
        # or
        # return stdout
 

def main():
    # use plural for lists and not singular
    nodes = ['node41', 'node42', 'node52']
    links = ['eth0', 'eth0', 'eht0']
    get_links(nodes, links)


if __name__ == '__main__':
    main()
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Thanks DeaD_EyE!
You hint of "ip_command = 'ip link show {}'.format(link)" helped

#!/usr/bin/python
import subprocess

node = ['node41', 'node42', 'node52']
link = ['eth0', 'eth0', 'eht0']

for i in range(len(node)):
     ip_command = 'ip link show {}'.format(link[i])
       print ip_command
       subprocess.call(["ssh", node[i], ip_command])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Passing List of Objects in Command Line Python usman 7 3,187 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  Select correct item from list for subprocess command pythonnewbie138 6 3,308 Jul-24-2020, 09:09 PM
Last Post: pythonnewbie138
  python if command for a list fid 3 2,009 May-08-2020, 11:52 AM
Last Post: fid
  Add list item into executable command zhome888 2 2,131 Dec-19-2019, 12:23 AM
Last Post: zhome888
  How to use list (structure) in the SQL Select command? pyuser1 2 2,931 Apr-27-2018, 02:33 PM
Last Post: pyuser1
  How to find the list of dependencies not present in the remote artifactory repository csplrj 6 4,715 Mar-30-2018, 03:43 PM
Last Post: csplrj
  Saving shell command into a List steackfrite 4 3,946 Aug-31-2017, 11:28 AM
Last Post: steackfrite

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020