【问题标题】:How to create directory if doesn't exists in sftp如果 sftp 中不存在如何创建目录
【发布时间】:2014-12-11 10:07:53
【问题描述】:

如果登录sftp服务器后目录不存在,我想创建一个目录。

test.sh

sftp name@example.com << EOF
mkdir test
put test.xml
bye
EOF

现在我调用 test.sh 并每次将不同的文件上传到测试文件夹。运行时

mkdir test

第一次工作,第二次抛出无法创建目录:失败错误?

如果不存在如何创建目录,如果存在则不要在 sftp 中创建目录。

【问题讨论】:

  • 您的帐户是否有对该计算机的常规 ssh 访问权限或仅限于 sftp?
  • 我既有常规的 ssh 访问权限,也有 sftp。

标签: shell sftp


【解决方案1】:

man 1 sftp(来自openssh-client 包):

-b batchfile

    Batch mode reads a series of commands from an input
    batchfile instead of stdin. Since it lacks user
    interaction it should be used in conjunction with
    non-interactive authentication. A batchfile of ‘-’
    may be used to indicate standard input. sftp will
    abort if any of the following commands fail: get,
    put, reget, reput, rename, ln, rm, mkdir, chdir, ls,
    lchdir, chmod, chown, chgrp, lpwd, df, symlink, and
    lmkdir. Termination on error can be suppressed on a
    command by command basis by prefixing the command
    with a ‘-’ character (for example, -rm /tmp/blah*).

所以:

{
  echo -mkdir dir1
  echo -mkdir dir1/dir2
  echo -mkdir dir1/dir2/dir3
} | sftp -b - $user@$host

【讨论】:

  • 好的,但这不是问题的答案。带有“-”的技巧只是避免了 SFTP 批处理的终止,但并不能避免错误本身。
  • @lef OMG,我在桌子底下))为了纯度,您可以使用古老的实用程序expect,它允许与交互式程序进行自动化交互。它非常实用,但对于简单任务来说是不必要的依赖。
  • @gaveonka 无关。任务是在目录存在且没有错误的情况下避免使用命令 - 因此输出中没有垃圾邮件失败消息 - 如果存在则不要继续使用它们。
  • 如我所说,使用expect。 Python/Perl 中有很多 expect 绑定,但在以后的情况下,您可以使用直接库进行 SFTP 访问。
  • 因此,如果您的解决方案是使用该工具,您应该给出一个说明该工具用法的答案,而不是复制手册。
【解决方案2】:

我知道这个帖子很旧并且已被标记为已回答,但答案在我的情况下不起作用。谷歌上的第二页搜索“sftp 检查目录”,所以这里有一个更新,可以为我节省几个小时。

使用 EOT,您无法捕获因找不到目录而导致的错误代码。我发现的解决方法是创建一个包含呼叫说明的文件,然后捕获该自动呼叫的结果。

下面的示例使用 sshpass,但我的脚本也使用相同的方法通过 sshkeys 进行身份验证。

创建包含说明的文件:

echo "cd $RemoteDir" > check4directory
cat check4directory; echo "bye" >> check4directory

设置权限:

chmod +x check4directory

然后使用批处理功能进行连接:

export SSHPAA=$remote_pass
sshpass -e sftp -v -oBatchMode=no -b check4directory $remote_user@$remote_addy

最后检查错误代码:

if [ $? -ge "1" ] ; then
  echo -e "The remote directory was not found or the connection failed."
fi

此时您可以退出 1 或启动其他操作。请注意,如果 SFTP 连接因密码或地址不正确等其他原因而失败,则该错误将触发该操作。

【讨论】:

    【解决方案3】:

    另一种变体是将 SFTP 会话一分为二。

    第一个 SFTP 会话只需发出 MKDIR 命令。

    然后,第二个 SFTP 会话可以假定目录存在并放置文件。

    【讨论】:

      【解决方案4】:

      您可以使用您帐户的 SSH 访问权限首先验证该目录是否存在(使用“test”命令)。如果它返回退出代码 0,则该目录存在,否则不存在。你可以采取相应的行动。

      # Both the command and the name of your directory are "test"
      # To avoid confusion, I just put the directory in a separate variable
      YOURDIR="test"
      
      # Check if the folder exists remotely
      ssh name@example.com "test -d $YOURDIR"
      
      if [ $? -ne 0 ]; then
        # Directory does not exist
        sftp name@example.com << EOF
        mkdir test
        put test.xml
        bye
        EOF
      else
        # Directory already exists
        sftp name@example.com << EOF
        put test.xml
        bye
        EOF
      fi
      

      【讨论】:

      • 对于纯 SFTP 解决方案(如果您没有 shell 访问权限或目标服务器不是 *nix)请参阅:How to check if file exists in remote Windows from local Linux script?
      • @OldSkool 登录 sftp 服务器后,我想要一个检查目录,而不是在 shell 脚本中。
      • @karan 既然你提到你也有 SSH 访问权限,这个脚本首先检查目录是否存在 通过 SSH。所以它会远程检查。
      • 值得注意的是,如果@karan 的服务器在其 openssl 服务中仅配置了 chrooted SFTP 访问,那么 ssh 检查不是一个选项,这是一种常见情况。然而,karan 确认他有常规的 ssh 访问权限,因此使用 ssh 检查显然更简单。
      • 如果您也有 ssh 访问权限,使用 mkdir -p 创建目录要简单得多,如下所示:ssh name@example.com "mkdir -p $YOURDIR",然后进行复制。
      【解决方案5】:

      如果目录已经存在,请尝试忽略错误。

      # Turn OFF error
      set +e
      
      # Create remote dirs
      sftp -P 22 -o StrictHostKeyChecking=no -oIdentityFile=key.pem -v $user@$host <<EOF
      mkdir <remote_path> # create remote directory
      bye
      EOF
      
      # Turn ON error
      set -e
      
      # Do upload to SFTP
      sftp -P 22 -o StrictHostKeyChecking=no -oIdentityFile=key.pem -v $user@$host <<EOF
      cd <remote_path>  # remote_path
      put <local_file_path> # local_path
      quit
      EOF
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-26
        • 2014-02-07
        • 1970-01-01
        • 1970-01-01
        • 2017-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多