I am trying to execute few commands in a server by logging in using sshpass
command like below.
SSH_ARGS='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -q'
sshpass -p 'password' ssh ${SSH_ARGS} user@IP 'sudo sed -i "/^server 0.rhel.pool.ntp.org/ { N; s/^server 0.rhel.pool.ntp.org\n/server xxx.xx.xx.xx iburst\n&/ }" /etc/ntp.conf'
sshpass -p 'password' ssh ${SSH_ARGS} user@IP 'sudo sed -i "/^#server 0.rhel.pool.ntp.org iburst/ { N; s/^#server 0.rhel.pool.ntp.org iburst\n/server xxx.xx.xx.xx iburst\n&/ }" /etc/ntp.conf'
echo "File /etc/ntp.conf is now edited"
sshpass -p 'password' ssh ${SSH_ARGS} user@IP 'sudo cat /etc/ntp.conf | grep "server xxx.xx.xx.xx iburst"'
if [ $? = 0 ];
then
sshpass -p 'password' ssh ${SSH_ARGS} user@IP 'sudo service ntpd status;sudo service ntpd restart'
else
echo "File /etc/ntp.conf is not updated in IP"
fi
So instead of repeating sshpass everytime, I would like to put sshpass -p 'password' ssh ${SSH_ARGS} user@IP
as a variable or function. How to do that?
I have tried ::
SSH_ARGS='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -q'
sshpp=`sshpass -p 'password' ssh ${SSH_ARGS} user@IP`
$sshpp 'sudo service ntpd status'
and this ::
SSH_ARGS='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -q'
sshpp() {
sshpass -p 'password' ssh ${SSH_ARGS} user@IP`
}
sshpp 'sudo service ntpd status'
I tried this also, but not working.
How will I achieve this instead of repeating sshpass
every time?
w
.for
loop now. If I get a solution for this, my script will look less messy and have not to repeatssh
everytime."
for sshpp not backquotes`
. Your second would work if you had the function use its arguments while preserving their quoting with"$@"
. Butalias
as per answer is better here.