bash - How to use su command from within ssh command in unix shell script -
my requirement login remote machine , make folders planning take username , pwd input user , try make automate shell script.
1.)i used following code ssh machine , give expected pwd login machine.
dsadminpwd=dsadminpwd pwd=pwd /usr/bin/expect<<eod spawn ssh -ostricthostkeychecking=no -ocheckhostip=no <username>@<remotemachineurl> expect "password" send "$pwd\n" eod the above works fine. while doing su dsadmin after this. unable go inside user taken password.
2.) have change user su dsadmin inside machine. there password dsadmin. not working properly.
dsadminpwd=dsadminpwd pwd=pwd /usr/bin/expect<<eod spawn ssh -ostricthostkeychecking=no -ocheckhostip=no <username>@<remotemachineurl> expect "password" send "$pwd\n" eod su dsadmin <like make folders in dsadmin directory> exit... after doing su dsadmin come
bash-3.00$ no sign of password or here.
from above not working
could please suggest if possible make su after ssh password in automated script. suggestions highly appreciated.
thanks!
i used expect command long time ago , worked smoothly su command if launched ssh console.
here examples maybe can find useful.
first of bash script:
#!/bin/bash exec 1> stdoutfile exec 2> stderrfile ./yourexpectscript your_remoteip_here userexample passwordexample secondly expect script:
#!/usr/bin/expect -- send_user "connecting remote server\n" set server [lindex $argv 0] set user [lindex $argv 1] set pass [lindex $argv 2] set dsadminpass "dsadminpwd" spawn ssh $user@$server expect "password: " send "$pass\n" expect { "> " { } "$ " { } "# " { } } #example command in ssh shell send "ls -lah\n" expect { "> " { } "$ " { } "# " { } default { } } #su command send "su - dsadmin\n" expect { "password: " { } } send "$dsadminpass\n" expect { "> " { } "$ " { } "# " { } default { } } #example command in dsadmin shell send "ls -lah\n" #login out dsdamin shell send "exit\n" expect { "> " { } "$ " { } "# " { } default { } } #login out ssh connection send "exit\n" send_user "connection remote server finished\n"
Comments
Post a Comment