How to run remote script with sudo access - expect

I am trying to run a remote script which need to run as a sudo but its unable to run the script as sudo. Is my approach is correct to do sudo? Please help..Thanks in advance.
spawn ssh -t {*}$ssh_opts $user#$ip bash $script {*}$argv

Try this :-
#!/usr/bin/expect
set timeout -1
spawn -noecho bash -c "ssh user#host \"sudo su -c 'your_command' - sudo_user\""
expect {
-re ".*assword:"{
send "password\r"
exp_continue
} eof {
wait
}
}
Hope it helps.!!!

Related

How to execute commands right after login using SSH connection? But after few seconds of login, it disconnect me from remote server

#!/bin/bash
expect <<END
spawn ssh -o user#remote_ip
expect "password"
send "my_pass\r"
expect eof
END
cd /var/www/html/node_project/
##npm init -y
npm install
##node index.js
I want run some commands right after login into remote server. I can login successfully, but 2 or 3 seconds later, it automatically logout me from the remote server. How can I make it wait until all of my commands run and execute successfully?
Use this:
sshpass -p "PASSWORD" ssh -o StrictHostKeyChecking=accept-new USERNAME#IP_ADDRESS "sleep 10 ; ls -l"
or this (for connection timeout):
sshpass -p "PASSWORD" ssh -o StrictHostKeyChecking=accept-new USERNAME#IP_ADDRESS 'nohup bash -c "sleep 3; ls -l"'
Note: replace ls -l with your command.
Here is my solution: I have run multiple commands in line and it executes all the commands one after another.
spawn ssh user#remote_ip "cd /var/www/html/node_project/" && "npm install" && "node index.js"

Run bash script on remote server

I'm trying to run a bash script on the remote server that is already on the remote server. I'm using ssh pass to do it but I'm seeing errors
test.sh (resides on the remote server)
#!/usr/bin/env bash
echo "This is test"
adb start-server
sshpass command (I'm running this sshpass command from docker ubuntu image
sshpass -p password ssh -oStrictHostKeyChecking=no -oCheckHostIP=no user#host "bash -s" < /Users/user/Documents/workspace/test.sh
I also tried
sshpass -p password ssh -oStrictHostKeyChecking=no -oCheckHostIP=no user#host 'cd /Users/user/Documents/workspace/; sh test.sh'
I get this error message
bash: /Users/user/Documents/workspace/test.sh: No such file or directory
The examples you're showing are for a local script, and you said it's a remote script.
sshpass -p password ssh -oStrictHostKeyChecking=no -oCheckHostIP=no user#host "bash /path/to/test.sh"
that ought to do it.
you can try to find your test.sh on the remote computer:
sshpass -p password ssh -oStrictHostKeyChecking=no -oCheckHostIP=no user#host "find ~/ -name \"test.sh\""
Try with here-document:
sshpass -p password ssh -oStrictHostKeyChecking=no -oCheckHostIP=no -T user#host <<EOF
bash /Users/user/Documents/workspace/test.sh
EOF
Include -T option for ssh command, as mentioned above, to disable pseudo-tty.
[AT REMOTE MATCHINE] Ensure that path of adb executable is included in PATH environment variable. Else, specify it with absolute path in the Shell script.

expect script works while invoking individually but not as a salt state

I'm trying to do scp as well as ssh through expect. Below script works if I invoke it directly from terminal like /usr/bin/expect myexpect.sh but when I ran it using salt, the first scp command works where the second ssh fails.
myexpect.sh
#!/usr/bin/expect -f
set timeout 240
spawn scp apps.tar.gz /srv/salt/integration/serverclass_merged.conf foo#10.10.10.10:/home/foo
expect "password:"
send "password\n";
expect eof
spawn ssh -o StrictHostKeyChecking=no foo#10.10.10.10 "cd /home/foo;tar --strip-components=1 -xzvf apps.tar.gz -C /opt/apps/;cp serverclass_merged.conf /opt/local/serverclass.conf"
expect "assword:"
send "password\r"
interact
Relevant salt state looks like,
st.sls
copy_apps:
cmd.run:
- name: /usr/bin/expect /home/ocdn_adm/myexpect.sh
I know nothing about salt-stack but I suspect it's not running your Expect script from a pty. So replace interact with expect eof (or expect -timeout 12345 eof if necessary). interact works only when stdin is on a tty/pty.

multiple commands in spawn script to copy keys

I'm trying to create a script to copy ssh keys to multiple servers for passwordless login. I have a list of servers entered one per line in serverlist.txt. Below is the script I created.
#!/usr/bin/expect -f
set f [open "serverlist.txt"]
set hosts [split [read $f] "\n"]
close $f
foreach host $hosts {
spawn -noecho sh -c "cat ~/.ssh/id_rsa.pub | ssh -t -o StrictHostKeyChecking=no $host 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'"
expect "assword:";
send "abc123\r"; # sending the password
expect "$"; #expecting the $prompt
send "exit\r";
interact
}
Im getting below error while running this script
send: spawn id exp4 not open
while executing
"send "exit\r""
("foreach" body line 6)
invoked from within
"foreach host $hosts {
spawn -noecho sh -c "cat ~/.ssh/id_rsa.pub | ssh -t -o StrictHostKeyChecking=no $host 'mkdir -p ~/.ssh && cat >> ~/.ssh/authori..."
(file "sshkeys" line 6)
If i commented below lines I'm getting diff error
#expect "assword:";
#send "abc123\r";
#expect "$";
#send "exit\r";
Pseudo-terminal will not be allocated because stdin is not a terminal.
user#1.1.1.1's password:
Pseudo-terminal will not be allocated because stdin is not a terminal.
ssh: mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys: Name or service not known
Note:
ssh-copy-id is not available.
None of the target servers have the .ssh directory created.
Can somebody help me to identify the issue.

cannot execute command after logging in with expect

I have the following script:
#!/usr/bin/expect -f
set timeout 60
spawn ssh -X user#login.domain.co.uk
expect "Password:"
# Send the password, and then wait for a shell prompt.
send "password\r"
exp_continue
expect "user*"
send "ls -la\r"
however I get the following:
Password: command returned bad code: -101
while executing
"exp_continue"
(file "./hpclogin.sh" line 10)
If I remove the exp_continue i.e.
#!/usr/bin/expect -f
set timeout 60
spawn ssh -X user#login.domain.co.uk
expect "Password:"
# Send the password, and then wait for a shell prompt.
send "password\r"
expect "user*"
send "ls -la\r"
I can log in successfully, however the ls -la command does not get executed. Is there something wrong with the flow-control of my program?
exp_continue is only useful inside an expect block. For example:
spawn ssh -X user#example.com
expect {
"continue connecting (yes/no)? " {
send "yes\r"
exp_continue"
}
"Password:"
}
send "password\r"
I think you're not seeing the ls output because you're not expecting to see anything after to send it. Depending on your workflow, here are 2 thoughts:
add the command as arguments to ssh
spawn ssh -X user#example.com ls -la
expect {
"continue connecting (yes/no)? " {
send "yes\r"
exp_continue"
}
"Password:"
}
send "password\r"
expect eof
expect a prompt after the command, and log out afterwards
spawn ssh -X user#example.com
expect {
"continue connecting (yes/no)? " {
send "yes\r"
exp_continue"
}
"Password:"
}
send "password\r"
expect $theprompt
send "ls -la\r"
expect $theprompt
send "exit\r"
expect eof
Of course, this would be much simpler using ssh keys:
ssh-keygen
ssh-copy-id user#example.com
ssh -X user#example.com ls -la

Resources