通过不带SMB协议的Java代码连接到Windows EC2实例

时间:2019-03-20 08:54:53

标签: java windows smb winrm cifs

我正在尝试通过带有CIFS或WinRM的Java代码连接AWS Windows实例,而不必使用SMB协议。 我不确定是否可能

在实例上运行WinRM服务,为了进行测试,我允许所有流量作为实例的入站。

我想在此实例上复制一些文件,也想使用此Java代码运行一些bash脚本。

我已经尝试过 xebialabs/overthere 和CIFS。

以下我尝试实现的代码库,但是与此相关(所有主机属性都存在)

    ConnectionOptions options = new ConnectionOptions();

    options.set(ConnectionOptions.OPERATING_SYSTEM, OperatingSystemFamily.WINDOWS);
    options.set(CifsConnectionBuilder.CONNECTION_TYPE, CifsConnectionType.WINRM_INTERNAL);
    options.set(CifsConnectionBuilder.WINRM_TIMEMOUT, "PT172800.000S");
    options.set(ConnectionOptions.CONNECTION_TIMEOUT_MILLIS, 900000);
    options.set(ConnectionOptions.SOCKET_TIMEOUT_MILLIS, 900000);
    options.set(CifsConnectionBuilder.WINRM_ENABLE_HTTPS, false);
    options.set(CIFS_PORT, CIFS_PORT_DEFAULT);
    options.set(ConnectionOptions.PROTOCOL, CIFS_PROTOCOL);

    // Host Properties
    options.set(ConnectionOptions.ADDRESS, host);
    options.set(ConnectionOptions.USERNAME, user);
    options.set(ConnectionOptions.PASSWORD, password);

    AddressPortMapper mapper = DefaultAddressPortMapper.INSTANCE;
    OverthereFile workingDirectory = null;
    WinrsConnection connection = new WinrsConnection(options, mapper, workingDirectory);

    connection.connect();

错误:

  

线程“主”中的异常java.lang.IllegalArgumentException:无法与未运行Windows的Winrs代理创建smb:winrm_native连接       在com.xebialabs.overthere.winrs.WinrsConnection.connect(WinrsConnection.java:89)       在ot.WinRmUtils.getWinRmConnection(WinRmUtils.java:76)       在ot.WinRmUtils.getNewWinRMConnection(WinRmUtils.java:89)       在ot.OtPOC.main(OtPOC.java:16)

我也尝试过cloudsoft/winrm4j,但这也不起作用。

1 个答案:

答案 0 :(得分:0)

在配置中,您正在配置WinrsConnection,而在实例化Overthere.getConnection(CifsConnectionBuilder.CIFS_PROTOCOL, config)时。首选使用工厂方法WinrmConnection,或直接实例化WinrsConnection

CifsConnectionType.WINRM_NATIVE(或winrs.exe)仅适用于Windows-2-Windows连接,因为它使用WinrmConnection命令执行WinRM命令。

CifsConnectionType.WINRM_INTERNAL(或+ (UIImage *)image:(UIImage *)image withFilterName:(NSString *)filterName { if (!image) return nil; CIImage *beginImage = [[CIImage alloc] initWithCGImage:image.CGImage]; CIContext *context = [CIContext contextWithOptions:nil]; CIFilter *filter = [CIFilter filterWithName:@"CIPhotoEffectProcess"]; [filter setValue:beginImage forKey:kCIInputImageKey]; CIImage *outputImage = [filter outputImage]; CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]]; UIImage *newImage = [UIImage imageWithCGImage:cgimg]; beginImage = nil; context = nil; filter = nil; outputImage = nil; CGImageRelease(cgimg); return newImage; } )适用于所有连接,因为它是完全用Java实现的。