Connect via ssh and run a program after a switch user - linux

I want to create inside a bash script a funcion that connects via ssh to another server (using rsa keys), do a switch user (inserting the password), start a program and then exit with the exit code of the program started.
Below a test I'm doing:
#! /usr/bin/expect
set timeout 120
spawn ssh user1#10.211.55.24
expect ".*user1"
sleep 3
send "whoami\r"
send "/bin/su hdfs\r"
expect "*?assword:"
send "hdfs\r"
expect "$"
send "whomai\r"
send "exit\r"
It works until the switch user, I can switch to the hdfs user but the following commands are not sent (whomai). The $ prompt is correct. Furthermore I'm not able to get the exit code of the command (in this example echo command).

Related

How to ssh from one server to multiple server and switch to root and then change root password

From server A, i want to ssh multiple server using a non root user as direct root login is disabled to all server.
then i need to su - to switch to root
perform some normal operations, like changing directory, listing file etc.
and at the end change the root password using passwd command
I have expect installed on all servers and i am able to ssh from server A to any of the other servers but stuck in switching to root user and performing other operations as listed above.
#!/bin/bash
ssh user#ip<<'ENDSSH'
./su2root
#random operations
pwd
whoami
#random operations
./changepass
ENDSSH
su2root
#!/usr/bin/expect -f
spawn su -
set password "rootpass"
expect "assword:"
send "$password\r"
changepass
#!/usr/bin/expect -f
set newpassword "newrootpass"
spawn passwd
expect "*assword*"
send "$newpassword\r"
expect "*assword*"
send "$newpassword\r"
expect eof
exit 0
expecting to accomplish it using shell scripting only. Thanks in advance.
My initial comment on your question noted that you should be looking into running expect locally and spawning ssh sessions to the remote servers.
While not exactly answering your question, here is a simple expect script that logs into a remote system (one of my systems at home named valhalla), uses sudo -i to become root and executes the id command to show the user UID/GIDs. Note that I use ssh keys for logins which is why there is no expect for the initial login password.
The remote systems do not need expect in this example.
#!/usr/bin/expect
# vi: set ts=2 sw=2 noai ic showmode showmatch:
spawn ssh valhalla
expect "valhalla:"
send "sudo -i\r"
expect "assword for *"
send "XXXXXX\r"
expect "# "
send "id\r"
expect "(root)"
exit 0

Calling a script from another script doesn´t execute all the commands when SSH

I am trying to call a script from another script because I am using "spawn" (from expect packet) to execute an SSH to another machine. Basically, after executing a comparison and routine in my main script (scriptA.sh) it calls for another script (scriptB.sh). The calls works fine, as I can see that the SSH is correctly executed. However, something looks wrong as not all the commands are executed correctly. If I execute those commands manually it works.
I have running an application on a machine that sometimes gets frozen and shows totally white screen. scriptB.sh kills firefox and re-open the broswer on background. The commands works fine if i execute them manually:
sudo killall firefox
export DISPLAY=:0.0 (I don´t need "sudo" for this)
/usr/local/bin/run_digital_signage_firefox.sh& (This command must be running without "sudo")
This is a very small resume from my scriptA.sh (I execute this script like this: sudo ./scriptA.sh):
#!/bin/bash
...something....
export username
export password
export IP
sh /home/mydirectory/scriptB.sh
...something....
and this is my scriptB.sh (I pass the username, password and IP using "export" in my scriptA.sh):
#!/bin/bash
/usr/bin/expect << EOF
spawn ssh test#$IP "sudo killall firefox && export DISPLAY=:0.0 && /usr/local/bin/run_digital_signage_firefox.sh&"
sleep 3
expect "*?ame:*" {
send "$username\r"
sleep 2
expect "*?assword:*"
send "$password\r"
sleep 2
expect "\r"
sleep 2
}
expect "*?(yes/no)*" {
send "yes\r"
sleep 2
expect "*?ame:*"
send "$username\r"
sleep 2
expect "*?assword:*"
send "$password\r"
expect "\r"
sleep 2
}
EOF
When scriptA.sh calls scriptB.sh it seems that it gets to kill firefox browser but not to initialize this with the command "/usr/local/bin/run_digital_signage_firefox.sh&". I must run this command without sudo but even if i try to add "sudo /usr/local/bin/run_digital_signage_firefox.sh&" in the SSH in scriptB.sh it does not work as well. It seems that the script is not executing "/usr/local/bin/run_digital_signage_firefox.sh&" neither "sudo /usr/local/bin/run_digital_signage_firefox.sh&". I need to execute the last part of the script to open again the firefox browser.
Both scripts have rwx permissions in ugo.

expect auto login sucess, but can't exec any command

the shell code:
~ cat test_longin.sh
#!/usr/bin/expect
set timeout 120
spawn /usr/bin/ssh fuyou#target.server
expect "*assword*"
send "fuyou\n"
interact
When I type ./test_login.sh, it can login target servers successfully, but when I execute ls command, the console doesn't output any text. So I can't exec any commond on target server. Is the interaction problematic?
I think you should expect something after send, but not use command "interact" immediately.
xxx:/home/username/temp # cat t1.exp
#!/usr/bin/expect
spawn ssh serverx
expect "#"
send "ls -l\r"
expect "#"
interact

How to automate telnet session using Expect?

I'm trying to write an expect script to automate telnet. This is what I have so far.
#!/usr/bin/expect
# Test expect script to telnet.
spawn telnet 10.62.136.252
expect "foobox login:"
send "foo1\r"
expect "Password:"
send "foo2\r"
send "echo HELLO WORLD\r"
# end of expect script.
Basically, what I want to do is telnet to the following IP address and then echo HELLO WORLD. However, it seems that the script fails after attempting to telnet...I'm not sure if it's able to accept login and password input, but it is not echoing HELLO WORLD. Instead, I just get this output:
cheungj#sfgpws30:~/justin> ./hpuxrama
spawn telnet 10.62.136.252
Trying 10.62.136.252...
Connected to 10.62.136.252.
Escape character is '^]'.
Welcome to openSUSE 11.1 - Kernel 2.6.27.7-9-pae (7).
foobox login: foo1
Password: foo2~/justin>
It's hard to tell, but from the output you're pasting it looks like:
Your script isn't waiting for login to complete before sending the next command.
Your script is exiting and closing the process before you can see any output.
There are no guarantees in life, but I'd try this as a first step:
#!/usr/bin/expect -f
spawn telnet 10.62.136.252
expect "foobox login:"
send "foo1\r"
expect "Password:"
send "foo2\r"
# Wait for a prompt. Adjust as needed to match the expected prompt.
expect "justin>"
send "echo HELLO WORLD\r"
# Wait 5 seconds before exiting script and closing all processes.
sleep 5
Alternatives
If you can't get your script to work by manually programming it, try the autoexpect script that comes with Expect. You can perform your commands manually, and autoexpect will generate an Expect typescript based on those commands, which you can then edit as needed.
It's a good way to find out what Expect actually sees, especially in cases where the problem is hard to pin down. It's saves me a lot of debugging time over the years, and is definitely worth a try if the solution above doesn't work for you.
You're sending the echo command without first expecting the prompt. Try:
# after sending the password
expect -re "> ?$"
send "echo HELLO WORLD\r"
expect eof
Have you seen this StackOverflow Question?
He seems to have got things working by using curly braces.
Here is a simplified version
#!/usr/bin/expect
# just do a chmod 755 one the script
# ./YOUR_SCRIPT_NAME.sh $YOUHOST $PORT
# if you get "Escape character is '^]'" as the output it means got connected otherwise it has failed
set ip [lindex $argv 0]
set port [lindex $argv 1]
set timeout 5
spawn telnet $ip $port
expect "'^]'."

expect command - Unix

I am using 'expect' to automate ssh password authentication. When I run the script in SunOS, I find the spawned ssh process gets killed once the below script is completed. This is not the case in Linux. How do we avoid it? Should we ignore the SIGCHLD signal somehow?
Is there anyway to determine through this script if spawned process is successful and report error if any?
#!/usr/local/bin/expect -f
set password blah-blah
spawn ssh -NfL 8002:<test domain>:22 test#testdomain.com
expect "* password:*"
send -- "$password\r"
send -- "\r"
expect EOF
-Karthik
If you use ssh-keys, you won't need to code passwords in shell scripts.
You could even encrypt the key with a passphrase, and use ssh-agent to manage the key for you -- you unlock your key in the morning, start your tunnel, and then forget your key when you head to lunch, unlock your key in the afternoon, and forget it again when you go home at night. No on-disk magic gateway to remote machines.
Instead of putting the ssh command in the background you could put the expect script into the background:
#!/usr/local/bin/expect -f
if {[fork] != 0} exit
disconnect
set password blah-blah
spawn ssh -NL 8002:localhost:22 test#testdomain.com
expect {
EOF {exit 1}
"assword:" {}
}
send -- "$password\n"
send -- "\n"
expect EOF
wait
Works for me on Linux. At least for the setup phase, stopping it is more difficult. I had to kill -9 to stop the expect script. Which probably requires killing the ssh process as well.

Resources