简体   繁体   English

使用ssh的“语法错误:意外的文件结尾”

[英]“syntax error: unexpected end of file” with using ssh

Can anyone tell me what is wrong with this code? 谁能告诉我这段代码有什么问题? It is supposed to be a help me do a speed test on a remote server. 它应该是帮助我在远程服务器上进行速度测试。 I get the following error when trying to execute it via SSH using PUTTY: syntax error: unexpected end of file 尝试使用PUTTY通过SSH执行时出现以下错误:语法错误:意外的文件结束

    ssh_server=$1
    test_file=".scp-test-file"
    # Optional: user specified test file size in kBs
    if test -z "$2"
    then
        # default size is 10kB ~ 10mB
        test_size="10000"
    else
        test_size=$2
    fi
    # generate a 10000kB file of all zeros
    echo "Generating $test_size kB test file..."
    `dd if=/dev/zero of=$test_file bs=$(echo "$test_size*1024" | bc) \
        count=1 &> /dev/null`
    # upload test
    echo "Testing upload to $ssh_server..."
    up_speed=`scp -v $test_file $ssh_server:$test_file 2>&1 | \
        grep "Bytes per second" | \
        sed "s/^[^0-9]*\([0-9.]*\)[^0-9]*\([0-9.]*\).*$/\1/g"`
    up_speed=`echo "($up_speed*0.0009765625*100.0+0.5)/1*0.01" | bc`
    # download test
    echo "Testing download to $ssh_server..."
    down_speed=`scp -v $ssh_server:$test_file $test_file 2>&1 | \
        grep "Bytes per second" | \
        sed "s/^[^0-9]*\([0-9.]*\)[^0-9]*\([0-9.]*\).*$/\2/g"`
    down_speed=`echo "($down_speed*0.0009765625*100.0+0.5)/1*0.01" | bc`
    # clean up
    echo "Removing test file on $ssh_server..."
    `ssh $ssh_server "rm $test_file"`
    echo "Removing test file locally..."
    `rm $test_file`
    # print result
    echo ""
    echo "Upload speed:   $up_speed kB/s"
    echo "Download speed: $down_speed kB/s"

Any Ideas? 有任何想法吗? Thanks! 谢谢!

Remove the backticks from around those commands that are not part of an assignment to a variable. 从那些不属于变量赋值的命令周围删除反引号。

Also, make sure there are no tabs, spaces or carriage returns after the line-continuation backslashes (and no carriage returns at all in the file). 此外,确保在行继续反斜杠后没有制表符,空格或回车符(并且文件中根本没有回车符)。

Instead of multiplying by the ugly "0.0009765625", divide by 2^17 or 131072 . 而不是乘以丑陋的“0.0009765625”,除以2^17131072

Why are you dividing by 1? 你为什么除以1? Just omit that. 只是省略。 Divide by 100 instead of multiplying by 0.01. 除以100而不是乘以0.01。

Even though it's unlikely that the variables' contents contain whitespace, you should make it a habit to always quote variables when they are expanded. 尽管变量的内容不太可能包含空格,但您应该养成在扩展变量时始终引用变量的习惯。

#!/bin/bash
ssh_server=$1
test_file=".scp-test-file"
# Optional: user specified test file size in kBs
if test -z "$2"
then
    # default size is 10mB
    test_size="10000"
else
    test_size=$2
fi
# generate a file of all zeros
echo "Generating $test_size kB test file..."
dd if=/dev/zero of="$test_file" bs=$(echo "$test_size*1024" | bc) \
    count=1 &> /dev/null
# upload test
echo "Testing upload to $ssh_server..."
up_speed=$(scp -v "$test_file" "$ssh_server:$test_file" 2>&1 | \
    sed -n '/Bytes per second/s/^[^0-9]*\([0-9.]*\)[^0-9]*\([0-9.]*\).*$/\1/p')
up_speed=$(echo "scale = 2; $up_speed / 131072 * 100.0" | bc -l)
# download test
echo "Testing download to $ssh_server..."
down_speed=$(scp -v "$ssh_server:$test_file" "$test_file" 2>&1 | \
    sed -n '/Bytes per second/s/^[^0-9]*\([0-9.]*\)[^0-9]*\([0-9.]*\).*$/\2/p')
down_speed=$(echo "scale = 2; $down_speed / 131072 * 100.0" | bc -l)
# clean up
echo "Removing test file on $ssh_server..."
ssh $ssh_server "rm '$test_file'"
echo "Removing test file locally..."
rm "$test_file"
# print result
echo
echo "Upload speed:   $up_speed kB/s"
echo "Download speed: $down_speed kB/s"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM