在Ubuntu上安装和配置SFTP服务器的Shell脚本已损坏

时间:2015-05-19 01:41:06

标签: shell

我正在尝试创建一个shell脚本来自动设置SFTP服务器,但我遇到了一些问题,如代码中所述。

#!/bin/bash

sftp_user='sftpuser'
sftp_passwd='SomePassword'

print_success() { echo -e "\e[32m${@}\e[0m"; }
print_error() { echo -e "\e[31m${@}\e[0m"; }

if [ `whoami` != "root" ] && [ `whoami` != "forge" ] && [ `whoami` != "homestead" ] && [ `whoami` != "vagrant" ];
then
    print_error "You must be root to run this script"
    exit 1
fi

# PROBLEM #1 The IF below doesn't correctly detect when apt is updated. Always says failed.
echo "Updating apt"
sudo apt-get update 2>&1
if [ $? -ne 0 ]
then
    print_success "Updated apt"
else
    print_error "Failed to update apt"
    exit 1
fi

echo "Installing VsFTPD package"
sudo apt-get -y install vsftpd 2>&1
if [ $? -ne 0 ]
then
    print_success "Installed VsFTPD"
else
    print_error "Failed to Install VsFTPD"
    exit 1
fi

# PROBLEM #2 The conditional below does not detect if openssh server already installed and up to date.
echo "Installing openssh-server"
sudo apt-get install openssh-server 2>&1
if [ $? -ne 0 ]
then
    print_success "Installed openssh-server"
else
    print_error "Failed to Install openssh-server"
    exit 1
fi

sshd_config='/etc/ssh/sshd_config'

sudo mv $sshd_config $sshd_config.bak
sudo rm -f $sshd_config

echo "# SSH Config
Port 22
Protocol 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
UsePrivilegeSeparation yes
KeyRegenerationInterval 3600
ServerKeyBits 1024
SyslogFacility AUTH
LogLevel INFO
LoginGraceTime 120
PermitRootLogin without-password
StrictModes yes
RSAAuthentication yes
PubkeyAuthentication yes
IgnoreRhosts yes
RhostsRSAAuthentication no
HostbasedAuthentication no
PermitEmptyPasswords no
ChallengeResponseAuthentication no
X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
AcceptEnv LANG LC_*
UsePAM yes
Subsystem sftp internal-sftp
Match group ftpaccess
ChrootDirectory %h
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp
" | sudo tee $sshd_config

echo "Restarting SSH service"
sudo service ssh restart

echo "Creating user and group"
sudo groupadd ftpaccess
sudo useradd -m $sftp_user -g ftpaccess -s /usr/sbin/nologin
echo "$sftp_passwd" | sudo passwd $sftp_user

echo "Done"

我不是专业制作shell脚本的,所以很可能我也错过了其他的东西。有什么想法吗?

代码基于: http://www.krizna.com/ubuntu/setup-ftp-server-on-ubuntu-14-04-vsftpd/

编辑: 更新的脚本包含所有修复程序以防任何人感兴趣:

#!/bin/bash

# Based on: http://www.krizna.com/ubuntu/setup-ftp-server-on-ubuntu-14-04-vsftpd/
sftp_user='someuser'
sftp_passwd='SomePassword'

# IMPORTANT: Edit the values above or comment out the user add code before running.

print_success() { echo -e "\e[32m${@}\e[0m"; }
print_error() { echo -e "\e[31m${@}\e[0m"; }

if [ `whoami` != "root" ] && [ `whoami` != "forge" ] && [ `whoami` != "homestead" ] && [ `whoami` != "vagrant" ];
then
    print_error "You must be root to run this script!"
    exit 1
fi

echo "Updating apt..."
sudo_output=$(sudo bash -c "apt-get update 2>&1; echo $?")
sudo_result=$?
aptget_result=$(echo "${sudo_output}"| tail -1)

echo "${sudo_output}"

# Check results
if [ ${sudo_result} -eq 0 ]; then
    if [ ${aptget_result} -eq 0 ]; then
       print_success "Updated apt."
    else
       print_error "Failed to apt, apt-get error!"
    fi
else
    print_error "Failed to update apt, sudo error!"
    exit 1
fi

echo "Installing VsFTPD package..."
sudo_output=$(sudo bash -c "apt-get -y install vsftpd 2>&1; echo $?")

# Get results.
sudo_result=$?
aptget_result=$(echo "${sudo_output}"| tail -1)

# Show apt-get output.
echo "${sudo_output}"

# Check results
if [ ${sudo_result} -eq 0 ]; then
    if [ ${aptget_result} -eq 0 ]; then
       print_success "Installed VsFTPD."
    else
       print_error "Failed to Install VsFTPD, apt-get error!"
    fi
else
    print_error "Failed to Install VsFTPD, sudo error!"
    exit 1
fi

echo "Installing openssh-server"
sudo_output=$(sudo bash -c "apt-get install openssh-server 2>&1; echo $?")
sudo_result=$?
aptget_result=$(echo "${sudo_output}"| tail -1)
echo "${sudo_output}"

# Check results
if [ ${sudo_result} -eq 0 ]; then
    if [ ${aptget_result} -eq 0 ]; then
       print_success "Installed openssh-server."
    else
       print_error "Failed to install openssh-server, apt-get error!"
    fi
else
    print_error "Failed to install openssh-server, sudo error!"
    exit 1
fi

sshd_config='/etc/ssh/sshd_config'

sudo mv $sshd_config $sshd_config.bak
sudo rm -f $sshd_config

echo "# SSH Config
Port 22
Protocol 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
UsePrivilegeSeparation yes
KeyRegenerationInterval 3600
ServerKeyBits 1024
SyslogFacility AUTH
LogLevel INFO
LoginGraceTime 120
PermitRootLogin without-password
StrictModes yes
RSAAuthentication yes
PubkeyAuthentication yes
IgnoreRhosts yes
RhostsRSAAuthentication no
HostbasedAuthentication no
PermitEmptyPasswords no
ChallengeResponseAuthentication no
X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
AcceptEnv LANG LC_*
UsePAM yes
Subsystem sftp internal-sftp
Match group ftpaccess
ChrootDirectory %h
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp
" | sudo tee $sshd_config

echo "Restarting SSH service..."
sudo service ssh restart

echo "Creating user and group..."
sudo groupadd ftpaccess
sudo useradd -m $sftp_user -g ftpaccess -s /usr/sbin/nologin
echo "$sftp_passwd" | sudo passwd $sftp_user

echo "Done! :)"

1 个答案:

答案 0 :(得分:1)

您正在检查sudo的返回值,而不是apt-get的返回值。

编辑:
sudo "whoami; whoami"会返回错误,您需要sudo bash -c 'whoami; whoami'。解决方案是没有bash -c,我改变了这个。
注意:也许不需要整个构造,sudo true; echo $?; sudo false; echo $?在我的服务器上返回0和1。你能检查你的sudo命令的输出吗?

如果要同时检查两者:

sudo_output=$(sudo bash -c "apt-get -y install vsftpd 2>&1; echo $?")
# Get results
sudo_result=$?
aptget_result=$(echo "${sudo_output}"| tail -1)
# Show apt-get output
echo "${sudo_output}"

# check results

if [ ${sudo_result} -eq 0 ]; then
    if [ ${aptget_result} -eq 0 ]; then
       print_success "Installed VsFTPD"
    else
       print_error "Failed to Install VsFTPD, apt-get error"
    fi
else
    print_error "Failed to Install VsFTPD, sudo error"
    exit 1
fi