Question

im trying to make a backup toll that reads data from xml file and make a copy of the back up file the xml file contains all the device data like so:

<data>
    <devices>
        <device name='cisco' ip='10.10.10.10' uname='username' password='passpass' backup_make='Write memory' backup_location_device='nvram:/startup-config' backup_location='idan/backup_fiels/backup1.cfg' ></device>     
        </devices>
</data> 

i use the data to connect in ssh like so, its working good :

xmldoc = minidom.parse('xmlfile.xml')
devicelist = xmldoc.getElementsByTagName('device')

    address=('ssh ',devicelist[0].attributes['uname'].value,'@',devicelist[0].attributes['ip'].value)
    password = (devicelist[0].attributes['password'].value)

    ssh_device = pexpect.spawn(address)
    ssh_device.expect('.assword:')
    .
    .
    .

but when i do the same for scp im geting an error

backup = ('scp ',devicelist[0].attributes['uname'].value,'@',devicelist[0].attributes['ip'].value,':',devicelist[0].attributes['backup_location_device'].value,' test.cfg')

ssh_device = pexpect.spawn(backup)
ssh_device.expect('.assword:')
.
.
.

when im running the script:

Traceback (most recent call last):
  File "xmltest.py", line 34, in <module>
    backup_make = pexpect.spawn(backup)
  File "/usr/local/lib/python2.7/site-packages/pexpect/__init__.py", line 485, in __init__
    self._spawn(command, args)
  File "/usr/local/lib/python2.7/site-packages/pexpect/__init__.py", line 590, in _spawn
    'executable: %s.' % self.command)
pexpect.ExceptionPexpect: The command was not found or was not executable: scp Netadmn9@10.10.10.10:nvram:/startup-config test.cfg.

and the floowing command is working too:

backup = ('scp Netadmn9@10.106.11.111:nvram:/startup-config HSBU1a1_idan.cfg')

ssh_device = pexpect.spawn(backup)
ssh_device.expect('.assword:')
.
.
.

can any one help ? thanks !

Was it helpful?

Solution

Pexpect is expecting a command and then a list of arguments, so it needs breaking up.

try:

backup = "scp %s@%s:%s test.cfg" % (devicelist[0].attributes['uname'].value,devicelist[0].attributes['ip'].value,devicelist[0].attributes['backup_location_device'].value)

split_command = shlex.split(backup)
ssh_device = pexpect.spawn(split_command)

On a side note, if you setup public private key login then you wont need pexpect to do the backup you can use subprocess call.

read here.

split_command = shlex.split(backup)
subprocess.call(split_command)

I have added a string format line instead of the tuple you generated.

This should be sensibly split by shlex.split()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top