In this bash script we will use expect scripting.Expect is known primarily as an indispensable application for system administrators . We use expect generally in case where input from user is required , for eg. ssh, ftp, telnet, sudo etc.
Given below is sample bash script which will first ssh the remote host and then get command output from remote host (here, we used
uname -a command at Step )
Step 1 : Create a new file.
Step 2 : Copy and paste below given content in file.
Change the value as per your information in variables –
HOST=Give Remote Hostname
USER=Give Remote Host user name
PASS=Give user’s password
#!/bin/bash HOST="localhost" USER="chitti" PASS="123" CMD=$@ XYZ=$(expect -c " spawn ssh $USER@$HOST expect \"password:\" send \"$PASS\r\" expect \"\\\\$\" send \"$CMD\r\" expect -re \"$USER.*\" send \"logout\" ") echo "$XYZ"
And at the end, save the file.
Step 3: Make your file executable by owner of file , run the given below command.
chmod 750 expectcmd
Step 4: Give commands as argument along with expectcmd script. See below given example.
Note: (Try this command to get kernel info of Remote machine)
sh expectcmd "uname -a"
See the below given screenshot for reference.