1

i am using paramiko of python to manipulate access remote linux machine. My command "mount device dir" is failing with " No such file or directory", even though exact the same command succeeds once i use it remotely (connected via ssh, not via paramiko).

I have tried to vary /etc/fstab to some values, again, same situation. Once i type it via ssh - ok, the same command via paramiko - above error message.

Any ideas?

example on command (changed minimally from origin):

        import paramiko
        self.ssh = paramiko.SSHClient()
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.ssh.connect('192.168.1.1', username='root', password='passwd')
        stdin, stdout, stderr = self.ssh.exec_command("/bin/mount /dev/sda1")

gives me an error:

 mount /dev/sda1 failed: mount: mounting /dev/sda1 on /media/card failed: No such file or directory

contents from /etc/fstab:

/dev/sda1       /media/card          vfat      fmask=0000,dmask=0000  0  0

of course, /media/card directory exists. again, i can use above command manually via ssh and it works as expected.

update. meanwhile i tried fabric library of python (built on paramiko), exactly as described in Python - How do I authenticate SSH connection with Fabric module?

c = fabric.Connection(host = '192.168.1.1', user = "root", connect_kwargs={'password': 'passwd'})  
c.run("/bin/mount /dev/sda1")  

giving me exactly the same error message as with paramiko directly.

update2. well, as a matter of working around, i mounting drive using direct ssh call, as suggested below in comments. after i do in code whatever necessary, i try to unmount drive using "normal" paramiko call:

self.ssh.exec_command("/bin/umount /dev/sda1")

and it works. so now i am completely lost, mount as above is failing, but unmount is working. this is real strange..

update3. i have tried to extra set LD_LIBRARY_PATH to location of mount's libraries, it needs both libm.so.6 and libc.so.6, both located in /lib like:

self.ssh.exec_command("export LD_LIBRARY_PATH=/lib:/usr/lib && /bin/mount /dev/sda1")

yet no success again.

etwas77
  • 514
  • 1
  • 6
  • 17
  • 2
    Can you share the command & your paramiko code? – rdas Apr 01 '19 at 10:47
  • i added minimal example and also an error and line in /etc/fstab for clarity – etwas77 Apr 01 '19 at 11:22
  • I do not have enough experience with `mount` to give a definitive answer. --- But it's quite likely that the root cause is the same as here: [Getting “sh: sesu: not found” error, when trying to run sesu command using Python Paramiko exec_command](https://stackoverflow.com/q/55419330/850848) --- Test doing `ssh root@192.168.1.1 mount /dev/sda1` – Martin Prikryl Apr 01 '19 at 11:42
  • I didn't mean the path to `mount` (your edit `/bin/mount`) -- I've meant an effect of a profile in general. Did you test the `ssh` command, as I've suggested? – Martin Prikryl Apr 01 '19 at 12:00
  • i tested "ssh root@192.168.1.1 mount /dev/sda1" - works as a charm. do you suggest to call it via system call, not via paramiko? – etwas77 Apr 02 '19 at 08:57
  • No, I'm not suggesting that. Did you call that command on the same machine that runs your Python code? Did you use password authentication? (as with Python) – Martin Prikryl Apr 02 '19 at 09:02
  • yes, i did call it from server side, so to say and it asked me for root password, after i typed it in - mount succeeded. – etwas77 Apr 02 '19 at 09:05
  • What do you mean *server-side*? I assume that you run your Python code on *client-side* (on your *local machine*). – Martin Prikryl Apr 02 '19 at 09:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/191073/discussion-between-etwas77-and-martin-prikryl). – etwas77 Apr 02 '19 at 09:12
  • 1
    Even your new code shows IP `192.168.1.1`, while your `ssh` test uses `192.168.201.220`. – Martin Prikryl Apr 02 '19 at 10:54
  • like it really matters, what really ip i use. you miss the point. point is mount command via paramiko fails, due to unknown "missing file or directory", although all paths are ok and exactly the same command on command prompt on target machine works as intended. – etwas77 Apr 03 '19 at 07:17
  • Well, it looks like you are connecting to a different host using Python than you do with `ssh`. That's my point. – Martin Prikryl Apr 03 '19 at 07:25
  • again, you missing the point of my question. be sure all values are ok. i am just not using real ones here in this example. but code is ok and is working (except mount). – etwas77 Apr 03 '19 at 07:49
  • Can you provide `ls -l /media/card` executed as root on `192.168.1.1`? – gbajson Apr 12 '19 at 11:51
  • yes, it works as intended, no problem with "ls" – etwas77 Apr 15 '19 at 06:04

1 Answers1

0

I was able to get this to work (first draft. Also, I am new to python). Anyway, here is a snip of my code.

The biggest hang-up for me was that it seems as though there is a 4->1 requirement for back slashes in the windows hostname.

Make sure you have a share from the windows PC first. My computer/share name in this case is "COMP_NAME/SHARE_NAME" The username/password provided are your window creds for accessing the share.

import sys
import paramiko
import constant


### START ###############################################################################
# connect to a GW device
# GW: hostname to connect to
# return: client connection object 
def connectToClient(GW):

    try:
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(GW, username=constant.GW_USER, password=constant.GW_PASS)
    except:
        print("Unexpected error:", sys.exc_info()[0])
        return None

    return client
### END ################################################################################


### START ###############################################################################
# execute a command on the remote device
# client: client connection object to the GW
# cmd: the command to execute
#   eg. 'ls -l'
# return: nothing (TODO: maybe return error info)
def exec(client, cmd):
    stdin, stdout, stderr = client.exec_command(cmd)

    for line in stdout:
        print(line.strip('\n'))

    #for line in stderr:
    #    print(line.strip('\n'))

    return
### END #################################################################################

# other stuff
# .
# .
# .

##########################################
# Start - upload the self extracting file to the GW
##########################################
#create the mount point
exec(client, "sudo mkdir /mnt/remote_files")

#mount the source directory (4 to 1 for the back slash chars in the UNC address ...)
exec(client, "sudo mount -t cifs -o username=oxxxxxxp,password=cxxxxxxxxx0 \\\\\\\\COMP_NAME\\\\SHARE_NAME /mnt/remote_files")

#copy the script file
exec(client, "cp /mnt/remote_files/selfextract.bsx rtls/scripts/selfextract.bsx")

#unmount the remote source
exec(client, "sudo umount /mnt/remote_files")

##########################################
# Done - upload the self extracting file to the GW
##########################################

# other stuff
# .
# .
# .

Hope this helps someone..

Pat

Pat
  • 1