2017-09-29 46 views
0

我試圖運行的打算在Linux服務器上修改文件權限爲777以下Groovy腳本的Linux SFTP服務器建立連接 -與使用Groovy

@GrabConfig(systemClassLoader = true) 
@Grab(group="com.jcraft", module="jsch", version="0.1.46") 
import com.jcraft.jsch.*; 
import com.jcraft.jsch.Channel; 
import com.jcraft.jsch.ChannelSftp; 
import com.jcraft.jsch.ChannelSession; 
import java.io.InputStream; 
import java.io.File; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.util.Vector;  
java.util.Properties config = new java.util.Properties() 

config.put "StrictHostKeyChecking", "no" 
JSch ssh = new JSch(); 
Session session = null; 
Session sess = ssh.getSession ("USERNAME", "HOST", 22); 
sess.with { 
setConfig config 
setPassword ("PASSWORD"); 
connect() 
Channel chan = openChannel ("sftp"); 
chan.connect() 
ChannelSftp sftp = (ChannelSftp) chan; 

"chmod 777".execute(null, new File("WORKING DIRECTORY\Test_ftpuser_place.txt")) 

chan.disconnect() 
disconnect() 
} 

此外,我試着用下面的命令而不是Chmod,但仍然無效。

builder = new AntBuilder() 
builder.chmod(dir:"WORKING DIRECTORY", perm:'+rwxrwxrwx', includes:'Test_ftpuser.txt') 

並即時得到上運行腳本的前半部分這個錯誤 -

java.io.IOException: Cannot run program "chmod": CreateProcess error=2, The system cannot find the file specified 

    at java_lang_Runtime$exec$0.call(Unknown Source) 

    at ConsoleScript45$_run_closure1.doCall(ConsoleScript45:45) 

    at ConsoleScript45.run(ConsoleScript45:18) 

Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified 

    ... 3 more 

可能有人請幫我出這一點。

謝謝!

回答

0

參見這一行:

「搭配chmod 777」 .execute(空,新的文件( 「工作目錄\ Test_ftpuser_place.txt」))

第二個參數中的 「執行」方法表示當前工作目錄(請參閱文檔here)。您正在使用它來表示您要更改的文件,我不認爲它是用於此目的的文件。

嘗試先創建文件,然後更改其權限。您也可以使用methods on the File object來設置這些,而不必使用「process」.execute():

def myFile = new File("path/to/file") 
myFile.write("Hello World") 
myFile.setReadable(true, false) 
myFile.setWritable(true, false) 
myFile.setExecutable(true, false)