If you need to do a remote SSH command you often run into a snag.  You cannot log in with SSH using a script because you have to enter a password, which, unless you use a utility like “expect” – isn’t possible.  Yes, you can generate keypairs, which is the accepted standard, but what if your scenario doesn’t have that option?  (Like needing to SSH in to thousands of systems that you don’t maintain, but for which you do have a common password.)

Fortunately, someone out there developed the “sshpass” utility for us non-conformists to do our job.  Here’s how to install it:

In CentOS you’ll need to enable the EPEL repository first, if you haven’t already:

# yum -y install epel-release

Then you can install sshpass:

# yum --enablerepo=epel install sshpass

Once that’s done, here’s an example of how the script can be invoked.  In this example, I’m logging in to a Ubiquiti device at the IP address 10.0.0.4 to gather some wireless stats.  Replace “myusernamehere” and “mypasswordhere” with your actual username and password, of course.

# sshpass -p 'mypasswordhere' ssh 10.0.0.4 -l myusernamehere 'mca-status'

deviceName=Xxxxx, Xxxx,deviceId=00:15:6D:5C:XX:XX,firmwareVersion=XM.ar7240.v5.5.8.20795.140123.1700,platform=NanoBridge M2,deviceIp=172.30.0.1

apMac=00:15:6D:82:XX:XX
wlanOpmode=sta
wlanConnections=1
wlanUptime=55404
essid=alleyesonme1@1
freq=2449
signal=-53
noise=-102
ccq=880
uptime=815091
loadavg=10
memTotal=29524
memFree=6384
memBuffers=2744
ackTimeout=95
distance=7200
lanIpAddress=0.0.0.0
wlanIpAddress=10.0.0.4
wlanTxRate=26.0
wlanRxRate=65.0
wlanTxLatency=4
wlanPolling=1
wlanPollingQuality=81
wlanPollingCapacity=66
lanRxBytes=222226204
lanRxPackets=1339070
lanRxErrors=0
lanTxBytes=2140528932
lanTxPackets=2036501
lanTxErrors=0
lanPlugged=1
lanSpeed=100Mbps-Full
wlanRxBytes=3064677
wlanRxPackets=35267
wlanRxErrors=0
wlanTxBytes=3891886
wlanTxPackets=19136
wlanTxErrors=1
wlanRxErrNwid=0
wlanRxErrCrypt=0
wlanRxErrFrag=0
wlanRxErrRetries=0
wlanRxErrBmiss=0
wlanRxErrOther=0
latitude=0.000000
longitude=0.000000
cfgCrc=71084ccc