需要使用shell脚本

时间:2015-12-03 08:55:06

标签: bash shell ssh sed expect

我正在尝试以下方法:

#!/bin/bash

NodeIP="10.63.12.19"
userpd="password"



expect -c "
    spawn ssh root@$NodeIP
    expect "*assword:"
    send \"$userpd\r\"
    interact
    " 

CONFIG_FILE="/etc/main/params.conf"

User_val=10
IP_val=5
Blocking_max=2
Monitoring_max=2

sed -c -i "s/\(userMaxFailAttempts *= *\).*/\1$User_max/" $CONFIG_FILE
sed -c -i "s/\(userMonitoringWindow *= *\).*/\1$Monitoring_window/" $CONFIG_FILE
sed -c -i "s/\(userBlockingWindow *= *\).*/\1$Blocking_window/" $CONFIG_FILE
sed -c -i "s/\(ipMaxFailAttempts *= *\).*/\1$IP_max/" $CONFIG_FILE
sed -c -i "s/\(ipMonitoringWindow *= *\).*/\1$Monitoring_window/" $CONFIG_FILE
sed -c -i "s/\(ipBlockingWindow *= *\).*/\1$Blocking_window/" $CONFIG_FILE 

我需要在我正在执行ssh的远程服务器上执行ssh并编辑上面的param.conf文件,并且还需要在该ssh会话中执行一些其他命令。目前,文件未通过上述方法进行修改

1 个答案:

答案 0 :(得分:1)

您应该设置ssh密钥身份验证,然后根本不需要(并且您不会在纯文本文件中硬编码密码)

#!/bin/bash

NodeIP="10.63.12.19"
CONFIG_FILE="/etc/main/params.conf"
User_val=10
IP_val=5
Blocking_max=2
Monitoring_max=2

ssh root@$NodeIP << end_commands
  sed -c -i "s/\\(userMaxFailAttempts *= *\\).*/\\1$User_max/"           "$CONFIG_FILE"
  sed -c -i "s/\\(userMonitoringWindow *= *\\).*/\\1$Monitoring_window/" "$CONFIG_FILE"
  sed -c -i "s/\\(userBlockingWindow *= *\\).*/\\1$Blocking_window/"     "$CONFIG_FILE"
  sed -c -i "s/\\(ipMaxFailAttempts *= *\\).*/\\1$IP_max/"               "$CONFIG_FILE"
  sed -c -i "s/\\(ipMonitoringWindow *= *\\).*/\\1$Monitoring_window/"   "$CONFIG_FILE"
  sed -c -i "s/\\(ipBlockingWindow *= *\\).*/\\1$Blocking_window/"       "$CONFIG_FILE"
end_commands

你需要加倍反斜杠,因为在将命令发送到远程主机之前,shell正在进行一轮替换。