Using Expect with SSH and Su
We have discussed using expect with sudo in an SSH session. Another common way to run commands on remote servers as root is to use the su – root command, where sudo is not configured. The trick to automating su operations using expect is to launch the su command with the spawn ssh command. For our next example we have a file called server_list, containing a list of remote hosts. We need to ssh to each host using a non-privileged user account (local, LDAP, NIS), then su to root and change permissions of the /etc/issue file.
# cat ./server_list node1 node2 node3 node4 node5
The following shell script will read hostnames from the list, initiate SSH, enter passwords for the user and root account, and execute the chmod command.
#!/bin/ksh username=$1 userpass=$2 rootpass=$3 cat server_list | while read host do expect -c " set timeout 5 spawn ssh -tq ${username}@${host} su - root expect "ssword" { send "${userpass}r" } expect "ssword" { send "${rootpass}r" } expect "#" { send "chmod 644 /etc/issuer" } expect "#" { send "exitr" } expect eof" done
You will provide the username and both user and root passwords from command line when running the script:
./chmod_script.sh userbob bobspassword rootpassword
Hi guys
I am fairly new to shell scripts, and have written the following script to SSH into a certain server, SU into the correct user account (I do not control the server, so this is the only way to do it), set the display and run a series of applications.
The Issue I am having is, as the shell script SSH’es into the server, the script does not continue to write the output into the SSH session. I was not expecting this to work, as I believe that the script terminates as SSH starts, my question is, how can I achieve what I want?
The script is below:
#!/bin/csh
echo Loading Remote Sparc Display – Press Enter to Begin
read
ssh ruser@169.24.7.249
sleep 5
P4ssW0rD
sleep 5
su – rmtinstall
sleep 5
AnOTH3R P455w0Rd
sleep 5
setenv DISPLAY 10.81.38.153:0.0
cmdtool& cmdtool& iview&
exit
Any Ideas would be much appreciated!!
Paul
I would like to use telnet or ssh session to administer my mysql database and develop my php sites with vi editor.
How can I restore an sql dump file to a mysql server?
I work in an office where an underling constantly uses the internet for frivolous reasons–or just bugs the crap out of various people by forcing everyone to watch Youtube videos, or read Lolcats or FML aloud. This…makes me want to strangle. I need a way to remotely shut off the internet on the iMac using my little Macbook laptop. Any ideas?
What protocol is used to establish a secure telnet session over a TCP/IP connection?
It takes along time for my ssh sessions to connect when my firewall is running, why is that?
opensolaris
ipfilters opensolaris/solaris 10