I am running the following script
#!/usr/bin/expect -f
set user [lindex $argv 0]
set pass [lindex $argv 1]
set PATH [lindex $argv 2]
set INV_PATH [lindex $argv 3]
spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user#localhost
expect "assword: "
send "$pass\r"
expect "$ "
send "echo $pass | /usr/local/bin/sudo -S $INV_PATH/orainstRoot.sh\r"
expect "$ "
send "cd $PATH/bin\r"
expect "$ "
send "echo $pass | /usr/local/bin/sudo -S cp oraenv coraenv sqlplus dbhome /usr/bin\r"
expect "$ "
send "echo $pass | /usr/local/bin/sudo -S $PATH/root.sh\r"
expect "Check"
send "\r"
Its working fine in some machines and for some machines its throwing the following error... I have checked, its not the PATH causing the issue.
/usr/local/bin/sudo: /scratch/prod_sw/app/oraInventory/orainstRoot.sh: command not found
$ echo PASSWD | /usr/local/bin/sudo -S cd /scratch/prod_sw/app/prod_sw/product/11.2.0/db_home/bin
/usr/local/bin/sudo: cd: command not found
$ echo PASSWD | /usr/local/bin/sudo -S cp oraenv coraenv sqlplus dbhome /usr/bin
cp: cannot stat `oraenv': No such file or directory
cp: cannot stat `coraenv': No such file or directory
cp: cannot stat `sqlplus': No such file or directory
cp: cannot stat `dbhome': No such file or directory
$ echo PASSWD | /usr/local/bin/sudo -S /scratch/prod_sw/app/prod_sw/product/11.2.0/db_home//root.sh
Check /scratch/prod_sw/app/prod_sw/product/11.2.0/db_home/install/root_slcad22rhu_2013-07-22_04-41-49.log for the output of root script
Try to give complete path for these files and check once.
cp /path/oraenv /path/coraenv /path/sqlplus /path/dbhome /usr/bin
Related
I tried limiting ls command to a specific user. It works, but when I execute /bin/ls, it executes successfully again, how to restrict here.
useradd -m $username -s /bin/rbash
echo "$username:$password" | chpasswd
mkdir /home/$username/bin
chmod 755 /home/$username/bin
echo "PATH=$HOME/bin" >> /home/$username/.bashrc
echo "export PATH" >> /home/$username/.bashrc
ln -s /bin/ls /home/$username/bin/
When I run the below code, I'getting this error
bash: /var/out.txt: No such file or directory
#!/usr/bin/expect
set timeout -1
spawn ssh user#10.103.234.1 'ls -t /var/backups/archives/' > /var/outp.log
expect "user#10.103.234.1's password:"
send "Password\n"
expect eof
if [catch wait] {
puts "failed"
exit 1
}
exit 0
Expect/Tcl does not understant the redirection (>) char. Try this:
spawn bash -c "ssh user#10.103.234.1 ls -t /var/backups/archives/ > /var/outp.log"
use tee replaced
spawn ssh user#10.103.234.1 'ls -t /var/backups/archives/|tee -a /var/outp.log'
I have linux script like below:
sshpass -p "pwd" ssh -tt user << 'EOF'
cd /directory
file=$(ls -1t | head -1)
exit
EOF
How to use the file parameter outside ssh. That is after EOF statement.
I think that you have to work with the output of the SSH command to capture it into a local variable.
This could be a viable solution (tried with obviously different parameters locally, OS Ubuntu 17.04):
CMD=`cat <<EOF
cd /directory
ls -1t | head -1
EOF`
FILE=`sshpass -p "pass" ssh -t user#host -o LogLevel=QUIET "$CMD"`
echo "$FILE"
I have a expect script named load_data.exp placed in home directory
#!/usr/bin/expect
spawn osm2pgsql -s -l -d postgres -W -U postgres -H $OSM_DATABASE_PORT_5432_TCP_ADDR -P 5432 --hstore $filename
expect "Password"
send "$OSM_DATABASE_ENV_POSTGRES_PASSWORD\n"
interact
there is a environment variable OSM_DATABASE_PORT_5432_TCP_ADDR with has the value of 172.17.0.13 verified by
echo $OSM_DATABASE_PORT_5432_TCP_ADDR
output
172.17.0.13
run the load_data.exp by ./load_data.exp, I got the error
can't read "OSM_DATABASE_PORT_5432_TCP_ADDR": no such variable
while executing
"spawn osm2pgsql -s -l -d postgres -W -U postgres -H $OSM_DATABASE_PORT_5432_TCP_ADDR -P 5432 --hstore $filename"
(file "../load_data.exp" line 4)
seems to be that spawn can not have access to environment variable DATABASE_PORT_5432_TCP_ADDR
You can pass Bash variables to the Expect the following way:
#!/usr/bin/expect
set HOST [lindex $argv 0]
set FILENAME [lindex $argv 1]
set PASSWORD [lindex $argv 2]
spawn osm2pgsql -s -l -d postgres -W -U postgres -H $HOST -P 5432 --hstore $FILENAME
expect "Password"
send "$PASSWORD\n"
interact
Then call your expect script like this:
load_data.exp $OSM_DATABASE_PORT_5432_TCP_ADDR $filename $OSM_DATABASE_ENV_POSTGRES_PASSWORD
I need to iterate on a sequence of servers(many type of servers, each type of servers stored in separate files), run on them some commands(stored in different files accordingly to server type) and log the output on the local machine, using ssh with password. Since sshpass, ssh key authentication is not a solution for my case please don't recommend them.
Here is my code:
#!/usr/local/bin/expect -f
#Set path to nodes files
NODES=nodes/*
#Set path to commands files
CMD=commands/*
for fn in $NODES
do
echo "Working in $fn"
for fc in $CMD
do
echo "Working in $fc"
if [ ${fn:6:3} = ${fc:9:3} ]
then
# read Nodes from file
while read fn_line; do
#extracting substrings of user, host, password separated by comma
IFS=', ' read -a uhp <<< $fn_line
#establish ssh session to the node
eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no ${uhp[0]}#${uhp[1]}
echo ${uhp[0]} ${uhp[1]} ${uhp[2]}
#use correct prompt
set prompt "assword:*"
interact -o -nobuffer -re $prompt return
send "${uhp[2]}\r"
set prompt ":|#|\\\$"
interact -o -nobuffer -re $prompt return
#execute and logging HC commands on the node
while read fc_line; do
#set prompt ":|#|\\\$"
#interact -o -nobuffer -re $prompt return
echo "$fc_line\r" >> logs/${fn:6:3}.log
$fc_line\r >> logs/${fn:6:3}.log
#interact -o -nobuffer -re $prompt return
done < $fc
done < $fn
fi
#cat $f
done
done
I know in my code the problem is combination of bash and expect interpreter. Please help me to do it only in expect style or show me how can i combine bash with expect. Other problem is the while after establishing of ssh connection, but i think it can be solved by storing it previously in an array and looping through it after establishing of ssh connection.
How about writing a small utility in expect which spawns ssh command:
#!/usr/bin/expect
set HOST [lindex $argv 0]
set PORT [lindex $argv 1]
set USER [lindex $argv 2]
set PASSWORD [lindex $argv 3]
set COMMAND [join [lrange $argv 4 end] " "]
spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no -p $PORT $USER#$HOST $COMMAND
expect "assword:"
send "$PASSWORD\r"
expect eof
exit
and using it in a Bash script like this:
ssh-util <host> <port> <user> <pass> <command>
e.g.
ssh-util 10.0.0.10 22 root s3cr3t ls -la