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 ];
sshpass -p 'password' ssh ${SSH_ARGS} user@IP 'sudo service ntpd status;sudo service ntpd restart'
echo "File /etc/ntp.conf is not updated in IP"

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?

  • Why don't you put that in a script on the remote server where it needs to run? Jan 4 '17 at 4:31
  • 1
    Note that putting a password in the command line is very insecure as command lines are logged in different places and can be seen by other users with w. Jan 4 '17 at 4:32
  • I have multiple servers that need to make changes; using for loop now. If I get a solution for this, my script will look less messy and have not to repeat ssh everytime.
    – prado
    Jan 4 '17 at 4:34
  • I am aware about password issue, the servers are not publicly accessible.
    – prado
    Jan 4 '17 at 4:35
  • Your first try would work if you used doublequotes " for sshpp not backquotes `. Your second would work if you had the function use its arguments while preserving their quoting with "$@". But alias as per answer is better here. Jan 4 '17 at 8:44

What you seem to be looking for is an alias:

alias sp='sshpass -p "password" ssh $SSH_ARGS user@IP'

You can therefore run your commands like:

sp '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'

Note that there may be ways to simplify your life a bit more by making scripts to do these repetitive tasks, but that is beyond the scope of this question.

  • Aliases don't take arguments; instead they always append the rest of the command line after substituting the first word. Shell functions and scripts do use $*.and $1 ${3/foo/bar} etc Jan 4 '17 at 8:46
  • Oops, you're right. I updated it. Jan 5 '17 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.