无法在SSH脚本中使用参数执行脚本

时间:2014-08-27 07:04:14

标签: shell unix ssh scp

我有这个ssh脚本ssh到远程服务器,然后scp .seq文件回到原始服务器..

ssh脚本

#! /usr/bin/bash

localServer=`hostname`
echo $localServer
#fetching the remote server credentials from sftp.conf
while read line
     do
     if [[ $line =~ ^#  ]];
        then
        continue;

     else
         serverIP=`echo $line|cut -d',' -f1`
         userID=`echo $line|cut -d',' -f2`
     fi
     break;
     done < sftp.conf

for ((;;)) {
     #now logging into the remote server
     ssh $userID@$serverIP 'sh /data/admin/scripts/SapcmedadpebM/test/sub_script.sh $localServer $userID'
     echo "script is running"
     sleep 10;
     }

远程服务器上的scp脚本 -

#---------parameters
user='root'
server='mhedr1'
ipPath='/data/admin/scripts/SapcmedadpebM/test'
opPath='/data/admin/scripts/SapcmedadpebM/test/files'

cd $ipPath
for file in `find -maxdepth 1 -mmin +1  -name "*.seq"`
#for file in *.seq
do
mv $file ./temp
scp ./temp/$file $2@$1:$opPath
echo $?
rm -f ./temp/$file
#echo $files>>abcde
done

现在的问题是,当我使用参数($ localServer&amp; $ userID)和scp脚本时,它会抛出错误“ssh:无法解析主机名:名称解析连接中的临时故障丢失” 另一方面,当我在脚本中硬编码这些参数时,它工作正常。任何人都可以告诉我为什么当我使用ssh从本地服务器执行它时scp脚本不接受参数....请告诉我你是否需要更清晰...谢谢

1 个答案:

答案 0 :(得分:0)

在bash环境中,您可以使用$ HOSTNAME变量。在调用SSH时,无法通过简单引用解析$ userid变量。

尝试:

localServer=$HOSTNAME
echo $localServer
#fetching the remote server credentials from sftp.conf
while read line
     do
     if [[ $line =~ ^#  ]];
        then
        continue;

     else
         serverIP=`echo $line|cut -d',' -f1`
         userID=`echo $line|cut -d',' -f2`
     fi
     break;
     done < sftp.conf

for ((;;)) {
     #now logging into the remote server
     ssh ${userID}@$serverIP "sh /data/admin/scripts/SapcmedadpebM/test/sub_script.sh $localServer ${userID}"
     echo "script is running"
     sleep 10;
     }