gpt4 book ai didi

java - 如何使用java在shell脚本中传递用户名和密码

转载 作者:行者123 更新时间:2023-11-30 03:36:57 24 4
gpt4 key购买 nike

我有一个 shell 脚本。现在我想使用 java 在此脚本中传递 NAMEPASS 。然后我想使用java执行它。下面我在这里编写了我的 shell 脚本以及 java 代码。

请帮帮我。

#!/bin/bash

# Create ftp user, create folders and set permissions
# Usage: ./create_ftp_user.sh [username] "[password]"
#

NAME=bimal
PASS=bimal

echo "USAGE: create_ftp_user.sh [username] [password]"

# check input parameters
if [ -z "$NAME" ]; then
echo "Error: username is not set"
exit
fi

if [ -z "$PASS" ]; then
echo "Error: password not set"
exit
fi

# create system user
echo "Creating user: $NAME"
echo "With password: $PASS"

useradd -p `openssl passwd -1 $PASS` -m $NAME -g ftpaccess -s /usr/sbin/nologin

# save to users log
echo "user: $NAME, pass: $PASS" >> new_ftp_users_list

# add user to ftp daemon list
echo "$NAME" >> /etc/vsftpd/chroot_list

# create user ftp dir
mkdir /var/ftpupload/$NAME

# Set Ownership
chown $NAME: /var/ftpupload/$NAME

# Set permissions
chmod 0777 /var/ftpupload/$NAME

# restart vsftp daemon
#/etc/init.d/vsftpd restart
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package testscript;

/**
*
* @author deepak
*/
import java.io.IOException;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;

public class TestScript {
int iExitValue;
String sCommandString;

public void runScript(String command){
sCommandString = command;
CommandLine oCmdLine = CommandLine.parse(sCommandString);
DefaultExecutor oDefaultExecutor = new DefaultExecutor();
oDefaultExecutor.setExitValue(0);
try {
iExitValue = oDefaultExecutor.execute(oCmdLine);
} catch (ExecuteException e) {
// TODO Auto-generated catch block
System.err.println("Execution failed.");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("permission denied.");
e.printStackTrace();
}
}

public static void main(String args[]){
TestScript testScript = new TestScript();
testScript.runScript("gksudo sh /home/deepak/Desktop/ftpusers.sh");
}
}

最佳答案

CommandLine 可用于设置 shell 参数:

CommandLine oCmdLine = new CommandLine(sCommandString);
oCmdLine.addArgument("bimal");
oCmdLine.addArgument("bimalPassword");

您可以引用 Apache CommandLine 上的各种选项文档页面。

完成后,参数将在 shell 脚本中作为 $1 和 $2 位置 shell 参数:

NAME=$1
PASS=$2

关于java - 如何使用java在shell脚本中传递用户名和密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27658041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com