Pass Variable Value In Ssh Command

Hello all,
I am trying to execute Code:
ssh $a@$b mkdir abc

in a shell script where, a and b are variables
a=username and b=server.ip
It is giving me an error Quote:
ssh connect to host port 22 connection refused
why am I getting the error?? ssh does not accept variables? Can anyone help.
Thanks in Advance


Similar Content



Calculation Using Variable Inside Variable

I have some variables which have stored numerical values like
Quote:
A1=10 A2=20 A3=25
B1=10 B2=15 B3=12 and so on
As well as another set of variables which stored these variables like
Quote:
var1=A1 var2=A2 var3=A3
var4=B1 var5=B2 var6=B3 and so on
I have to calculated things using second set of variable.
In the script, there is line
Code:
read number

user will enter number manually, suppose 500
I have to calculate using
Code:
expr 500 \* ${$var1} / 100

and output of this multiplied by
Code:
expr $above_output \* ${$var2} /100

and output of this multiplied by
Code:
expr $above_output \* ${$var3} /100

But not able to deal with two levels of variable. The calculation will be hard when it comes for three level and four level.

Please help.

How To Copy File From Remote Host To Local Host Then Delete From Remote Host

I have an expect script to SSH to a remote host and obtain some user inputs and information about the server/network configuration. The responses are saved in a text file that I then need to copy to my local host so that I can read the lines into variables for use in the parent shell script.

Is there a way to do this without needing to enter the username and password for the local host to use function scp? I have tried the following in my expect script to no avail:
Code:
spawn scp $usr@$host:$flnm .
expect {
	-re "(.*)assword:" { 
		send -s "$pswd\r"
	}
}

I have also tried to directly scp the file and enter the username and password to try to debug the issue, and that doesn't work either:
Code:
spawn scp file.txt user@host:file.txt
expect {
	-re "(.*)assword:" {
		send -s "password\r"
	}
	"you sure you want to continue connecting" {
		send -s "yes\r"
		exp_continue
	}
}

In both scenarios I have used exp_internal 1, and there are no errors. But I do not end up with the file on my local host.

Following the copy, I would like to delete the file from the remote host. Any suggestions on how to accomplish this?

How To Redirect The Error Message?

I have a hostlist and there are several hosts. I want to use ssh to connect to them. I want to see whether the host is available or not by the time length of ssh takes. If it take long than 5s (which means it is not available). Then stop it, and ssh to next host. Once a host is available, then output the name of host. I previously use nmap, but the IT security told it is not allowed to install it on the desktop on campus.
Code:
    HOSTS=`cat $host_list`
    for line in $HOSTS
    do 
    ssh -o ConnectTimeout=5s $line true >> /dev/null
    RESULT=$?
    if [ $RESULT -eq 0 ]
    then
    echo $line > succeed.txt
    else
    echo $line > fail.txt
    fi
    done

The above script is all I can do.
The problem is when it is failed, I want to redirect the error message
Code:
ssh: connect to host c28-0112-05.ad.mtu.edu port 22: No route to host

. But I still get the error message.

Misbehaving 'echo' Command On Cygwin

Hi,

I've used unix before, but have just started to play with CYGWIN (1.1.3) on windows XP (32 bit). I'm having trouble getting 'echo' to behave as I would expect.

I've created a script called runstuff which contains these lines....
Code:
#!/bin/bash
# This line works as expected....
echo "the user is <$USER> and the hostname is <$HOSTNAME>"
MYVAR1=bill
MYVAR2=fred
# This lines doesn't.....
echo "my first variable is<$MYVAR1> and my second is <$MYVAR2>"

The output I get is this...
Quote:
$ runstuff
the user is <Mike> and the hostname is <MikesLaptop>
> and my second is <fredl

In the first echo statement it is just echoing a couple of external variables - this works as expected. In the 2nd echo example the two variables are internal, but the displayed output isn't what I'd expect, my guess is that the first part of the output "my first variable is <bill" has somehow been overwritten by the remainder of the string. Or am I missing something?

I have tried exporting the variables, and I've experimented with wrapping them in quotes, but the results are always the same.

Any help gratefully received.

How To Solve Scp Ssh Port 22 Error ?

Hi!

Im trying to use scp to copy changed httpd.conf files to 2 remote machines (the script is executed in one machine from remote and the result is copied to the both).

scp -vvv first_machine_name@ip_address:filename \second_machine_name@second_ip_address:/usr/local/apache/conf

Using RHEL 5.7 with mremote in windows 7 im getting this error:

ssh: connect to host <ip address> port 22: Connection refused

How can I solve it ? Thanks.

Ncmpcpp Problems.

when I try to start ncmpcpp I get this:
Code:
% ncmpcpp                                                      ~
Couldn't connect to MPD (host = localhost, port = 6600): Connection refused

Sending SMB Client Commands Through Shell Script

Hello.

I have a shell script, which I am using to access the SMB Client:

Code:
#!/bin/bash
cd /home/username
smbclient //link/to/server$ password -W domain -U username
recurse
prompt
mput baclupfiles
exit

Right now, the script runs, accesses the server, and then asks for a manual input of the commands.

Can someone show me how to get the commands:
Code:
recurse, prompt, mput baclupfiles and exit

to be run by the shell script please?

Benefit Of Source Command

Hi,

What are the benefits that we get when we choose to source a file rather than execute it as a shell script?

and then in what way is export opposite to source?

It looks like they are doing the same thing, i.e making environment variables accessible in the local shell

Need Help To Get The Available Hosts Among Many Hosts

I have many hosts as following. But those hosts are dual operating system (Linux and Windows). I always run program background. If someone is using Linux, it is fine. But if someone is using windows or the host is offline. Then, I cannot use ssh. The way I do preiviously is first ssh one by one. And then find the hosts which are offline or windows. And write them down one by one. And then ssh to hosts except them. Let us assume the number of programs is less than the total available hosts. Can someone write a shell script to output the all available hosts to a file like "host_available"?
Here is the host file.
https://www.dropbox.com/s/vbz6w864y3...tlist.txt?dl=0
I am using ssh to connect the computers on campus. If the computer I am trying to connect is offline or using windows, the ssh will take long time, and finally it will failed. I write a shell script to generate the hostlist
Code:
#!/bin/bash
for i in `seq -w 1 28`
do
echo "c15-0330-$i.ad.mtu.edu"
# I would like to add a command here to see whether ssh c15-0330-$i.ad.mtu.edu it succeeds or not. And then output a #file which contains the all available host.
done
for i in `seq -w 1 20`
do
echo "c28-0112-$i.ad.mtu.edu"
done
for i in `seq -w 1 20`
do
echo "c28-112a-$i.ad.mtu.edu"
done

I do not how to set a certain time to see whether connection is successful or not (see the comment in the shell script).

Sending Mails With Port Number 587 Bash Script Help

Hi All,
We have some shell scripts which will send emails about backup, alerts etc using mail command. Example.
Code:
echo "Test Mail" | mail -s "Test" me@mydomain.com

I guess the above command just connect port number 25 of our mail server even if we do not define any port number. Now I would like add smtp port number as 587 in all of the bash scripts. I googled for this but could not get whatever I am expecting. So expecting your kind reply to define smtp port number as 587 in all of the bash scripts.

Thanks in advance.