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
Related
I am trying to write a script that will auto configure mikrotik router, but some weird character appears as soon as password is sent through the script and logins the router. Below is the script that i wrote,
#!/bin/bash
read -p "Enter IP Address: " Public_IP
read -p "Enter username: " Username
read -sp "Enter Password: " Password
echo
expect -c "
spawn ssh -o StrictHostKeyChecking=no $Username#$Public_IP
set timeout -1
expect "password:"
send "$Password\r"
expect "\[admin*"
sleep 4
expect "\[admin*"
"
While I run this script I see below the following in the image,
So what i want to accomplish is to see the second image and send some commands for example;
/ip arp print
send above commmand for instance.
image after running the script
the actual interface i want to get and send command like that
you need to add +tc after username in ssh command
spawn ssh -o StrictHostKeyChecking=no $Username+tc#$Public_IP
it´s works for me
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.
I'm trying to find a way to delete a Gmail email using bash command line.
I tried to find something with mutt or alpine but I did not found a way to do it without launching the client.
The purpose is to delete the first email with one command line.
I had few issue so I prefered imap connection this is how I did it and I empty all the mailbox as I don't need an email in once my robot read them. Thanks Aserre for your help :
#!/usr/bin/expect
set timeout 1
set ip "imap.gmail.com"
set socket "993"
set user "myusername"
set pass "mypassword"
spawn openssl s_client -connect $ip:$socket -crlf
expect -re ".OK.*" {send "01 LOGIN $user $pass \r"}
expect -re "01 OK.*" {send "02 SELECT INBOX\r"}
expect -re "02 OK.*" {send "03 STORE 1:* +FLAGS (\\Deleted)\r"}
expect -re "03 OK.*" {send "04 EXPUNGE\r"}
expect -re "04 OK.*" {send "05 LOGOUT\r"}
Bye
I am a beginner in the development of scripts.I want to do 4 tasks in one script:
1. Connect to machine 1 by ssh
2.Retrieve the data and put it in a CSV file
3.Send the CSV file to a directory
4.connect to machine 2 by ssh and do step 2 and 3
Fisrt test :
#!/bin/bash
#Settings
DATE=$(date +%Y_%m_%d)
DATE1=$(date +%d/%m/%Y)
HIER=$(date -d '1 day ago' "+%Y-%m-%d")
HEURE=$(date +"%T")
#Ip adress of the machine
machine1= IP1
machine2= IP2
#Connexion SSH OLT1
ssh root#IP1
#Command for geting data on machine
enable
config
display XXXX > IP1.csv
#exit connection
exit
I do not have any result so i think
Do i make a loop for executing the task for the 2 machines?
Thanks
I'm trying tu use expect because i find RSA key problem by using SSH and shell script
#spawn ssh $host -l $user
expect "login:"
send "root\r"
expect "passe:"
send "test\r"
expect -re $prompt
send "terminal length 0\r"
expect -re $prompt
send "enable\r"
expect -re $prompt
send "config\r"
expect -re $prompt
send "display .....\r"
expect -re "\r\nend\r\n"
send "exit\r"
I don't know how i can put the result on csv file to a directory?
Besides, can i put the IP on #spawn assume that task should be done on 2 machines
Thanks for your feedback
I try thisaccording to your sample but the ouput file is empty:
#!/usr/bin/expect
spawn ssh X.X.X.X -l root
expect "User password:"
send "admin\n"
expect -re "device>"
send "enable\r"
expect -re "device#"
send "config\r"
expect -re "device(config)#"
send "display .......\r"
expect -re "{ <cr>||<K> }:"
send "\r"
set date [timestamp -format %C%y%m%d]
log_file -noappend /home/device/CSTtest.csv
You can run your command and export to out file in same ssh call like this:
# ...
# first machine command
ssh root#IP1 "<COMMAND>" > /PATH_LOCAL_TO_SAVE_FILE/IP1.csv
# second machine command
ssh root#IP2 "<COMMAND>" > /PATH_LOCAL_TO_SAVE_FILE/IP2.csv
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.!!!