使用 Groovy 连接 linux sFTP 服务器
Connecting with linux sFTP server using Groovy
我正在尝试 运行 以下 Groovy 脚本,这些脚本旨在将 linux 服务器上的文件权限更改为 777 -
@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[=14=].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
有人可以帮我解决这个问题吗?
谢谢!
看到这一行:
"chmod 777".execute(null, new File("WORKING DIRECTORY\Test_ftpuser_place.txt"))
"execute" 方法中的第二个参数表示当前工作目录(参见文档 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)
我正在尝试 运行 以下 Groovy 脚本,这些脚本旨在将 linux 服务器上的文件权限更改为 777 -
@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[=14=].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
有人可以帮我解决这个问题吗?
谢谢!
看到这一行:
"chmod 777".execute(null, new File("WORKING DIRECTORY\Test_ftpuser_place.txt"))
"execute" 方法中的第二个参数表示当前工作目录(参见文档 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)