Hello all , I just need to create a folder in an other machine using ssh or samba ; for example i execute :

os.mkdir( rachid@rachid:/home/rachid/Bureau/new, 0777 );

but It shows the next error :

OSError: [Errno 2] No such file or directory: 'rachid@rachid:/home/rachid/Bureau/new'

Thank you for answering .

hello ismatus...... you can try pexpect module to do the same thing......try it....

Hello sun , thank you for answering , can you give an exemple about how to use this module please , i search right now to see how it is working .

Thank you for answering , but i couldn't understand till now how to deal with that ..

Maybe this code could help you.....you need to download the pexpect module....

try:
        enter = "Are you sure you want to continue connecting"
        password_type ="password:"
        p=pexpect.spawn("ssh 192.168.101.101 folder_name")

        i=p.expect([enter, password_type, pexpect.EOF])

        if i==0:
                p.sendline("yes")
                i=p.expect([enter, password_type, pexpect.EOF])

        if i==1:
                p.sendline("123456")
                i=p.expect([enter, password_type, pexpect.EOF])

        elif i==2:
                print "Error...."
                sys.exit(0)

except Exception as error:
                print error
                sys.exit(0)

Oh sun , thank you for your efforts , i am bigining now from down level , so i just tried :

sudo scp fichiertest.py user@10.100.8.22:/home/user/Bureau

, and it works fine after demanding our two passwords . Now i am trying to creat a new folder in that machine so i tried after searching :

sudo ssh user@10.100.8.22:/home/user/Bureau/new

, but i sent me this error message :

ssh: Could not resolve hostname 10.100.8.22:/home/hatim/Bureau/new: Name or service not known

, thank you infinitly friend sun .

No need to thank, you also helped me frnd....
you dont have to give colon after the ip in ssh.....

so your command sld be...

sudo ssh user@10.100.8.22 "mkdir /home/user/Bureau/new"

this will work...try it..

:) , ok i will try now .

Thank you again sun , i can not not do it , it worked perfectly , i till you my next problem in few time :).

Hello Sun , you know i think that the function :

tkFileDialog.askdirectory()

must show from the begining the network , not only local folders ...
well , your command worked very well , but i can not apply this for python code :

#!/usr/bin/python
#-*- coding:Utf-8 -*-
import os
import time
import sys

source = []
# 1. Les fichiers et repertoires a sauver sont indiques dans une liste.

print sys.argv

for i in range(len(sys.argv)):
          source.append(sys.argv[i])

source.remove(source[0])
source.remove(source[0])

print source
print sys.argv[1]

#source = ['/home/ismail/Bureau/Doc_Unix' ]

# Notez qu'il faut utiliser des double quotes a l'interieur la chaine pour les noms de fichiers avec des espaces.
 # 2. la sauvegarde est rangee dans un repertoire a part.

target_dir = sys.argv[1]

#target_dir = '/home/ismail/Bureau/Testfolder'
 
 # 3. Les fichiers sont places dans une archive zip.
# 4. Le jour courant est le nom du sous-repertoire dans le repertoire principal

today = target_dir + os.sep + time.strftime('%Y%m%d')

# L'heure courante est le nom de l'archive zip

now = time.strftime('%H%M%S')

 # Creer le sous-repertoire s'il n'existe pas

if not os.path.exists(today):
    os.mkdir(today) # cree le repertoire
     
print 'Sauvegarde reussie vers', today

 # Le nom du fichier zip

target = today + os.sep + now + '.zip'
 
# 5. Nous utilisons la commande zip pour creer une archive
 
zip_command = "zip -qr {0} {1}".format(target, ' '.join(source))
scp_command = "scp {1}.zip {0}".format(target, ' '.join(source))
 
# lancement de la sauvegarde

if os.system(zip_command) == 0:
    print 'Sauvegarde reussie vers', target
    
else:
    print 'Echec de la sauvegarde'

Can you explain to me plz , the problem is in line 42 :

os.mkdir(today) # cree le repertoire

, to explain to you more what i am trying to do ; the application create a crontab line like :

00 17 01 30 * python /home/ismail/Bureau/SaveProg/Backup.py  /home/ismail/Bureau/Boulot  /home/ismail/Bureau/Boulot

, it works in locale , and i just need to execute this Backup.py with destination argument in network , so now , i just need to execute this :

00 17 01 30 * python /home/ismail/Bureau/SaveProg/Backup.py  usertest@10.100.8.22:/home/usertest/Bureau/  /home/ismail/Bureau/Boulot

, that will save the folder Boulot in usertest@10.100.8.22:/home/usertest/Bureau/ , and before this Backup.py have to create a folder : usertest@10.100.8.22:/home/usertest/Bureau/20120131 , but this don't work , i can not create this last folder in netwrk ...

it is very interesting problem.....if you dont mind me trying this in my own way, by using pexpect and making remote computer password less......it would be a interesting thing to do, that will do for my own, n post here........ this approch i cant able to understand because comments in the problem are in your local language not in english..so if you convert those in english and post here,,,,,

Good morning Sun ,ok , i try to translate :) : this is the new code :

#!/usr/bin/python
#-*- coding:Utf-8 -*-
import os
import time
import sys

source = []
# 1. Files and directories to save are indicated in a list.

print sys.argv

for i in range(len(sys.argv)):
          source.append(sys.argv[i])

source.remove(source[0])
source.remove(source[0])

print source
print sys.argv[1]

#source = ['/home/ismail/Bureau/Doc_Unix' ]

# Note that you must use double quotes inside a string for the file names with spaces.
 # 2. the backup is stored in a separate folder.

target_dir = sys.argv[1]

#target_dir = '/home/ismail/Bureau/Testfolder'
 
 # 3. The files are placed inside the archive zip .
# 4.The current day is the name of the subdirectory in the directory

today = target_dir + os.sep + time.strftime('%Y%m%d')

#The current time is the name of the zip archive

now = time.strftime('%H%M%S')

 # Create the subdirectory if it does not

if not os.path.exists(today):
    os.mkdir(today) # cree le repertoire
     
print 'Sauvegarde reussie vers', today

 # Le nom du fichier zip

target = today + os.sep + now + '.zip'
 
# 5. We use the zip command to create an archive
 
zip_command = "zip -qr {0} {1}".format(target, ' '.join(source))
scp_command = "scp {1}.zip {0}".format(target, ' '.join(source))
 
# start backup

if os.system(zip_command) == 0:
    print 'Successful backup to', target
    
else:
    print 'Failed to save'

You know , till now I could , in a test script , it can create a folder with some name in a distant machine giving like arguments for example : ./test.py user@10.100.8.22 , with this code for example :

#!/usr/bin/python
#-*- coding:Utf-8 -*-
import os
import time
import sys

source = []
print sys.argv
for i in range(len(sys.argv)):
          source.append(sys.argv[i])

source.remove(source[0])
source.remove(source[0])

print source
print sys.argv[1]

target_dir = sys.argv[1]
today = target_dir + os.sep + time.strftime('%Y%m%d')
now = time.strftime('%H%M%S')

if sys.argv[1][0] == "/" :
   print sys.argv[1] + "local"
   if not os.path.exists(today):
      os.mkdir(today) # cree le repertoire
else :
   print sys.argv[1] + "network"
   os.system('sudo ssh %s "mkdir /home/hatim/Bureau/testfolder"' % (sys.argv[1])) 
        
print 'Sauvegarde reussie vers', today

, is it good ? ;) .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.