c shell 脚本无法识别远程服务器

c shell 脚本无法识别远程服务器

在 AIX 上我有一个脚本,它可以通过 ssh 连接到远程服务器

 #!/bin/sh     

 Usage () {     
    echo "Usage: $0 <config_file>"     
 echo "Example:   ./transfer_dmz_startD.sh /export/data/mbsesb/config/transfer_dmz.cfg"     
    exit     
 }          

if [ $# -lt 1 ]    
then    
    usage    
fi    

config_file=${1}    
NAME=`basename $0 .sh`    
LOG=${NAME}.log    
Today=`date '+%Y%m%d'`    
TARFILE=${NAME}.${Today}    
ZIPFILE=${TARFILE}.gz    
DMZ_USER=aaa    
DMZ_HOST=bbbb
TIMESTAMP=`date +%H:%M:%S`    

LOCL_WORKING_DIR=`grep "^local" $config_file | cut -d'=' -f2`    
LOGDIR=`grep "^log" $config_file | cut -d'=' -f2`    
DESTSERVNAME=`grep "^destserver" $config_file | cut -d'=' -f2`    
DAILY_DEST_DIR=`grep "^daily" $config_file | cut -d'=' -f2`    
WEEKLY_DEST_DIR=`grep "^weekly" $config_file | cut -d'=' -f2`    
MONTH_DEST_DIR=`grep "^monthly" $config_file | cut -d'=' -f2`    
LOCLSERVNAME=`grep "^loclserver" $config_file | cut -d'=' -f2`    
LOCL_FILES_DIR=`grep "^mbsesb" $config_file | cut -d'=' -f2`    
LOGF=${LOGDIR}.${LOG}    

mkdir -p ${LOCL_FILES_DIR}/cool_${Today}    

if [ $? != 0 ]    
then    
    echo "Cannot create ${LOCL_FILES_DIR}/cool_${Today}" >>$LOGF   
    echo "Check each directory permission and rerun the program">>$LOGF    
    echo "$NAME terminated abnormaly">>$LOGF    
     mailx -r [email protected] -s "!!!Please read  a $LOGF and make     appropriate action"    
    exit 1    
fi    



ssh ${DMZ_USER}@{$DMZ_HOST}    

if [ $? != 0 ]    
then    
    echo "Cannot connect to $DMZ_USER server, please check connection and re-run the script again">>$LOGF    
    echo "$NAME terminated abnormaly">>$LOGF    
     echo "$NAME.sh cannot connect to ${DMZ_USER}@{$DMZ_HOST}" | mailx  -r [email protected] -s "!!!Please read  a $LOGF and make appropriate action"    
    exit 1    
fi    

datetime=`date +%p`    

if [ ${datetime} -eq "AM" ]    
than    
    ext=Daily    
else    
    ext=EOD    
fi    
cd  $DAILY_DEST_DIR    

tar -cvf $TARFILE.$ext.tar *$Today*.*    

if [ $? != 0 ]    
then    
    echo "$NAME.sh cannot create tar file on $DMZ_USER server" >>$LOGF    
    echo "email is sending to developer" >>$LOGF    
     mailx -r [email protected] -s "!!!Cannot tar the files on     hbrgmfidmzb1. Please check the files on daily directory"    
fi    

gzip  $TARFILE.$ext.tar    

if [ $? != 0 ]    
then
    echo "$NAME.sh cannot create zip file"    
    echo "email is sending to developer"    
     mailx -r [email protected] -s "!!!Cannot tar the files on hbrgmfidmzb1. Please check the files on daily directory"    
fi    

执行该命令后出现错误:

ssh:无法解析主机名 {Apsswd}:未提供或未找到主机名和服务名
您给出的标志没有意义,因为您不发送邮件。
SSH_KEY 已设置,我可以手动连接到该服务器,但脚本不起作用。
有什么建议吗?

答案1

这是不正确的ssh命令调用。正确的是

ssh user@host

检查man ssh手册。

PSssh不允许将密码作为参数传递。您需要使用某个 ssh 代理,它会要求您输入一次需要手动输入的密码,然后它会ssh自动提供密码,或者您可以使用另一个第三方助手 sshpass -p 'Password' ssh user@host(不确定它是否存在于 AIX 上)

摆脱密码的另一种方法是使用公钥认证,这样你就可以提供私钥(无需密码创建ssh

ssh -i /path/to/private/key user@host

变量csh应指定为

set HOST = "RealRemoteHostName"
set USER = "SomeUser"

然后在脚本中使用它

ssh $USER@$HOST

附言

问题更新后发现它不是 C Shell(csh)但/bin/sh问题就在那里

相关内容