Running shell command after expect login - linux

Iam trying to exceute a command after logging in to a linux RHEL box using expect and interact.
Below is script
#!/usr/bin/expect
set timeout 100
set temp [lindex $argv 0]
spawn ssh userid#10.20.30.40
expect "Password:"
send "password\n";
interact
expect "*3.2*"
send "./p.sh\n";
Its successfully logging in to the box but after that its not excecuting the command.
This is the actual output of the commnad after login , which iam trying to exceute.
Using keyboard-interactive authentication.
Password:
Last login: Sun Mar 22 11:04:01 2015 from com
-sh-3.2$ pbrun pbapp wasapp=ksh
Please note home directories are intended only for user/application profiles.
$
These are the errors i received
-sh-3.2$ exit
logout
Connection to 10.20.30.40 closed.
expect: spawn id exp7 not open
while executing
"expect "*$""
(file "./testWas.sh" line 8)
when i try
expect "*$"
exec "pwd"
-sh-3.2$ exit
logout
Connection to 10.20.30.40 closed.
couldn't execute " pwd ": no such file or directory
while executing
"exec { pwd }"
(file "./testWas.sh" line 8)
Edit:
Thanks to red #glenn jackman
iam able to excute pbrun commands after login..
But after excecuting the pbrun command script is exiting
#!/usr/bin/expect
set timeout 100
set host [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set command [lindex $argv 3]
spawn ssh $username#$host expect "Password:"
send "$password\n";
expect -re {\$ $} ; # put here string from your server prompt
send "./p.sh\n";
expect -re {\$ $} ;
send "pwd\n";
This is the content of p.sh
Only first line of the script is executing..
-sh-3.2$ cat p.sh
pbrun pbapp wsapp=ksh
pwd
clear
-sh-3.2$
There is a similar unanswered question
How to run "pbrun pbapp wasapp=ksh" command using SSH java client?

interact tells expect that you are going into manual mode, where you, the human, is in control of the spawned command. I see you then typed exit which ended the ssh session. Since the spawned command ended, the interact command ended and control returned to the script. The next command dies because the spawned command is not running.
Simply put, remove interact:
#!/usr/bin/expect
set timeout 100
set temp [lindex $argv 0]
spawn ssh userid#10.20.30.40
expect "Password:"
send "password\r" # a carriage return more exactly represents
# "hitting enter"
expect -re {\$ $} # this regular expression matches the end of the prompt
send "./p.sh\r"
if { you want to interact manually with the ssh session } {
interact
} else {
expect -re {\$ $} # if p.sh exits the ssh session, remove this line
send "exit\r" # and this one too.
expect eof
}

Related

Linux Shell script (bash) to change IP address of remote machine using SSH

I'd like to have a shell script to change the IP address of a remote machine using SSH.
The problem I am having is that both SSH and remote commands require passwords. I am using 'expect' to handle the password input and this works works well in isolation for each step, but not combined.
i.e. I have a separate script on the remote machine to change the IP address. This works when running manually on the remote machine.
#!/usr/bin/expect -f
set ipaddr [lindex $argv 0]
set password "mypassword"
set timeout 2
if {[llength $argv] == 0} {
send_user "Usage: ./set_ip.sh ipaddress/24\n"
exit 1
}
spawn sudo nmcli dev modify eth0 ipv4.addresses $ipaddr
expect {
password { send "$password\r" ; exp_continue }
timeout { send_user "\nFailed to get password prompt\n"; exit 1 }
eof exit
}
close
And I have a script to perform the SSH, then call the above remote script
#!/usr/bin/expect -f
set target [lindex $argv 0]
set newipaddr [lindex $argv 1]
set password "mysshpassword"
set timeout 2
spawn ssh $target
expect {
password: {send "$password\r"}
timeout { send_user "\nFailed to get password prompt\n"; exit 1 }
eof exit
}
send "~/set_ip.sh $newipaddr/r"
send "exit\r"
close
When the remote script gets called, it seems to run okay but the password prompt following the nmcli command is never received.
If someone could advise where I am going wrong I'd be grateful.
Also, this is the first time I've used bash, so feel free to express critique.
Thanks

Use of expect to run scripts on remote machine

I am working on a project that requires some assistance.
I have automated most of the information required for the completion of this project but the only thing that is lagging is the running of local shell scripts on the remote machine.
As we are aware that no Linux command is recognized by the script that uses the 'expect' library.
Herein we have two use cases that I have tried:
1) Running the desired list of commands on the remote server using only one expect script which has both the script execution as well as pushing of output using scp to the local machine, here is a snippet of this code:
`chmod 777 localscript.sh
cat > script1.sh <<- "ALL"`
`#!/usr/bin/expect
set password [lindex $argv 0];
set ipaddress [lindex $argv 1];
set timevalue [lindex $argv 2];
set timeout $timevalue
spawn /usr/bin/ssh username#$ipaddress /bin/bash < ./localscript.sh
expect "assword:"
send "$password\r"
set timeout $timevalue
spawn /usr/bin/scp username#$2:"/path/from/source/*" /path/to/destination/folder/
expect "assword:"
send "$password\r"
interact
ALL
chmod 777 script1.sh
./script1.sh $password $2 $timevalue`
2) Running the desired list of commands on the remote server in a separate expect script and using scp to get files in a different script:
`cat > script1.sh <<- "ALL" `
`#!/usr/bin/expect
set password [lindex $argv 0];
set ipaddress [lindex $argv 1];
set timevalue [lindex $argv 2];
set timeout $timevalue
spawn /usr/bin/ssh username#$ipaddress /bin/bash < ./localscript.sh
expect "assword:"
send "$password\r"
interact
ALL
cat > script2.sh <<- "ALL2"`
`#!/usr/bin/expect
set password [lindex $argv 0];
set ipaddress [lindex $argv 1];
set timevalue [lindex $argv 2];
set timeout $timevalue
spawn /usr/bin/scp username#ipaddress:"/path/from/source/*" /path/to/destination/folder/
expect "assword:"
send "$password\r"
interact
ALL2
chmod 777 localscript.sh script1.sh script2.sh
./script1.sh $password $2 $timevalue
sleep 5
./script2.sh $password $2 $timevalue`
I believe the above codes should both be valid in their own respect however, the output for the same seem to be quite unexpected:
1) Both the commands ssh and scp are being executed almost simultaneously after password is entered hence, it is not giving localscript enough time to do its job, here's the output I see:
spawn /usr/bin/ssh username#1.2.3.4 /bin/bash < ./localscript.sh
Warning private system unauthorized users will be prosecuted.
username#1.2.3.4's password: spawn /usr/bin/scp
username#1.2.3.4:"/home/some/file/*" /another/file/
Warning private system unauthorized users will be prosecuted.
username#1.2.3.4's password:
scp: /home/some/file/*: No such file or directory
Please note: This functionality is working fine without the involvement of expect
2) Here we are executing ssh and scp separately, however, it seems like it is unable to recognize that the file localscript.sh exists:
spawn /usr/bin/ssh username#1.2.3.4 /bin/bash < ./localscript.sh
Warning private system unauthorized users will be prosecuted.
username#1.2.3.4's password:
bash: localscript.sh: No such file or directory
Warning private system unauthorized users will be prosecuted.
username#1.2.3.4's password:
scp: /home/some/file/*: No such file or directory
Any feedback on the same would be appreciated, I think the first approach might be a feasible solution, except the fact that spawn is too fast and none of the 'sleep' or 'after' commands are helping/working. I think the second approach is also valid however it seems like there is a different way of running a local script on a remote server than the usual way we do on Linux when using 'expect'.
Sorry for so much elaboration, I am hoping to be out of my misery soon :)
Indeed the timeout you are setting is not working as you expect it to. Both scripts are spawned, and the expect "assword:" after each spawn is actually catching and reacting to the same password prompt.
expect is actually more sophisticated than a cursory glance would lead you to believe. Each spawn should return a PID, which you can use with your expect to look for output from a specific process.
expect can also be broken down into multiple parts, and have the ability to define subroutines. Here are some more advanced use examples https://wiki.tcl-lang.org/10045
In this specific case I would suggest waiting for the scp to complete before spawning the next process.
expect {
"assword:" {
send "$password\r"
exp_continue # Keep expecting
}
eof {
puts -nonewline "$expect_out(buffer)"
# eof so the process should be done
# It is safe to execute the next spawn
# without exp_continue this expect will
# break and continue to the next line
}
}

How to root login to remote machine in using shell script

I want to root login of a remote machine using shell script.
I tried with below shell command , I am able to login to remote machine , but I am unable to root login of remote machine.
#!/usr/bin/expect
set timeout 20
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
spawn ssh "$user\#$ip"
expect "Password:"
send "$password\r";
expect "/u/ainet->"
spawn "su -"
expect "Password:"
send "mypasswd\r";
interact
The above shell only prompt me to /u/ainet->, but I not able to execute the below command:
spawn "su -"
expect "Password:"
send "mypasswd\r";
on remote machine, edit /etc/ssh/sshd_config
PermitRootLogin = yes
then save & exit, restart sshd service
I cannot answer your question directly, as in: I don't know how to handle the password prompt properly.
My advice is to avoid prompts though. You can do this by allowing passwordless sudo commands.
explained here

Expect command asks for password again in Linux

I am new to Linux Shell Scripting.I need to execute a shell script that gets username, password, host-name, command-to-be-executed as 1,2,3,4 parameters respectively. I used expect command but it prompts for password again.The main theme of this script is to execute a command from a remote server. Password security issues is not a problem. The main issue is that I must not be prompted for password input. I have to run this for about 80 servers, so I cant provide password for each and every time. Please help me solving this.
My script:
echo username = $1
echo Password = $2
echo Host-Name = $3
echo Command to Be executed = $4
expect -c "spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no $1#$3 $4; expect \"*assword:*\"; send "$2"; interact
#expect eof
"
My Output:
username = root
Password = root#123
Host-Name = host-name
Command to Be executed = ls
spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no root#host-name ls
root#host-name's password:
~
first your index will start at 0 not 1 so username = $0
also you need to set your variables like so: (this is the expect script i use on sever hundred nodes)
#!/usr/bin/expect
set user [lindex $argv 0]
set password [lindex $argv 2]
set ip [lindex $argv 2]
set command [lindex $argv 3]
spawn ssh -o StrictHostKeyChecking=no "$user\#$ip"
expect "assword:"
send "$password\r";
expect "$ "
send "$command\r"
expect "$ "
send "exit\r"

How to write bash script that enters password after the first command? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Using expect to pass a password to ssh
I want to have ssh connection to a remote machine and instead of using ssh command along with the machine address and password, I just want to write a little function that executes the ssh command to the machine and enters the pass after the server asks it. I can write the ssh part but how can I make the script that enters also the pass when the host ask for it?
You may use expect script. You can pass arguments from cmd line. Sample code I write:
#!/usr/bin/expect
set timeout 100
set host [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set command [lindex $argv 3]
spawn ssh $username#$host $command
#puts $command
expect {
"(yes/no)?"
{
send "yes\n"
expect "*assword:" { send "$password\n"}
}
"*assword:"
{
send "$password\n"
}
}
You can use Expect tool
It's exactly what you need:
EDIT
#!/usr/bin/expect
set timeout 60
set user "yourName"
set machine "nameOfYourMachine"
set password "yourPassword"
set command "command that you want execute via ssh"
spawn ssh $user#$machine
while {1} {
expect {
eof {break}
"The authenticity of host" {send "yes\r"}
"password:" {send "$password\r"}
"*\]" {send "exit\r"}
"bash" {send "$command"}
}
}
wait
close $spawn_id
Just workaround it as you need

Resources