You are not logged in.

#1 2016-06-17 15:25:21

sethropf
Member
Registered: 2016-05-27
Posts: 11

Novice learning to Bash script

I have been reading some helpful documentation on how to bash script, but I have run into a wall and cannot find the answer for which I am looking. I am sure I am doing something dumb, I am just not sure what. I would like to create a simple alias for ssh to automatically use my username, and in addition, start script to log the session with a specific filename and location. The first problem I have, is that using @ seems to enter a space I don't want. So:

alias sssh='ssh user.name@$1' gives me ssh username@ ip-address

That space after the @ is breaking it. If instead I do this:

alias sssh='ssh -l user.name $1'

I can get the first part to work, but then if I want to start script with a filename of $1-$(date +%F_%R).log like this:

alias sssh='ssh -l user.name $1 | script $1-$(date +%F_%R).log'

$1 seems to break and isn't used properly regardless of how I add it to the filename.

Is there a better answer for logging my sessions to these devices (routers, switches, firewalls) than script? If not, I additionally tried to change what directory script was saving the files too, but it did not like that at all. How do I do this?

Thank you in advance for any help.

Offline

#2 2016-06-17 15:30:26

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: Novice learning to Bash script

You can set the default user and other options in your ~/.ssh/config file.

https://wiki.archlinux.org/index.php/Se … figuration


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#3 2016-06-17 17:01:47

sethropf
Member
Registered: 2016-05-27
Posts: 11

Re: Novice learning to Bash script

Forgive me, but I am not sure how that helps me much. I have thousands upon thousands of devices I may be called on to troubleshoot/config and creating a host entry for each just isn't realistic. I don't see anything in there about logging the sessions either.

Offline

#4 2016-06-17 17:13:27

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: Novice learning to Bash script

sethropf wrote:

I have thousands upon thousands of devices I may be called on to troubleshoot/config and creating a host entry for each just isn't realistic.

You have a single username hardcoded in that alias; you can set it in the ssh config instead.

An alias simply substitutes the first word in a command. If you want to do something more, use a shell function.

Offline

#5 2016-06-17 17:18:23

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,772

Re: Novice learning to Bash script

You might look at ssh's -l switch.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#6 2016-06-17 17:20:27

sethropf
Member
Registered: 2016-05-27
Posts: 11

Re: Novice learning to Bash script

Raynman wrote:
sethropf wrote:

I have thousands upon thousands of devices I may be called on to troubleshoot/config and creating a host entry for each just isn't realistic.

You have a single username hardcoded in that alias; you can set it in the ssh config instead.

An alias simply substitutes the first word in a command. If you want to do something more, use a shell function.


Please don't think I am being argumentative, I appreciate you taking the time to respond. I just want to make sure I understand.

Is an alias not capable of doing what I am trying to do? It is definitely possible that I am misunderstanding what I have read online about aliases. The reason I ask is I am getting responses on how to do things in an alternative manner instead of just a pointer to what I am doing wrong in the manner I chose. In the mean time, I will look up documentation on functions, and see what I can put together with that. Thanks for the suggestion!

Last edited by sethropf (2016-06-17 17:21:14)

Offline

#7 2016-06-17 17:35:52

sethropf
Member
Registered: 2016-05-27
Posts: 11

Re: Novice learning to Bash script

ewaller wrote:

You might look at ssh's -l switch.

I used that successfully above, in:

alias sssh='ssh -l user.name $1'

However, I cannot get it to work with the second half of the command introducing script to log the session. The captured ip-address is not appending to the filename for some reason.

Offline

#8 2016-06-17 17:40:43

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: Novice learning to Bash script

Aliases don't take arguments, they simply substitute one string for another. In your examples the $1 isn't being expanded into anything. Taking your third attempt as an example

sssh 192.168.1.1

is equivalent to typing...

ssh -l user.name $1 | script $1-$(date +%F_%R).log 192.168.1.1

As the variable $1 isn't set to anything you get the error you describe.


If you want to use arguments then you need to use a function instead, for example...

function sssh () {
  ssh user@"$1" | script $1-$(date +%F_%R).log
}

Last edited by Slithery (2016-06-17 17:47:09)


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#9 2016-06-17 18:06:59

sethropf
Member
Registered: 2016-05-27
Posts: 11

Re: Novice learning to Bash script

slithery wrote:

Aliases don't take arguments, they simply substitute one string for another. In your examples the $1 isn't being expanded into anything. Taking your third attempt as an example

sssh 192.168.1.1

is equivalent to typing...

ssh -l user.name $1 | script $1-$(date +%F_%R).log 192.168.1.1

As the variable $1 isn't set to anything you get the error you describe.


If you want to use arguments then you need to use a function instead, for example...

function sssh () {
  ssh user@"$1" | script $1-$(date +%F_%R).log
}

Awesome! I will try that. The part of your statement that confuses me is that the $1 does work for the ssh command, but does not work for the script command. So it is definitely getting set.

Offline

#10 2016-06-17 18:15:28

sethropf
Member
Registered: 2016-05-27
Posts: 11

Re: Novice learning to Bash script

That function works great. Thanks to you, and everyone else that took the time to respond. I guess the $1 is only good up until the pipe. Someday I might even understand why!


Thanks again.

Offline

#11 2016-06-17 18:18:17

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: Novice learning to Bash script

sethropf wrote:

Awesome! I will try that. The part of your statement that confuses me is that the $1 does work for the ssh command, but does not work for the script command. So it is definitely getting set.

No, it isn't.

sssh 192.168.1.1
becomes...
ssh -l user.name $1 192.168.1.1

As $1 isn't set this is equivalent to...
ssh -l user.name 192.168.1.1

The IP address isn't coming from your alias, it's from you typing it in after the alias. This is also where the space is coming from in your first example.

Last edited by Slithery (2016-06-17 18:20:18)


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#12 2016-06-17 18:28:28

sethropf
Member
Registered: 2016-05-27
Posts: 11

Re: Novice learning to Bash script

slithery wrote:
sethropf wrote:

Awesome! I will try that. The part of your statement that confuses me is that the $1 does work for the ssh command, but does not work for the script command. So it is definitely getting set.

No, it isn't.

sssh 192.168.1.1
becomes...
ssh -l user.name $1 192.168.1.1

As $1 isn't set this is equivalent to...
ssh -l user.name 192.168.1.1

The IP address isn't coming from your alias, it's from you typing it in after the alias. This is also where the space is coming from in your first example.

Ok... Weird. Why then if I put 'echo $1' in the alias do I get the IP?

Offline

#13 2016-06-17 21:15:12

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: Novice learning to Bash script

Because once again, aliases do not take arguments.

Aliases expand to something else. Not unlike actual variables -- which can also be used as a command.

[eschwartz@arch ~]$ varcommand="echo blah"
[eschwartz@arch ~]$ $varcommand
blah
[eschwartz@arch ~]$ $varcommand 192.168.1.1
blah 192.168.1.1
[eschwartz@arch ~]$ echo $1

[eschwartz@arch ~]$ alias somealias="echo $1"
[eschwartz@arch ~]$ alias somealias
alias somealias='echo '
[eschwartz@arch ~]$ somealias 192.168.1.1
192.168.1.1
[eschwartz@arch ~]$ set -- blah
[eschwartz@arch ~]$ echo $1
blah
[eschwartz@arch ~]$ somealias 192.168.1.1
192.168.1.1
[eschwartz@arch ~]$ alias somealias="echo $1"
[eschwartz@arch ~]$ alias somealias
alias somealias='echo blah'
[eschwartz@arch ~]$ somealias 192.168.1.1
blah 192.168.1.1

Aliases are treated as though you typed the value of the alias rather than the alias itself.
Functions are actual commands, which accept positional parameters just like shellscripts.

Also, aliases are subject to variable expansion at the time the alias was created (standard variable expansion rules apply, check your quoting).

Last edited by eschwartz (2016-06-17 22:03:00)


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#14 2016-06-17 21:49:55

Ropid
Member
Registered: 2015-03-09
Posts: 1,069

Re: Novice learning to Bash script

sethropf wrote:

Ok... Weird. Why then if I put 'echo $1' in the alias do I get the IP?

You can set $1 on the command line like this:

set -- a b c

This will set $1 to "a", and $2 to "b", and $3 to "c".

If you now use an alias with $1 in it, bash will insert "a" in that spot. See here, with your "echo" example:

$ alias x='echo $1'

$ x test
test

$ set -- a b c

$ x test
a test

Btw., it's important in this example to use single quotes ( ' ) instead of double quotes ( " ) when defining the alias, so that bash will interpret the $1 whenever the alias is run, instead of when it is defined.

Last edited by Ropid (2016-06-17 21:51:10)

Offline

Board footer

Powered by FluxBB