or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

command-execution.mdconfiguration.mddevice-connection.mdexceptions.mdfacts.mdfilesystem.mdindex.mdoperational-tables.mdsoftware.md

device-connection.mddocs/

0

# Device Connection Management

1

2

Core device connection functionality for managing NETCONF and console sessions with Junos devices, including authentication, session management, connectivity testing, and connection lifecycle operations.

3

4

## Capabilities

5

6

### Device Class

7

8

Main class for NETCONF connections to Junos devices supporting SSH transport, authentication methods, session management, and automatic facts gathering.

9

10

```python { .api }

11

class Device:

12

def __init__(self, *vargs, **kvargs):

13

"""

14

Initialize Device connection object.

15

16

Parameters (passed as keyword arguments):

17

- host (str): Target device hostname or IP address (can also be first positional arg)

18

- user (str): Login username

19

- passwd (str): Login password

20

- port (int): NETCONF SSH port (default: 830)

21

- ssh_private_key_file (str): Path to SSH private key file

22

- ssh_config (str): Path to SSH config file

23

- timeout (int): Default RPC timeout in seconds

24

- gather_facts (bool): Automatically gather device facts on connect

25

- auto_probe (int): Auto-probe timeout before connection attempt

26

- sock_fd (socket): Existing socket file descriptor

27

- session_listener: NETCONF session event listener

28

- conn_open_timeout (int): Connection establishment timeout

29

- mode (str): Connection mode for console connections

30

- baud (int): Baud rate for serial console connections

31

- cs_user (str): Console server username

32

- cs_passwd (str): Console server password

33

- fact_style (str): Facts gathering style ('new' or 'old')

34

- cs_timeout (int): Console connection timeout

35

"""

36

```

37

38

### Connection Management

39

40

Methods for establishing, managing, and terminating device connections with comprehensive error handling and connection state management.

41

42

```python { .api }

43

def open(self, **kwargs):

44

"""

45

Open connection to the device.

46

47

Parameters:

48

- auto_probe (int): Probe timeout before connection

49

- gather_facts (bool): Gather facts after connection

50

- normalize (bool): Normalize RPC responses

51

52

Raises:

53

- ConnectError: Base connection error

54

- ConnectAuthError: Authentication failure

55

- ConnectTimeoutError: Connection timeout

56

- ConnectUnknownHostError: Unknown host

57

- ConnectRefusedError: Connection refused

58

"""

59

60

def close(self):

61

"""

62

Close the device connection.

63

64

Cleanly terminates the NETCONF session and releases resources.

65

"""

66

67

def probe(self, timeout=5):

68

"""

69

Test connectivity to device without establishing NETCONF session.

70

71

Parameters:

72

- timeout (int): Probe timeout in seconds

73

74

Returns:

75

- bool: True if device is reachable

76

77

Raises:

78

- ProbeError: Probe operation failed

79

"""

80

```

81

82

### Console Class

83

84

Console connection class for telnet and serial connections to Junos devices, supporting out-of-band management and console server connections.

85

86

```python { .api }

87

class Console:

88

def __init__(

89

self,

90

host=None,

91

user='root',

92

mode='telnet',

93

baud=9600,

94

timeout=0.5,

95

attempts=10,

96

ssh_private_key_file=None,

97

port=23,

98

cs_user=None,

99

cs_passwd=None,

100

gather_facts=True,

101

auto_probe=0,

102

fact_style='new',

103

cs_timeout=90

104

):

105

"""

106

Initialize Console connection object.

107

108

Parameters:

109

- host (str): Console server hostname or IP address

110

- user (str): Device login username (default: 'root')

111

- mode (str): Connection mode ('telnet' or 'serial')

112

- baud (int): Serial connection baud rate

113

- timeout (float): Operation timeout in seconds

114

- attempts (int): Connection retry attempts

115

- ssh_private_key_file (str): SSH private key for console server

116

- port (int): Console server port (default: 23 for telnet)

117

- cs_user (str): Console server username

118

- cs_passwd (str): Console server password

119

- gather_facts (bool): Gather facts after connection

120

- auto_probe (int): Auto-probe timeout

121

- fact_style (str): Facts gathering style

122

- cs_timeout (int): Console operation timeout

123

"""

124

125

def open(self, **kwargs):

126

"""

127

Open console connection to device.

128

129

Establishes telnet or serial console connection and performs

130

device login authentication.

131

"""

132

133

def close(self):

134

"""

135

Close console connection.

136

137

Cleanly terminates the console session.

138

"""

139

140

def zeroize(self):

141

"""

142

Perform device zeroize operation.

143

144

WARNING: This completely erases device configuration and

145

returns device to factory defaults.

146

"""

147

```

148

149

### Connection Properties

150

151

Properties for accessing connection state, device information, and session details.

152

153

```python { .api }

154

# Read-only properties

155

@property

156

def connected(self) -> bool:

157

"""Connection status (True if connected, False otherwise)"""

158

159

@property

160

def hostname(self) -> str:

161

"""Device hostname from facts or connection parameters"""

162

163

@property

164

def user(self) -> str:

165

"""Login username for the connection"""

166

167

@property

168

def master(self) -> bool:

169

"""True if connected to master routing engine"""

170

171

@property

172

def re_name(self) -> str:

173

"""Current routing engine name"""

174

175

# Read-write properties

176

@property

177

def timeout(self) -> int:

178

"""Default RPC timeout in seconds"""

179

180

@timeout.setter

181

def timeout(self, value: int):

182

"""Set default RPC timeout"""

183

184

@property

185

def transform(self):

186

"""XML transformation function for RPC responses"""

187

188

@transform.setter

189

def transform(self, func):

190

"""Set XML transformation function"""

191

```

192

193

## Usage Examples

194

195

### Basic NETCONF Connection

196

197

```python

198

from jnpr.junos import Device

199

200

# Basic connection with username/password

201

dev = Device(host='router1.example.com', user='admin', passwd='secret')

202

dev.open()

203

204

print(f"Connected to {dev.hostname}")

205

print(f"Connection status: {dev.connected}")

206

207

dev.close()

208

```

209

210

### SSH Key Authentication

211

212

```python

213

from jnpr.junos import Device

214

215

# Connection using SSH private key

216

dev = Device(

217

host='192.168.1.1',

218

user='admin',

219

ssh_private_key_file='/home/user/.ssh/id_rsa'

220

)

221

dev.open()

222

223

# Device is now connected

224

dev.close()

225

```

226

227

### Console Connection

228

229

```python

230

from jnpr.junos import Console

231

232

# Telnet console connection

233

console = Console(

234

host='console-server.example.com',

235

port=2001,

236

user='root',

237

mode='telnet'

238

)

239

console.open()

240

241

# Perform console operations

242

console.close()

243

```

244

245

### Connection with Auto-Probe

246

247

```python

248

from jnpr.junos import Device

249

250

# Test connectivity before connecting

251

dev = Device(host='router1.example.com', user='admin', passwd='secret')

252

253

# Probe first

254

if dev.probe(timeout=10):

255

print("Device is reachable")

256

dev.open()

257

else:

258

print("Device is not reachable")

259

```

260

261

### Connection Error Handling

262

263

```python

264

from jnpr.junos import Device

265

from jnpr.junos.exception import ConnectError, ConnectAuthError, ConnectTimeoutError

266

267

dev = Device(host='router1.example.com', user='admin', passwd='secret')

268

269

try:

270

dev.open()

271

print("Successfully connected")

272

except ConnectAuthError:

273

print("Authentication failed")

274

except ConnectTimeoutError:

275

print("Connection timed out")

276

except ConnectError as e:

277

print(f"Connection failed: {e}")

278

finally:

279

if dev.connected:

280

dev.close()

281

```

282

283

## Types

284

285

```python { .api }

286

# Connection parameters type

287

ConnectionParams = dict[str, any] # Dict with connection parameters

288

289

# Socket file descriptor type

290

SocketFD = int # File descriptor for existing socket connection

291

292

# Session listener type

293

SessionListener = object # NETCONF session event listener object

294

```